【问题标题】:Current date and past 30 days total value当前日期和过去 30 天的总值
【发布时间】:2019-12-05 21:39:31
【问题描述】:

我目前拥有的代码显示了每天的当前值并给出了运行总数。

select t.lgl_entity_nm, d.date_key, 
count(distinct s.site_key) AS Dis, 
SUM(Dis) over (partition by t.lgl_entity_nm order by d.date_key ASC rows unbounded preceding) RunningTotal
from site_v s
join touchpoint_v t
on s.site_key = t.site_key
join omni_promo_varnt_fact_v o
on o.touchpoint_key = t.touchpoint_key
join date_v d
on d.date_key = o.date_key
where d.date_key between 20190901 and 20190931
and t.lgl_entity_nbr = 1
and tot_selected_qty > 0
and event_typ_cd in ('IS-SPRINT-T', 'IS-PRINT-T')
group by 1,2

给我这个输出:

lgl_entity_nm   date_key    dis runningtotal
Ahold USA      20190901     729     729
Ahold USA      20190902     733     1462

如果您查看它在某个时间段之间设置的日期。我想要实现的是它在一行中向我显示当前日期或任何设置的日期值以及过去 30 天的总数。假设日期是2019-09-30:

lgl_entity_nm   date_key(current date)  dis    total (past30 days)
Ahold USA       20190930                739     21953

这可以实现吗?如果有怎么办?

【问题讨论】:

    标签: sql netezza cumulative-sum aginity


    【解决方案1】:

    试试这个?

    DECLARE @CurrentDate    DATE = GETDATE()
            ,@MonthBack     DATE = DATEADD(DAY,-30,GETDATE())
    
    SELECT  t.lgl_entity_nm
            ,@CurrentDate AS CurrentDate
            ,COUNT(DISTINCT s.site_key) AS Dis
            ,SUM(Dis) AS RunningTotal
    FROM    site_v AS s
            JOIN touchpoint_v AS t ON s.site_key = t.site_key
            JOIN omni_promo_varnt_fact_v AS o ON o.touchpoint_key = t.touchpoint_key
            JOIN date_v AS d ON d.date_key = o.date_key
    WHERE   d.date_key BETWEEN @MonthBack AND @CurrentDate
            AND t.lgl_entity_nbr = 1
            AND tot_selected_qty > 0
            AND event_typ_cd IN ('IS-SPRINT-T', 'IS-PRINT-T')
    GROUP BY
            t.lgl_entity_nm
    

    【讨论】:

    • 我看到你的逻辑可以完成这项工作。但是在 netezza 上工作,我认为我不能使用 @declare 选项。可能必须找到解决方法。
    • @darkpunk 他的脚本初始化了变量。也许在suery中使用它?还是 date_key 实际上是一个数字?
    • 不知道 Netezza。但是根据herehere 应该可以使用to_char(current_date,'YYYYMMDD') 之类的东西。或者也许那应该仍然是CAST 到一个数字。
    • 嘿@LukStorms 我会看看我是否可以使用 CAST 或 TO_CHAR 使其工作。
    【解决方案2】:

    我认为您可以从聚合中删除日期键:

    select t.lgl_entity_nm, max(d.date_key), 
           count(distinct s.site_key) as as RunningTotal
    from site_v s join
         touchpoint_v t
         on s.site_key = t.site_key join
         omni_promo_varnt_fact_v o join
         on o.touchpoint_key = t.touchpoint_key join
         date_v d
         on d.date_key = o.date_key
    where d.date_key between 20190901 and 20190931 and
          t.lgl_entity_nbr = 1 and
          tot_selected_qty > 0 and
          event_typ_cd in ('IS-SPRINT-T', 'IS-PRINT-T')
    group by 1;
    

    实际上,这与您的查询略有不同,因为您不是在整个期间计算不同的 site_key,而是将每天的不同计数相加。为此:

    count(distinct d.date_key || ':' || s.site_key) as RunningTotal
    

    【讨论】:

    • 这确实给了我一个运行总数,我想找到一种方法来使日期有效,就像当前和过去 30 一样。
    猜你喜欢
    • 2016-05-17
    • 2020-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多