Advertisement
Guest User

Untitled

a guest
Nov 13th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. --drop table temp1
  3. CREATE TEMP TABLE temp1 AS
  4. select to_char( dateFirst,'YYYY-MM') as startMonth, (cast(date_part('day',age(DateNoTime,dateFirst)) as int))/7 as Intervall,
  5. count(distinct id_for_vendor) as amountofUsers
  6. From
  7. (select Primery.*,PlayerFirst.DateFirst
  8. FROM
  9. (SELECT distinct T_Pr.id_for_vendor,date(T_Pr.device_timestamp) as DateNoTime
  10.  
  11. FROM
  12.  
  13. /*add Row number column by user and date for finding the first usage date*/
  14. (SELECT T.*, ROW_NUMBER() OVER(PARTITION BY T.id_for_vendor ,date(T.device_timestamp) ORDER BY T.device_timestamp) AS ROWNUMBER
  15. FROM public.Facetune2_usage_app_background  AS T) AS T_Pr
  16. WHERE T_Pr.ROWNUMBER=1) as Primery inner join
  17. /* */
  18. (SELECT T_Pr.*,date(T_Pr.device_timestamp) as DateFirst
  19.  
  20. FROM
  21.  
  22. /*add Row number column by user and date for finding the first usage date*/
  23. (SELECT T.*, ROW_NUMBER() OVER(PARTITION BY T.id_for_vendor  ORDER BY T.device_timestamp) AS ROWNUMBER
  24. FROM public.Facetune2_usage_app_background  AS T) AS T_Pr
  25. WHERE T_Pr.ROWNUMBER=1
  26. ) as PlayerFirst on (Primery.id_for_vendor=PlayerFirst.id_for_vendor)) as Y
  27. group by  to_char( dateFirst,'YYYY-MM') , (cast(date_part('day',age(DateNoTime,dateFirst)) as int))/7;
  28.  
  29. SELECT
  30.     D.startmonth,
  31.     max(case when intervall = '0'  then retentionRation end) AS inter0,
  32.    max(case when intervall = '1'  then retentionRation end) AS inter1,
  33.    max(case when intervall = '2'  then retentionRation end) AS inter2,
  34.    max(case when intervall = '3'  then retentionRation end) AS inter3,
  35.    max(case when intervall = '4'  then retentionRation end) AS inter4,
  36.    max(case when intervall = '5'  then retentionRation end) AS inter5,
  37.    max(case when intervall = '6'  then retentionRation end) AS inter6,
  38.    max(case when intervall = '7'  then retentionRation end) AS inter7,
  39.    max(case when intervall = '8'  then retentionRation end) AS inter8,
  40.    max(case when intervall = '9'  then retentionRation end) AS inter9,
  41.    max(case when intervall = '10'  then retentionRation end) AS inter10,
  42.    max(case when intervall = '11'  then retentionRation end) AS inter11,
  43.     max(case when intervall = '12'  then retentionRation end) AS inter12,
  44.    max(case when intervall = '13'  then retentionRation end) AS inter13,
  45.     max(case when intervall = '14'  then retentionRation end) AS inter14,
  46.    max(case when intervall = '15'  then retentionRation end) AS inter15,
  47.     max(case when intervall = '16'  then retentionRation end) AS inter16,
  48.    max(case when intervall = '17'  then retentionRation end) AS inter17,
  49.     max(case when intervall = '18'  then retentionRation end) AS inter18,
  50.    max(case when intervall = '19'  then retentionRation end) AS inter19,
  51.     max(case when intervall = '20'  then retentionRation end) AS inter20,
  52.        max(case when intervall = '21'  then retentionRation end) AS inter21,
  53.     max(case when intervall = '22'  then retentionRation end) AS inter22,
  54.    max(case when intervall = '23'  then retentionRation end) AS inter23,
  55.     max(case when intervall = '24'  then retentionRation end) AS inter24,
  56.    max(case when intervall = '25'  then retentionRation end) AS inter25,
  57.     max(case when intervall = '26'  then retentionRation end) AS inter26,
  58.    max(case when intervall = '27'  then retentionRation end) AS inter27,
  59.     max(case when intervall = '28'  then retentionRation end) AS inter28,
  60.    max(case when intervall = '29'  then retentionRation end) AS inter29
  61.    
  62.     from
  63. (select t1.startmonth,t1.intervall,(cast(t1.amountofusers as float)/cast(t2.amountofusers as float))*100 as retentionRation
  64. from temp1 as t1 inner join (select startmonth,amountofusers from temp1 where intervall=0 )
  65. as t2 on (t1.startmonth=t2.startmonth)) as D
  66. group by startmonth
  67. order by startmonth
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement