[英]Running Count Distinct using Over Partition By
我有一个数据集,其中包含随时间推移购买的用户ID。 我想显示已经进行购买的YTD不同用户数,按州和国家划分。 输出将有4列:国家,州,年,月,YTD具有购买活动的不同用户的计数。
有没有办法做到这一点? 当我从视图中排除月份并执行非常计数时,以下代码有效:
Select Year, Country, State,
COUNT(DISTINCT (CASE WHEN ActiveUserFlag > 0 THEN MBR_ID END)) AS YTD_Active_Member_Count
From MemberActivity
Where Month <= 5
Group By 1,2,3;
当用户跨越多个月进行购买时会出现此问题,因为我无法按月汇总,然后汇总,因为它会重复用户计数。
出于趋势目的,我需要查看一年中每个月的YTD计数。
在用户出现的第一个月内计算:
select Country, State, year, month,
sum(case when ActiveUserFlag > 0 and seqnum = 1 then 1 else 0 end) as YTD_Active_Member_Count
from (select ma.*,
row_number() over (partition by year order by month) as seqnum
from MemberActivity ma
) ma
where Month <= 5
group by Country, State, year, month;
在购买的第一个月内,每位会员只返回一次,按月计算,然后应用累计金额:
select Year, Country, State, month,
sum(cnt)
over (partition by Year, Country, State
order by month
rows unbounded preceding) AS YTD_Active_Member_Count
from
(
Select Year, Country, State, month,
COUNT(*) as cnt -- 1st purchses per month
From
( -- this assumes there's at least one new active member per year/month/country
-- otherwise there would be mising rows
Select *
from MemberActivity
where ActiveUserFlag > 0 -- only active members
and Month <= 5
-- and year = 2019 -- seems to be for this year only
qualify row_number() -- only first purchase per member/year
over (partition by MBR_ID, year
order by month --? probably there's a purchase_date) = 1
) as dt
group by 1,2,3,4
) as dt
;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.