如何按2个字段分组并按间隔类型?

时间:2013-11-06 04:08:07

标签: postgresql group-by sum intervals having

我有这张桌子:

TABLE T (
id int, 
month int,
interval hours
);

我希望按ID和月分组,并添加小时数。 例如:

id  month  hours
-------------------
 1  1      08:00:00  
 1  1      09:00:00 
 1  2      10:00:00 
 1  2      11:00:00 

我想:

1 1  17:00:00
1 2  21:00:00

我试过了:

SELECT * FROM T 
GROUP BY T.id , T.month
HAVING SUM( SELECT EXTRACT ( epoch FROM T.hours ) / 3600 );

但它没有用,我无法修复它。

1 个答案:

答案 0 :(得分:0)

SELECT
  id,
  month,
  sum(extract ('epoch' from hours)/3600)
FROM
  hours
GROUP BY
  id,
  month

SQL Fiddle