我有一个包含用户ID的数据集,这些用户ID随着时间的推移进行了购买。我想显示按年和年初至今已进行购买的用户数量,按州和国家/地区划分。输出将具有4列:国家,州,年,月,具有购买活动的不同用户的年初至今计数。
有没有办法做到这一点?当我从视图中排除月份并进行独立计数时,以下代码有效:
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计数。
答案 0 :(得分:0)
计算出现的第一个月的用户:
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;
答案 1 :(得分:0)
每个会员在购买的第一个月仅退货一次,按月计数,然后应用累计金额:
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
;