That did it! Thanks so much. This new query runs about a jillion times faster than the old one, which had to execute once for each day in the date range.<br><br><div><span class="gmail_quote">On 7/27/06, <b class="gmail_sendername">
Jason Galea</b> <<a href="mailto:lists@eightdegrees.com.au">lists@eightdegrees.com.au</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>not sure if this will work, but it seems like it should..<br><br>I recently got an order_by working like this..<br><br>order_by => ['"sum( quantity )" DESC'],<br><br>so maybe..<br><br>group_by => [<br>
'"DAYOFMONTH(DATE_ADD(date, INTERVAL -12 HOUR))"',<br> 'sensorid',<br> '"DATE_FORMAT(DATE_ADD(date, INTERVAL -12 HOUR),'%b')"'<br>]<br><br><br>cheers,<br><br>J<br><br>Sarah Berry wrote:<br>> I have a query where I need to group by several fields. I set it up in
<br>> SQL first to make sure that it would work on my db, and now I'm trying<br>> to put it into the proper DBIx format. The problem is that DBIx<br>> doesn't seem to like me using column aliases in my group_by clause.
<br>><br>> I found a message in the archives from this month (July 7, "help on<br>> group_by and select as?") that verified that I can't use aliases in<br>> group_by. How can I rephrase my query in a way that is tasteful to
<br>> DBIx? Should I just grit my teeth and use the straight SQL that<br>> already works?<br>><br>> The SQL query:<br>> SELECT<br>> DATE_ADD(date, INTERVAL -12 HOUR) AS newdate,<br>> DAYOFMONTH(DATE_ADD(date, INTERVAL -12 HOUR)) AS dayofmonth,
<br>> DATE_FORMAT(DATE_ADD(date, INTERVAL -12 HOUR),'%b') AS month,<br>> count(*)<br>> FROM tblsensorlog<br>> WHERE userid = 3003<br>> AND sensorid > 14 AND sensorid < 19<br>> AND date > '2006-06-01 11:59:59' AND date < '2006-07-01 12:00:00'
<br>> GROUP BY sensorid, month, dayofmonth<br>> ORDER BY newdate, sensorid;<br>><br>> The prose version: Find out how many times each sensor fired each<br>> night, where "night" is defined as anything between noon of one day
<br>> and noon of the next day, over several nights, for one user.<br>><br>> The DBIx attempt:<br>> my $range = {<br>> userid => "$userid" ,<br>> date => { '>' => "$tempdate->{'sql'} 11:59:59",
<br>> '<' =><br>> ($tempdate2+1)->{'sql'}." 12:00:00" },<br>> sensorid => { '>' => '14', '<' => '19' },<br>
> };<br>> my $fields = {<br>> select => [ "DATE_ADD(date, INTERVAL -12 HOUR)",<br>> "DATE_FORMAT(DATE_ADD(date,
<br>> INTERVAL -12 HOUR),'%b')",<br>> "DAYOFMONTH(DATE_ADD(date,<br>> INTERVAL -12 HOUR))",<br>> 'sensorid',<br>> { count => '*' }
<br>> ],<br>> as => [ 'newdate', 'month', 'daymonth',<br>> 'sensorid', 'qcount' ],<br>> group_by => [ 'daymonth', 'sensorid',
<br>> 'month', 'daymonth' ],<br>> order_by => [ 'newdate', 'sensorid' ]<br>> };<br>> my $rs =<br>> $schema->resultset('Tblsensorlog')->search($range,$fields);
<br>><br>> The error message:<br>> DBD::mysql::st execute failed: Unknown column 'newdate' in 'order<br>> clause' at /usr/lib/perl5/vendor_perl/5.8.8/DBIx/Class/Storage/DBI.pm<br>> line 525.<br>> DBD::mysql::st execute failed: Unknown column 'daymonth' in 'field
<br>> list' at /usr/lib/perl5/vendor_perl/5.8.8/DBIx/Class/Storage/DBI.pm<br>> line 525.<br>><br>> Thanks for your help.<br>><br>> - Sarah<br>><br>> _______________________________________________<br>
> List: <a href="http://lists.rawmode.org/cgi-bin/mailman/listinfo/dbix-class">http://lists.rawmode.org/cgi-bin/mailman/listinfo/dbix-class</a><br>> Wiki: <a href="http://dbix-class.shadowcatsystems.co.uk/">http://dbix-class.shadowcatsystems.co.uk/
</a><br>> IRC: irc.perl.org#dbix-class<br>> SVN: <a href="http://dev.catalyst.perl.org/repos/bast/trunk/DBIx-Class/">http://dev.catalyst.perl.org/repos/bast/trunk/DBIx-Class/</a><br>> Searchable Archive: <a href="http://www.mail-archive.com/dbix-class@lists.rawmode.org/">
http://www.mail-archive.com/dbix-class@lists.rawmode.org/</a><br>><br><br>_______________________________________________<br>List: <a href="http://lists.rawmode.org/cgi-bin/mailman/listinfo/dbix-class">http://lists.rawmode.org/cgi-bin/mailman/listinfo/dbix-class
</a><br>Wiki: <a href="http://dbix-class.shadowcatsystems.co.uk/">http://dbix-class.shadowcatsystems.co.uk/</a><br>IRC: irc.perl.org#dbix-class<br>SVN: <a href="http://dev.catalyst.perl.org/repos/bast/trunk/DBIx-Class/">http://dev.catalyst.perl.org/repos/bast/trunk/DBIx-Class/
</a><br>Searchable Archive: <a href="http://www.mail-archive.com/dbix-class@lists.rawmode.org/">http://www.mail-archive.com/dbix-class@lists.rawmode.org/</a><br></blockquote></div><br>