在postgres中将结果行合并为一行

时间:2013-11-28 11:10:43

标签: sql postgresql

我在postrges中获得以下数据

id|    count1     count2
12     3          0
12     0          2

但我想要综合结果

我从中获得结果

的查询
SELECT
    date_part( 'day', t1.requested_datetime ) AS requested_day,
    CASE
      WHEN date_part ( 'hour', t1.requested_datetime ) >= 0 AND date_part ( 'hour', t1.requested_datetime ) <= 11 THEN count ( date_part ( 'day', t1.requested_datetime ) )
      ELSE 0
    END AS count_am,
    CASE
      WHEN date_part ( 'hour', t1.requested_datetime ) > 11 AND date_part ( 'hour', t1.requested_datetime ) <= 23 THEN count ( date_part ( 'day', t1.requested_datetime ) )
      ELSE 0
    END AS count_pm
FROM
    table1 t1
WHERE
    date_part ( 'month', t1.requested_datetime ) = 11
    AND date_part ( 'year', t1.requested_datetime ) = 2013
GROUP BY
    requested_day,
    date_part ( 'hour', t1.requested_datetime )
ORDER BY
    date_part ( 'day', t1.requested_datetime )

预期结果

id    count1   count2
12    3        2

请帮忙 感谢

1 个答案:

答案 0 :(得分:0)

select id, 
       sum(count1) as count1,
       sum(count2) as count2
from your_table
group by id