我有一个这样的数据库:
descr | hours | timer
----------+-------+------------
foo | 1.3 | 2012-07-14
foo | 2.5 | 2012-07-15
foo | 2.35 | 2012-07-16
bar | 1 | 2012-08-16
baz | 1 | 2012-08-16
buh | 5 | 2012-08-17
这符合我的期望:
abc=# select extract('month' from timer) as Month, sum(hours) from foo group by Month order by 1;
month | sum
------+------
7 | 6.15
8 | 7
这并不完全符合我的预期:
abc=# select to_char(timer, 'Month'), sum(hours) from foo group by 1 order by 1 asc;
to_char | sum
----------+------
August | 7
July | 6.15
期望的结果是:
month | sum
----------+-----
July | 6.15
August | 7
我理解为什么会在 #3 中发生事情,但是如何在 PostgreSQL 中以最有效的方式实现预期的结果?