select m, p from various_joined_tables_and_views where u = N group by m, p, u The result looks like this:

m1, p1 m1, p2 m1, p3 m2, p1 m2, p2 m3, p1 . . . alot more #### [[m1, [p1,p2,p3], [m2, [p1,p2]], [m1, [p1]]] #### my @data; my %mkts; my ($m, $p); $cursor->bind_col(1, \$m); # this is in the hope that DBD::Oracle will actually implement # bind types in the future - # see http://rt.cpan.org/Public/Bug/Display.html?id=49818 # because I'd really like this column to be scalar that looks like an number # rather than a string - as JSON::XS will write it as N instead of 'N' $cursor->bind_col(2, \$p, {type => SQL_INTEGER}); while ($cursor->fetch) { if (exists($mkts{$m})) { foreach (@data) { if ($_->[0] == $m) { push @{$_->[1]}, $p + 0; # make it look like a integer - see above last; } } } else { $mkts{$m} = 1; my $n = @data; $data[$n][0] = $m; $data[$n][1] = [$p] + 0; } }