您好,我的公司希望更好地跟蹤有多少用戶活躍在我們的平臺上。我們使用 Microsoft SQL Server 2019 作為資料庫,連接到 Azure Data Studio。
下面是我們資料庫中的兩個表 DDL:
- 日歷表
| 柱子 | 資料型別 | 細節 |
|---|---|---|
| CALENDAR_DATE | 日期不為空 | 基準日期 (YYYY-MM-DD) |
| 公歷年 | 整數不為空 | 2010、2011等 |
| CALENDAR_MONTH_NUMBER | 整數不為空 | 1-12 |
| CALENDAR_MONTH_NAME | 變數(100) | 一月、二月等 |
| CALENDAR_DAY_OF_MONTH | 整數不為空 | 1-31 |
| CALENDAR_DAY_OF_WEEK | 整數不為空 | 1-7 |
| CALENDAR_DAY_NAME | 整數不為空 | 周一、周二等 |
| CALENDAR_YEAR_MONTH | 整數不為空, | 201011、201012、201101 等 |
- 收入分析
| 柱子 | 資料型別 | 細節 |
|---|---|---|
| 活動日期 | 日期不為空 | 下注日期 |
| 會員ID | 整數不為空 | 唯一玩家識別符號 |
| 游戲ID | SMALLINT 不為空 | 唯一游戲識別符號 |
| WAGER_AMOUNT | 實數不為空 | 游戲總投注額 |
| NUMBER_OF_WAGERS 個 | 整數不為空 | 游戲投注數 |
| 贏了_AMOUNT | 實數不為空 | 在游戲中贏得的總金額 |
| 活動_YEAR_MONTH | 整數不為空 | 年年年月 |
| BANK_TYPE_ID | 小整數默認值 0 不為空, | 0=真錢,1=獎金 |
下面兩個表的螢屏截圖:
日歷表

收入分析表

長話短說,“活躍”是指該會員在當月至少進行了一次真錢投注。
每個月,一個成員都有特定的生命周期型別。此狀態將根據他們之前和當前月份的活動每月更改一次。狀態如下:
| 新的 | 他們第一次下了真錢賭注 |
|---|---|
| 保留 | 在前一個日歷月和當前日歷月活躍 |
| 未保留 | 在前一個日歷月活躍但在當前日歷月不活躍 |
| 重新激活 | 在前一個日歷月不活躍,但在當前日歷月活躍 |
| 失效 | 在前一個日歷月或當前日歷月不活躍 |
我們最初希望獲得包含以下列的視圖:
會員編號 | CALENDAR_YEAR_MONT | MEMBER_LIFECYCLE_STATUS | LAPSED_MONTHS
此外,該視圖應顯示每個成員每月一行,從他們第一次下真錢賭注的月份開始。這個視圖應該給出他們那個月的生命周期狀態,如果成員已經失效,它應該顯示自他們上次活躍以來的月數的滾動計數。
到目前為止,我已經提出以下 CTE 來為我提供觀點基礎。但是我不確定 UNRETAINED 和 REACTIVATED 列。有什么想法嗎?
with all_activities as (
select a.member_id, activity_date, calendar_month_number as month_activity, calendar_year as year_activity,
datepart(month,CURRENT_TIMESTAMP) as current_month, datepart(year,CURRENT_TIMESTAMP) as current_year,
datepart(month,CONVERT(DATE, DATEADD(DAY,-DAY(GETDATE()),GETDATE()))) as previous_month, datepart(year,CONVERT(DATE, DATEADD(DAY,-DAY(GETDATE()),GETDATE()))) as year_last_month,
a.NUMBER_OF_WAGERS, (case when datepart(month,CURRENT_TIMESTAMP) = calendar_month_number and datepart(year,CURRENT_TIMESTAMP) = calendar_year then 'active' else 'inactive' end) as status,
case when (case when datepart(month,CURRENT_TIMESTAMP) = calendar_month_number and datepart(year,CURRENT_TIMESTAMP) = calendar_year then 'active' else 'inactive' end) = 'active' and number_of_wagers = 1 then 'New'
when (LAG((case when datepart(month,CURRENT_TIMESTAMP) = calendar_month_number and datepart(year,CURRENT_TIMESTAMP) = calendar_year then 'active' else 'inactive' end) ,1,0) OVER(PARTITION BY member_id ORDER BY calendar_month_number desc) = 'active' and calendar_month_number = datepart(month,CONVERT(DATE, DATEADD(DAY,-DAY(GETDATE()),GETDATE())))) then 'Retained'
when (calendar_month_number = datepart(month,CURRENT_TIMESTAMP) and year_activity = datepart(year,CURRENT_TIMESTAMP) and calendar_month_number = datepart(month,CONVERT(DATE, DATEADD(DAY,-DAY(GETDATE()),GETDATE())))) then 'Unretained'
from [dbo].[REVENUE_ANALYSIS] a
join CALENDAR b on a.ACTIVITY_DATE= b.CALENDAR_DATE
)
select * from all_activities
uj5u.com熱心網友回復:
這是關于客戶生命周期狀態分析,這需要做幾件事:
- 客戶獲取日期(最好存盤此日期,因為有些客戶可能會追溯到幾年或幾十年)。對于這個問題,我們假設
revenue_analysis有我們需要的一切并計算user acquisition month lapsedvschurned:churned客戶通常被定義為在一段時間內沒有活動。對于這個問題,我們沒有定義,因此,用戶將被報告為lapsed永遠。- 對于生命周期狀態計算,我們將收集以下資訊(member_id、calendar_month、acquisition_month、activity_month、prior_activity_month),以便計算最終結果。
with cte_new_user_monthly as (
select member_id,
min(activity_year_month) as acquisition_month
from revenue_analysis
group by 1),
cte_user_monthly as (
select u.member_id,
u.acquisition_month,
m.yyyymm as calendar_month
from cte_new_user_monthly u,
calendar_month m
where u.acquisition_month <= m.yyyymm),
cte_user_activity_monthly as (
select f.member_id,
f.activity_year_month as activity_month
from revenue_analysis f
group by 1,2),
cte_user_lifecycle as (
select u.member_id,
u.calendar_month,
u.acquisition_month,
m.activity_month
from cte_user_monthly u
left
join cte_user_activity_monthly m
on u.member_id = m.member_id
and u.calendar_month = m.activity_month),
cte_user_status as (
select member_id,
calendar_month,
acquisition_month,
activity_month,
lag(activity_month,1) over (partition by member_id order by calendar_month) as prior_activity_month
from cte_user_lifecycle),
user_status_monthly as (
select member_id,
calendar_month,
activity_month,
case
when calendar_month = acquisition_month then 'NEW'
when prior_activity_month is not null and activity_month is not null then 'RETAINED'
when prior_activity_month is not null and activity_month is null then 'UNRETAINED'
when prior_activity_month is null and activity_month is not null then 'REACTIVATED'
when prior_activity_month is null and activity_month is null then 'LAPSED'
else null
end as user_status
from cte_user_status)
select member_id,
calendar_month,
activity_month,
user_status,
row_number() over (partition by member_id, user_status order by calendar_month) as months
from user_status_monthly
order by 1,2;
結果(包括activity_month以便于理解):
member_id|calendar_month|activity_month|user_status|months|
--------- -------------- -------------- ----------- ------
1001| 201701| 201701|NEW | 1|
1001| 201702| |UNRETAINED | 1|
1001| 201703| |LAPSED | 1|
1001| 201704| |LAPSED | 2|
1001| 201705| 201705|REACTIVATED| 1|
1001| 201706| 201706|RETAINED | 1|
1001| 201707| |UNRETAINED | 2|
1001| 201708| |LAPSED | 3|
1001| 201709| 201709|REACTIVATED| 2|
1001| 201710| |UNRETAINED | 3|
1001| 201711| |LAPSED | 4|
1001| 201712| 201712|REACTIVATED| 3|
1002| 201703| 201703|NEW | 1|
1002| 201704| |UNRETAINED | 1|
1002| 201705| |LAPSED | 1|
1002| 201706| |LAPSED | 2|
1002| 201707| |LAPSED | 3|
1002| 201708| |LAPSED | 4|
1002| 201709| |LAPSED | 5|
1002| 201710| |LAPSED | 6|
1002| 201711| |LAPSED | 7|
1002| 201712| |LAPSED | 8|
編輯:
- 在 MySQL 中測驗的代碼,因為我沒有注意到洗掉了“mysql”標簽。
calendar_month在代碼中可以從calendar維度派生。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/537428.html
標籤:数据库sql服务器数据库
下一篇:更新groupby列的答案
