ahmedrahil786

MFT_21 - SIgnups and MFTs _ Rahil

May 25th, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. set @start := '2019-1-1';
  2.  
  3. select
  4. A.week as week,
  5. D.TotalSignups,
  6. E.TotalSignups_21,
  7. A.TotalMFTs,
  8. A.mft_21,
  9. CAST((A.TotalMFTs/D.TotalSignups)*100 AS DECIMAL(18,2)) as Pct_MFT_21_from_signups,
  10. CAST((A.mft_21/E.TotalSignups_21)*100 AS DECIMAL(18,2)) as Pct_MFT_21_from_signups_21
  11.  
  12.  
  13. from (select
  14. week(c.created_at + interval '8' hour) as Week,
  15. month(c.created_at + interval '8' hour) as month,
  16. DAY(c.created_at + interval '8' hour) as Day,
  17. count(distinct case when c.kind = 'subscriptionFee' and str_to_date(m.birthday, '%Y%m%d') >= NOW() - interval '21' year then c.member_id end) as mft_21,
  18. count(distinct case when c.kind = 'subscriptionFee' then c.member_id end) as TotalMFTs
  19.  
  20. from charges c
  21. join members m
  22. on m.id = c.member_id
  23. left outer join member_appendixes ma
  24. on ma.id = m.id
  25. where c.created_at + interval '8' hour >= @start
  26. and m.imaginary = 'normal'
  27. group by 1) A
  28.  
  29. join (
  30. select
  31. week(m.created_at + interval '8' hour) as week,
  32. count(distinct m.id) as TotalSignups
  33. from members m
  34. where m.created_at + interval '8' hour > @start
  35. and m.imaginary = 'normal'
  36. group by 1) D
  37. on D.week = A.week
  38.  
  39. left join (
  40. select
  41. week(m.created_at + interval '8' hour) as week,
  42. count(distinct case when str_to_date(m.birthday, '%Y%m%d') >= NOW() - interval '21' year then m.id end) as TotalSignups_21
  43. from members m
  44. where m.created_at + interval '8' hour > @start
  45. and m.imaginary = 'normal'
  46. and str_to_date(m.birthday, '%Y%m%d') >= NOW() - interval '21' year
  47. group by 1) E
  48. on E.week = A.week
  49.  
  50. group by 1
  51. order by 1 asc
  52. ;
Advertisement
Add Comment
Please, Sign In to add comment