我在postgresql中有一个表,它是warhouse活动的日志:
productid action qty time
5436 in 30 2014-09-18 11:46:25.113862
5436 in 30 2014-09-18 11:46:25.113862
5436 in 30 2015-10-18 08:13:11.124452
5436 in 30 2015-10-18 10:15:22.541125
2048 in 20 2014-09-18 11:46:35.873401
2048 in 80 2014-09-18 11:46:35.873401
2048 out 20 2014-09-18 11:46:35.873401
time
是TIME STAMP WITHOUT TIME ZONE
我希望每天SUM
获得qty
in
(productid
} productid qty date
5436 60 2014-09-18
5436 60 2015-10-18
2048 80 2014-09-18
。
对于上表,结果将是:
Select sum(qty)
from A
where action like 'in'
group by (partid,time)
Normaly我会这样做:
2015-10-18 08:13:11.124452
然而,这不起作用,因为它不知道产品2015-10-18 10:15:22.541125
的{{1}}和5436
应该组合在一起。
答案 0 :(得分:2)
这应该适合你:
select productid, time::DATE as dt, sum(qty)
from tablename
where action like 'in'
group by productid, time::DATE
请注意,我们正在将您的时间戳转换为日期以获得所需的结果。
示例SQLFiddle:http://sqlfiddle.com/#!15/45b37/2
答案 1 :(得分:1)
SELECT productid --1
,sum(qty) qty -- 2
,TIME::DATE DATE --3
FROM A
WHERE action = 'in'
GROUP BY 1 --(productid)
,3 --(TIME::DATE DATE)