簡體   English   中英

SQL 我們如何從 2 個單獨的列 start_date 和 end_date 中獲取月度趨勢?

[英]SQL how can we get monthly trend from 2 separated columns start_date and end_date?

假設我們有以下數據:

ID,State,start_date,end_date,Product

S1,Trial,2020/01/01,2020/01/07,Hulu
S1,Paid,2020/01/08,2020/01/31,Hulu
S1,Expired,2020/02/01,null,Hulu
S1,Paid,2020/03/01,2020/03/30,Hulu
S2,Paid,2020/01/08,2020/01/31,Hulu
S3,Paid,2020/01/09,2020/02/01,Hulu

create table test
  (
  ID varchar(10),
  State varchar(10),
  start_date datetime,
  end_date datetime,
  Product varchar(10)
  );

 insert into test 
 VALUES 
('S1','Trial','2020-01-01','2020-01-07','Hulu'),
('S1','Paid','2020-01-08','2020-01-31','Hulu'),
('S1','Expired','2020-02-01',null,'Hulu'),
('S1','Paid','2020-03-01','2020-03-30','Hulu'),
('S2','Paid','2020-01-08','2020-01-31','Hulu'),
('S3','Paid','2020-01-09','2020-02-01','Hulu')
 ;

這里的問題是獲取 2020 年活躍付費訂閱的月度趨勢。 對於每個訂閱者 (ID),我們只能計算他們活躍的月份。 因此,對於 S1,我們只能計算 2020 年 1 月和 2020 年 3 月活躍的 S1,而不是 2020 年 2 月。

在采訪中,我寫了一個 function 並說我們可以循環調用這個 function 為 2020 年的每個月

def month_active_sub($yyyymm):
   select 
   $yyyymm as month,
   count(distinct ID)
   from table where end_date >= $yyyymm and start_date <= $yyyymm and state='paid';

或者

   select 
   '202001' as month,
   count(distinct ID)
   from table where end_date >= '202001' and start_date <= '202001' and state='paid'

   union all 

   select 
   '202002' as month,
   count(distinct ID)
   from table where end_date >= '202002' and start_date <= '202002' and state='paid'

   union all for another 10 months

我想知道是否有更好的方法來編寫這個 SQL 查詢? 謝謝!

一種方法使用數字表:

select '2020-01-01' + interval n.n month start_of_month, count(t.id) no_active_subscribers
from (
    select 0 n 
    union all select 1
    union all select 2 
    ... 
    union all select 11
) n
left join mytable t 
    on  t.start_date >= '2020-01-01' + interval n.n month
    and t.end_date   <= '2020-01-01' + interval (n.n + 1) month
    and t.state = 'paid'
group by n.n

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM