I've been having a heck of a time getting the following DBIx::Class::ResultSet working and have tracked a couple of bugs through DBIx::Class::Storage::DBI (or SQL::Abstract, really).&nbsp; Here's the ResulSet:<br><br><span style="font-family: courier new,monospace;">
sub most_for_campaign {<br>&nbsp; my ($self, $limit) = @_;<br><br>&nbsp; return $self-&gt;search(undef,<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { select =&gt; [ 'Id', 'first_name', 'last_name',<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { count =&gt; 'Id' } 
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ],<br>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; as =&gt; [ qw/ Id first_name last_name total / ],<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rows =&gt; $limit,<br>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; group_by =&gt; [ qw/ Id first_name last_name / ],
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; });<br>}<br><br></span>First of all, I'm using a MS SQL database with the DBD::Sybase driver.<br><br>Because of the way SQL::Abstract::Limit rewrites MS SQL queries, that turns into:<br><br><span style="font-family: courier new,monospace;">
SELECT * FROM<br>(<br>&nbsp;&nbsp;&nbsp; SELECT TOP 10 * FROM<br>&nbsp;&nbsp;&nbsp; (<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SELECT TOP 10&nbsp; Id, first_name, last_name, COUNT( Id ) FROM contacts me <br>&nbsp;&nbsp;&nbsp; ) AS foo<br>&nbsp;&nbsp;&nbsp; <br>) AS bar<br><br></span>which breaks MS SQL because there is no implicit name for the COUNT( Id ) &quot;column.&quot;&nbsp; I fixed this with the following patch to DBIx::Class::Storage::
DBI.pm in _recurse_fields.<br><br><span style="font-family: courier new,monospace;">-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .'( '.$self-&gt;_recurse_fields($fields-&gt;{$func}).' )';</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .'( '.$self-&gt;_recurse_fields($fields-&gt;{$func}).' ) as _' . $func;</span><br style="font-family: courier new,monospace;"><br>The second issue I don't have an easy fix for.<br><br>DBIC::SQL::Abstract (inside DBIx::Class::Storage::DBI), which subclasses SQL::Abstract::Limit, overrides the select method.&nbsp; The $order parameter becomes a HASH ref instead of a string, and the group_by ARRAY ref gets stuck inside $order.&nbsp; DBIC::SQL::Abstract also overrides the SQL::Abstract::_order_by method to process the HASH ref and appends the contents of group_by to the query (assuming everything works well).&nbsp; There are a couple of problems with the SQL::Abstract::Limit::select method (before it calls the SQL::Abstract::select method).&nbsp; First, and the correct line is commented out, if $rows is not defined, the SQL::Abstract::Limit::select calls SQL::Abstract-&gt;new-&gt;select instead of $self-&gt;SUPER::select which would do cause the carefully overridden SQL::Abstract methods in DBIC::SQL::Abstract to be ignored.&nbsp; If $rows is defined (in my case), the call to $self-&gt;SUPER::select doesn't pass the $order parameter because, according the the comment, &quot;with LIMIT parameters, get the basic SQL without the ORDER BY clause.&quot;
<br><br>Is the answer to always pass $order to the SQL::Abstract::select method?&nbsp; Should the SQL::Abstract-&gt;new-&gt;select line be changed to $self-&gt;SUPER::select above?<br><br>Thanks in advance for your help.<br><br>
-matt<br><br>