my $stmt = "SELECT prod, count(*) from prod GROUP BY prod WITH ROLLUP;"; $sth = $dbh->prepare( $stmt ); $sth->execute(); my %pivot; while ( my ( $k, $v ) = $sth->fetchrow_array() ) { $k = 'Total' unless defined $k; $k = 'Unknown' if $k eq ''; $pivot{$k} = $v; }