Teradata SQL EXPAND +累积

时间:2018-12-04 10:28:16

标签: sql teradata

我有入职数据,随着时间的推移计算ID。我已经扩展了数据,但我还需要积累。

我有:

ONBOARDING_DATE ONBOARDED ONBOARDED_EXPANDED
12/06/2018                          
13/06/2018                          
14/06/2018            2            2
15/06/2018                         2
17/06/2018                         2
18/06/2018            5            5
19/06/2018                         5
20/06/2018                         5

随着更多ID的加入,我需要它来积累,但找不到能够与expand结合的正确功能。

ONBOARDING_DATE ONBOARDED ONBOARDED_EXPANDED_ACCUMULATED
12/06/2018                        
13/06/2018                        
14/06/2018            2            2
15/06/2018                         2
17/06/2018                         2
18/06/2018            5            7
19/06/2018                         7
20/06/2018                         7

我的脚本

SELECT 

a.ONBOARDING_DATES
, b.ONBOARDED
, SUM(ONBOARDED) OVER ( PARTITION BY 1
                    ORDER BY ONBOARDING_DATE ASC
                    RESET WHEN ONBOARDED<>0 ) AS ONBOARDED_EXPANDED

FROM

        (
        SELECT 

        CAST(CALENDAR_DATE AS DATE) AS ONBOARDING_DATES FROM 
SYS_CALENDAR.CALENDAR

        WHERE ONBOARDING_DATES BETWEEN ADD_MONTHS(CURRENT_DATE, - 9)  AND ADD_MONTHS(CURRENT_DATE, + 9)

        ) a

LEFT JOIN
    (

            SELECT DISTINCT

            COUNT(ID) AS ONBOARDED
            , WAVE_DATE

            FROM ENV0.R_ONBOARDING

            WHERE WAVE_DATE IS NOT NULL

            GROUP BY WAVE_DATE

    ) b

    ON a.ONBOARDING_DATS = b.WAVE_DATE

    ORDER BY a.ONBOARDING_DATE

谢谢您的帮助。

2 个答案:

答案 0 :(得分:1)

您似乎只想要一个累加的总和:

select ONBOARDING_DATE, ONBOARDED,
       sum(onboarded) over (order by onboarding_date rows between unbounded preceding and current row) as ONBOARDED_EXPANDED
from t;

答案 1 :(得分:1)

您的require('react-dom/umd/react-dom.production.min.js')可以使用RESET WHENLAST_VALUE进行重写,这将在Explain中产生一个STATS步骤,而不是两个。而且,如果您想同时计算IGNORE NULLSONBOARDED_EXPANDED,也可以将它们合并为一个步骤:

ONBOARDED_EXPANDED_ACCUMULATED

相同的分区,相同的ORDER将两个计算合并为一个步骤。