report_no, reported_by project_no date_reported cat1 cat2 cat3 cat4 cat5 cat6 cat7 cat8 cat9 cat10 cat11 cat12 cat13 cat14 cat15 #### use strict; $sth = $dbh->prepare(qq{ SELECT report_no, reported_by, project_no, to_char(date_reported,'DD-MON-YYYY'), cat1 . . . cat15 FROM TABLE ORDER BY report_no DESC }); $sth->execute(); $array_ref = $sth->fetchall_arrayref(); $i = 0; foreach $row (@$array_ref) { ($report_no,$reported_by,$project_no,$date_reported) = @$row[0..3]; #Categories in each row may have undef elements, replace #with a '-'. For defined values, sum them up. This sum is #used to calculate the average for each row. $number = 0; $sum = 0; foreach $element (@$row[4..18]) { if ($element == -1) { $element = '-' } else { $element = sprintf "%.2f", $element; $sum += $element; ++$number; } } $row_avge = sprintf "%.2f", $sum / $number; $i++; } $sth->finish(); $dbh->disconnect;