in reply to Re: Get top N rows in each group with DBIx::Class
in thread Get top N rows in each group with DBIx::Class

Hi Shadowsong, thanks very much for your reply and demo. I had seen that part of the doc and it looks promising, but the problem I am facing is that this filter ('N per "group"') must be applied after some other search calls on the RS.

Continuing the metaphor, this would be something like:

  1. Restrict Artists to given musical genre
  2. Restrict Albums to those release since given date
  3. Restrict results to maximum N Albums per Artist
In my app I have the first two steps handled with methods in My::Schema::ResultSet::CD:
sub by_genre { $_[0]->search({ 'me.genre' => $_[1] }); sub since { $_[0]->search({ 'me.released' => { '>=', $_[1] } });
So the consuming code can do:
my $rs = $schema->resultset('CD'); if ( my $genre = param->{'genre'} ) { $rs = $rs->by_genre( $genre ); } if ( my $since = param->{'since'} ) { $rs = $rs->since( $since ); }
I need to add a method that can act upon the existing ResultSet,
if ( my $limit = param->{'limit'} ) { $rs = $rs->limit_per_artist( $limit ); }
It's not clear to me that the doc you linked to and demoed can be adapted to search an existing RS, but I'll keep experimenting. Thanks again.


The way forward always starts with a minimal test.