条件和分区时的聚合值

时间:2018-03-30 17:30:54

标签: sql oracle

我有下表,我需要汇总

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

1 个答案:

答案 0 :(得分:0)

使用sum作为窗口功能。

SELECT t.*,SUM(CASE WHEN AUDIT = 'YES' THEN HOURS * DAYS END) 
           OVER(PARTITION BY ID,month) AS TOTAL
FROM TABLEA t