select category||type as grp, max(category) as cat, max(type) as typ, sum(quantity) as qty from table group by category || type #### sub group_by { my ($rows, $group_keys) = @_; my @recs=(); for my $row (@$rows) { push @recs, { GRP=>join(chr(28),@{$row}{@$group_keys}), %$row }; } return @recs; }