Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to ORDER before GROUP with DBIx::Class
    primarykey
    data
    text
    <p>I've got a simple temporal table that looks like this:</p> <pre><code>Table: item_approval item user status modified 2 fred approved 2010-12-01 00:00:00 3 fred approved 2010-12-02 00:00:00 4 fred disapproved 2010-12-03 00:00:00 7 jack unapproved 2010-12-05 00:00:00 4 fred approved 2010-12-06 00:00:00 4 jack unapproved 2010-12-07 00:00:00 4 fred disapproved 2010-12-04 00:00:00 </code></pre> <p>I'm using DBIx::Class. My "Item" result is defined with:</p> <pre><code>__PACKAGE__-&gt;has_many( "item_approvals", "Schema::Result::ItemApproval", { "foreign.item" =&gt; "self.id" }, { cascade_copy =&gt; 0, cascade_delete =&gt; 0 }, ); </code></pre> <p>Which means I can do:</p> <pre><code>my $item = $schema-&gt;resultset('Item')-&gt;find({id=&gt;4}); </code></pre> <p>Which is fine. Then, I can do:</p> <pre><code>my @approvals = $item-&gt;item_approvals; </code></pre> <p>to get a resultset like this:</p> <pre><code>item user status modified 4 fred disapproved 2010-12-03 00:00:00 4 fred approved 2010-12-06 00:00:00 4 jack unapproved 2010-12-07 00:00:00 4 fred disapproved 2010-12-04 00:00:00 </code></pre> <p>My question: How do I get the set of Fred and Jack's single most recent approval status? That is, I want to get this resultset:</p> <pre><code>item user status modified 4 fred approved 2010-12-06 00:00:00 4 jack unapproved 2010-12-07 00:00:00 </code></pre> <p>I tried things like this:</p> <pre><code>my @approvals = $item-&gt;search({}, { group_by =&gt; 'user', order_by =&gt; {-desc =&gt; 'modified'} }); </code></pre> <p>but the "ORDER BY" is executed <em>after</em> the "GROUP BY", so I get things like this instead:</p> <pre><code>item user status modified 4 fred disapproved 2010-12-03 00:00:00 4 jack unapproved 2010-12-07 00:00:00 </code></pre> <p>Help?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload