my %records; while( my $row = $sth->fetchrow_hashref ) { push @{ $records{ $row->{start_date} } }, $row; } #### foreach my $date( sort keys %records ) { print $date, "\n"; # then get each record, sorted by the start time my @recs = sort { $a->{start_time} cmp $b->{start_time} } @{ $records{$date} }; foreach my $rec( @recs ) { print $rec->{id_number}, "\t", $rec->{start_time}; print "\n"; } }