嗨大家我的查询中有一个数据集,如下所示:
Date | Count | Activity
10 Nov | 10 | A
11 Nov | 11 | A
10 Nov | 12 | B
11 Nov | 13 | B
我正在努力实现这一结果。基本上逻辑是第2行将减去第1行,第4行减去第3行。
Date | Count | Activity | Diff
10 Nov | 10 | A | 0
11 Nov | 11 | A | 1
10 Nov | 12 | B | 0
11 Nov | 13 | B | 1
我当前的查询如下所示:
select DATE, count(distinct(ID)) as Count,
count(distinct(ID)) - LAG(count(distinct(ID)),1) over (order by count(distinct(ID))) as Eng_change
from (Select DATA.*,PRODUCT.MAPPING from DATA left join PRODUCT on DATA.Part_Number=PRODUCT.PRODUCT_NUMBER ) OVERALLFUNNEL
WHERE ACTIVITY_RANK>5
group by OVERALLFUNNEL.ACTIVITY,OVERALLFUNNEL.DATE
ORDER BY ACTIVITY_RANK ASC
使用滞后将总是减去上一行,但这不是我想要的。
任何帮助或功能?
问候
答案 0 :(得分:0)
如果我正确理解您的问题,您希望每项活动都有所不同。如果是这样的话:
select DATE, activity,
count(distinct ID) as Count,
(count(distinct ID) -
LAG(count(distinct ID), 1) over (partition by activity
order by count(distinct ID)
)
) as Eng_change
这将为NULL
提供第一个值。如果您需要0
,请使用coalesce()
或类似内容。