我有下表,我需要汇总
Id Month Days Hours Audit
1 201803 20 30 Yes
1 201803 20 15 Yes
1 201802 19 4 No
2 201803 20 5 Yes
预期产出:
Id Month Days Hours Audit Total
1 201803 20 2 Yes 100
1 201803 20 3 Yes 100
1 201802 10 4 No
2 201803 20 5 Yes 100
要点: ID&分区月 聚合日&小时
我的SQL :(我的工作)
SELECT (CASE
WHEN AUDIT IN ('YES')
THEN HOURS * DAYS
END) OVER (PARTITION BY ID ,c.month) AS TOTAL
FROM TABLEA
答案 0 :(得分:0)
使用sum
作为窗口功能。
SELECT t.*,SUM(CASE WHEN AUDIT = 'YES' THEN HOURS * DAYS END)
OVER(PARTITION BY ID,month) AS TOTAL
FROM TABLEA t