SELECT monthKey, group, person, stat FROM areas a, stats s WHERE a.personId=s.personId ORDER BY monthKey, group, person, stat ; #### SELECT monthKey, group, person, stat FROM areas a, stats s WHERE a.personId=s.personId AND s.monthKey > 200401 AND s.monthKey < 200406 ORDER BY monthKey, group, person, stat ; #### my $sth = $dbh->prepare($query); $sth->execute(); my $current_month; my $current_group; my $current_person; while (my $rv = $sth->fetchrow_arrayref()); if ($rv[0] ne $current_monthkey) { # print out new month header $current_month = $rv[0]; undef $current_group; undef $current_person; } if ($rv[1] ne $current_group) { # print out new group header $current_group = $rv[0]; undef $current_person; } if ($rv[2] ne $current_person) { # print out new person header $current_person = $rv[1]; } # print out stat } $sth->finish(); $dbh->disconnect();