Can I have a GROUP BY as an expression? Without an alias I want to be able to achieve the following query.

SELECT FROM_UNIXTIME(ROUND(UNIX_TIMESTAMP(datetime)/60)*(60)) AS datetime,
ROUND(MAX(value), 2) AS value 
FROM  my_values v
WHERE (datetime &gt; '2012-12-15 00:00:00') AND (datetime &lt; '2013-01-01 00:00:00')  
GROUP BY ROUND(UNIX_TIMESTAMP(datetime)/60)
ORDER BY datetime;


You'll notice that my groupby clause is dynamic, so far in Drupal I have:

$select = db_select('my_values', 'v')
  -&gt;condition('datetime', '2012-12-15 00:00:00', '&gt;')
  -&gt;condition('datetime', '2013-01-01 00:00:00', '&lt;')
  -&gt;groupBy('gbdatetime')
  -&gt;orderBy('datetime', 'DESC');
$select-&gt;addExpression('FROM_UNIXTIME(ROUND(UNIX_TIMESTAMP(datetime)/60)*60)', 'datetime');
$select-&gt;addExpression('ROUND(UNIX_TIMESTAMP(datetime)/60)', 'gbdatetime');
$select-&gt;addExpression('ROUND(MAX(value), 2)', 'value');


This achieves the desired results, however It'd be great if I could do a GROUP BY ROUND(UNIX_TIMESTAMP(datetime)/60) without requiring the alias gbdatetime.

So, my question is can I have a GROUP BY as an expression?
This can be achieved like so:

$select = db_select('my_values', 'v')
  -&gt;condition('datetime', '2012-12-15 00:00:00', '&gt;')
  -&gt;condition('datetime', '2013-01-01 00:00:00', '&lt;')
  -&gt;orderBy('datetime', 'DESC');
$select-&gt;addExpression('FROM_UNIXTIME(ROUND(UNIX_TIMESTAMP(datetime)/60)*60)', 'datetime');
$select-&gt;addExpression('ROUND(MAX(value), 2)', 'value');

$groupBy = &amp;$select-&gt;getGroupBy();
$groupBy['gb'] = 'ROUND(UNIX_TIMESTAMP(datetime)/60)';

$result = $select-&gt;execute();


Which creates this query:

SELECT FROM_UNIXTIME(ROUND(UNIX_TIMESTAMP(datetime)/60)*60) AS datetime,
ROUND(MAX(value), 2) AS value 
FROM my_values v 
WHERE (datetime &gt; '2012-12-15 00:00:00') AND (datetime &lt; '2013-01-01 00:00:00') 
GROUP BY ROUND(UNIX_TIMESTAMP(datetime)/60) 
ORDER BY datetime DESC