每日sql-復(fù)購率問題count+case+timediff類函數(shù)

強推良心公眾號:猴子數(shù)據(jù)分析
資料來源:
如何分析?復(fù)購用戶?
人均付費如何分析?

今日要點:

  • timestampdiff(返回的時間格式,起始時間,結(jié)束時間)
  • case when ...then...else...end
  • count(case ……)
  • count(distinct)
image.png
select a.購買時間,
count(distinct a.用戶id) 當(dāng)日首次購買用戶數(shù),
count(distinct case when timestampdiff(month,a.購買時間,b.購買時間) <=1 
      then a.用戶id  else null end ) as 此月復(fù)購用戶數(shù),
count(distinct case when timestampdiff(month,a.購買時間,b.購買時間) =3 
      then a.用戶id  else null end ) as 第三月復(fù)購用戶數(shù),
count(distinct case when timestampdiff(month,a.購買時間,b.購買時間) =4
      then a.用戶id  else null end ) as 第四月復(fù)購用戶數(shù),
count(distinct case when timestampdiff(month,a.購買時間,b.購買時間) =5
      then a.用戶id  else null end ) as 第五月復(fù)購用戶數(shù),
count(distinct case when timestampdiff(month,a.購買時間,b.購買時間) =20
      then a.用戶id  else null end ) as 第二十月復(fù)購用戶數(shù)
from 課程訂單表 as a
left join 課程訂單表 as b
on a.`用戶id` = b.`用戶id`
where a.課程類型=2 and a.購買時間!=b.購買時間
group by a.購買時間;

timestampdiff與timediff的區(qū)別,前者可以返回時分秒的差,后者只能返回相差的天數(shù)


image.png

1、各地用戶數(shù)(以后看到這個題目就要想到去重呀)

select 城市,count(distinct 用戶id),sum(ARPU值)
from 各城市用戶ARPU值
group by 城市

2、各城市各分段用戶數(shù)是多少

select 
count(distinct case when ARPU值>0 and ARPU值<30 then 1 else null end )as '(0-30)'
count(distinct case when ARPU值>=30 and ARPU值<50 then 1 else null end )as '[30-50)'
count(distinct case when ARPU值>=50 and ARPU值<80 then 1 else null end )as '[50-80)'
count(distinct case when ARPU值>=80  then 1 else null end )as '[80以上)'
from 各城市用戶ARPU值
group by 城市

3、找出表2中重復(fù)的用戶數(shù)

select 用戶id
from 用戶套餐費用表
group by 用戶id
having count(用戶id)>2

4.mysql不支持全連接,left+union+right


image.png

image.png

一句SQL取出所有用戶對商品的行為特征,特征分為已購買、購買未收藏、收藏未購買、收藏且購買(輸出結(jié)果如下表)


image.png
select o.user_id,o.item_id,
(case when o.pay_time is not null then 1 else null end) as 已購
(case when o.pay_time is not null  and f.fav_time is null then 1 else null end)as 購買未收藏
(CASE when o.pay_time is null and f.fav_time is not null then 1 else 0 end) as  收藏未購買,
(CASE when o.pay_time is not null and f.fav_time is not null then 1 else 0 end) as 收藏且購買
from orders o
left join favourites f 
on o.user_id = f.user_id and o.item_id = f.item_id
UNION
SELECT
f.user_id,f.item_id,
(CASE when o.pay_time is not null then 1 else 0 end) as '已購買',
(CASE when o.pay_time is not null and f.fav_time is null then 1 else 0 end) as '購買未收藏',
(CASE when o.pay_time is null and f.fav_time is not null then 1 else 0 end) as '收藏未購買',
(CASE when o.pay_time is not null and f.fav_time is not null then 1 else 0 end) as '收藏且購買'
FROM orders o 
RIGHT JOIN favorites f 
ON o.user_id = f.user_id 
AND o.item_id = f.item_id
ORDER BY user_id, item_id;

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1.軟件測試級別? 單元測試:單元測試是對軟件組成單元進行測試。其目的是檢驗軟件基本組成單位的正確性。測試的對象是...
    聽聞白依閱讀 1,620評論 0 9
  • 最近又重新復(fù)習(xí)了一遍SQL的基礎(chǔ)和進階知識,SQL可以很基礎(chǔ)也可以很復(fù)雜,寫一篇總結(jié)來梳理自己的知識脈絡(luò) SQL是...
    元寶2020閱讀 491評論 0 0
  • 1、應(yīng)用擴展(Extension)這是一個千呼萬喚始出來的特性,也是一個可以發(fā)揮無限想象力的特性?,F(xiàn)在Apple允...
    Trigger_o閱讀 2,144評論 0 1
  • 漸變的面目拼圖要我怎么拼? 我是疲乏了還是投降了? 不是不允許自己墜落, 我沒有滴水不進的保護膜。 就是害怕變得面...
    悶熱當(dāng)乘涼閱讀 4,462評論 0 13
  • 感覺自己有點神經(jīng)衰弱,總是覺得手機響了;屋外有人走過;每次媽媽不聲不響的進房間突然跟我說話,我都會被嚇得半死!一整...
    章魚的擁抱閱讀 2,364評論 4 5

友情鏈接更多精彩內(nèi)容