# Ok, with SELECT DISTINCT * and using Perl for Printout $sth= $dbh->prepare ('SELECT DISTINCT * from test'); $sth -> execute; my $ref_distinct_rows = $sth->fetchall_arrayref; my %result; foreach my $row (@$ref_distinct_rows) { push (@{$result{"$row->[1] $row->[2]"}}, $row->[3]); } foreach my $key (sort keys %result) { print "$key ",join (':',sort @{$result{$key}}),"\n"; }