[英]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.