如何从同一查询中的用户流量表中检索WTD,YTD,MTD用户?

时间:2016-10-05 14:58:48

标签: sql oracle aggregate-functions window-functions

在如下的用户流量表中,我想计算一周到头(WTD),月到日(MTD),年初至今(YTD)用户和返回的用户数。

测试数据:

create table user_traffic (session_id number(6), session_day date, 
                           user_id number(6), product_id number(6));

insert into user_traffic values (  1, date '2016-09-07', 101, 1);
insert into user_traffic values (  2, date '2016-09-07', 101, 4);
insert into user_traffic values (  3, date '2016-09-07', 102, 1);
insert into user_traffic values (  4, date '2016-09-08', 101, 2);
insert into user_traffic values (  5, date '2016-09-08', 101, 4);
insert into user_traffic values (  6, date '2016-09-09', 102, 1);
insert into user_traffic values (  7, date '2016-09-10', 102, 1);
insert into user_traffic values (  8, date '2016-09-10', 103, 3);
insert into user_traffic values (  9, date '2016-09-25', 104, 3);
insert into user_traffic values (  10, date '2016-10-01', 103, 1);
insert into user_traffic values (  11, date '2016-10-02', 104, 3);

预期产出: -

  Week_Start_Day, WTD_new_cnt, WTD_returned_cnt


   Month_Start_Day, MTD_new_cnt, MTD_returned_cnt


   Year_Start_Day, YTD_new_cnt, YTD_returned_cnt

评论: -

例如:在10月02日访问的上述用户流量表userid = 104中,WTD,MTD,YTD新/返回计数如下:

WTD,new,return
2016-09-26(Mon)(Week start day ), 1,0 ( For userid = 104 )

MTD,new,return
2016-09,1,1 
2016-10,0,1  

YTD,new,return
2016,0,1

我尝试了什么?

    select session_day, 
           COUNT( distinct user_id ) AS user_cnt,
           count(distinct user_id) - lag(count(distinct user_id)) 
                                     over (order by session_day) gain,
           count(newu) AS  newu, count(returnu) AS returnu
      from
         (
          select session_id,
                 session_day,
                 user_id, 
                 CASE WHEN
                 count(*) over ( partition by user_id ORDER BY       
                                 session_day,session_id ROWS   
                                   BETWEEN UNBOUNDED PRECEDING AND          
                                 CURRENT ROW
                                )
                           = 1 
                      THEN 1
                  END 
                  AS newu,
                 CASE WHEN 
                 lag( session_day,1 ) over ( partition by user_id ORDER 
                                             BY session_day,session_id   
                                            ) 
                           <> 
                 lag( session_day,1 ) over ( order by 
                                             session_day,session_id
                                            ) 
                      THEN 1
                 END  AS returnu    
            from user_traffic u
        )
  group by session_day
  order by session_day;

我在会话日级别从用户流量表计算新/返回的用户时已经构建了这个sql。

0 个答案:

没有答案