ahmedrahil786

Source of MFTs - Trail

Jul 17th, 2019
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. set @startdate1 = '2019-1-1 00:00';
  2. select
  3. A.week as week,
  4. F.MFTs as total_MFTs,
  5. count(B.mid) as MFT_coupons,
  6. count(C.mid) as MFT_Referral_plus_Coupon,
  7. E.MFTs as Referral_MFT,
  8. (F.MFTs)-((count(B.mid)) + E.MFTs - (count(C.mid))) as No_coupon_Referral
  9. from
  10. (select
  11. ch.member_id as mid,
  12. weekofyear(ch.created_at + interval '8' hour) as week
  13. from charges ch
  14. where ch.created_at + interval '8' hour >= @startdate1
  15. and ch.kind = 'subscriptionFee') A
  16. join
  17. (select
  18. distinct c.member_id as mid,
  19. min(r.id) as fb,
  20. weekofyear(r.start_at + interval '8' hour) as week,
  21. c.coupon_policy_id as cpid,
  22. c.comment
  23. from coupons c
  24. join reservations r on c.reservation_id = r.id
  25. where c.used_at is not null
  26. and r.state = 'completed'
  27. and r.start_at + interval 8 hour > @startdate1
  28. group by 1
  29. order by 1 asc) B
  30. on B.mid = A.mid and B.week = A.week
  31. left join
  32. (select
  33. distinct c.member_id as mid,
  34. min(r.id) as fb,
  35. weekofyear(r.start_at + interval '8' hour) as week,
  36. c.coupon_policy_id as cpid,
  37. c.comment
  38. from coupons c
  39. join member_appendixes ma on ma.member_id = c.member_id
  40. join reservations r on c.reservation_id = r.id
  41. where c.used_at is not null
  42. and r.state = 'completed'
  43. and r.start_at + interval 8 hour > @startdate1
  44. and ma.recommender like '%@%'
  45. group by 1
  46. order by 1 asc) C
  47. on C.mid = A.mid and C.week = A.week
  48. left join
  49. (select
  50. Weekofyear(c.created_at + interval '8' hour) as Week,
  51. count(distinct case when c.kind = 'subscriptionFee' then c.member_id end) as MFTs
  52. from charges c
  53. join members m on m.id = c.member_id
  54. join member_appendixes ma on ma.member_id = m.id and ma.recommender like '%@%'
  55. where c.created_at + interval '8' hour > @startdate1
  56. and m.imaginary = 'normal'
  57. group by 1
  58. order by 1 desc) E
  59. on E.week = A.week
  60. left join
  61. (select
  62. Weekofyear(c.created_at + interval '8' hour) as Week,
  63. count(distinct case when c.kind = 'subscriptionFee' then c.member_id end) as MFTs
  64. from charges c
  65. where c.created_at + interval '8' hour > @startdate1
  66. group by 1) F
  67. on F.week = A.week
  68. group by A.week
  69. order by A.week Desc
Advertisement
Add Comment
Please, Sign In to add comment