ahmedrahil786

Becca Query - Referrals _ Edited by Rahil

May 22nd, 2019
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. set @start := '2018-12-31 00:00';
  2. set @start2 := '2019-11-1 00:00';
  3.  
  4. select
  5. A.Month as Month_2019,
  6. B.TotalReferralSignups as referralsingups_2019,
  7. D.TotalSignups as totalsignups_2019,
  8. A.MFTs as ReferralMFTs_2019,
  9. C.TotalMFTs as TotalMFT_2019,
  10. CAST((A.MFTs/B.TotalReferralSignups)*100 AS DECIMAL(18,2)) as Pct_MFT_from_referrals_Conversion,
  11. CAST((A.MFTs/C.TotalMFTs)*100 AS DECIMAL(18,2)) as Pct_MFT_to_totalMFTs,
  12. CAST((C.TotalMFTs/D.TotalSignups)*100 AS DECIMAL(18,2)) as Pct_MFT_Contribution_totalsignups
  13.  
  14. from(
  15. select
  16. MONTH(c.created_at + interval '8' hour) as Month,
  17. count(distinct case when c.kind = 'subscriptionFee' then c.member_id end) as MFTs
  18. from charges c
  19. join members m on m.id = c.member_id
  20. join member_appendixes ma on ma.member_id = m.id and ma.recommender like '%@%'
  21. where c.created_at + interval '8' hour > @start
  22. and c.created_at + interval '8' hour < @start2
  23. and m.imaginary = 'normal'
  24. group by 1) A
  25.  
  26. join (
  27. select
  28. MONTH(m.created_at + interval '8' hour) as Month,
  29. count(distinct m.id) as TotalReferralSignups
  30. from members m
  31. join member_appendixes ma
  32. on ma.member_id = m.id and ma.recommender like '%@%'
  33. where m.created_at + interval '8' hour > @start
  34. and m.created_at + interval '8' hour < @start2
  35. and m.imaginary = 'normal'
  36. group by 1) B
  37. on B.Month = A.Month
  38.  
  39. join (
  40. select
  41. MONTH(c.created_at + interval '8' hour) as Month,
  42. count(distinct case when c.kind = 'subscriptionFee' then c.member_id end) as TotalMFTs
  43. from charges c
  44. join members m
  45. on m.id = c.member_id
  46. where c.created_at + interval '8' hour > @start
  47. and c.created_at + interval '8' hour < @start2
  48. and m.imaginary = 'normal'
  49. group by 1) C
  50. on C.Month = B.Month
  51.  
  52. join (
  53. select
  54. MONTH(m.created_at + interval '8' hour) as Month,
  55. count(distinct m.id) as TotalSignups
  56. from members m
  57. where m.created_at + interval '8' hour > @start
  58. and m.created_at + interval '8' hour < @start2
  59. and m.imaginary = 'normal'
  60. group by 1) D
  61. on D.month = B.Month
  62.  
  63. ;
  64.  
  65. set @start3 := '2017-12-31 00:00';
  66. set @start4 := '2019-1-1 00:00';
  67.  
  68. select
  69. A.Month as Month_2018,
  70. B.TotalReferralSignups as referralsingups_2018,
  71. D.TotalSignups as totalsignups_2018,
  72. A.MFTs as ReferralMFTs_2018,
  73. C.TotalMFTs as TotalMFT_2018,
  74. CAST((A.MFTs/B.TotalReferralSignups)*100 AS DECIMAL(18,2)) as Pct_MFT_from_referrals_Conversion,
  75. CAST((A.MFTs/C.TotalMFTs)*100 AS DECIMAL(18,2)) as Pct_MFT_to_totalMFTs,
  76. CAST((C.TotalMFTs/D.TotalSignups)*100 AS DECIMAL(18,2)) as Pct_MFT_Contribution_totalsignups
  77.  
  78.  
  79. from(
  80. select
  81. MONTH(c.created_at + interval '8' hour) as Month,
  82. count(distinct case when c.kind = 'subscriptionFee' then c.member_id end) as MFTs
  83. from charges c
  84. join members m on m.id = c.member_id
  85. join member_appendixes ma on ma.member_id = m.id and ma.recommender like '%@%'
  86. where c.created_at + interval '8' hour > @start3
  87. and c.created_at + interval '8' hour < @start4
  88. and m.imaginary = 'normal'
  89. group by 1) A
  90.  
  91. join (
  92. select
  93. MONTH(m.created_at + interval '8' hour) as Month,
  94. count(distinct m.id) as TotalReferralSignups
  95. from members m
  96. join member_appendixes ma
  97. on ma.member_id = m.id and ma.recommender like '%@%'
  98. where m.created_at + interval '8' hour > @start3
  99. and m.created_at + interval '8' hour < @start4
  100. and m.imaginary = 'normal'
  101. group by 1) B
  102. on B.Month = A.Month
  103.  
  104. join (
  105. select
  106. MONTH(c.created_at + interval '8' hour) as Month,
  107. count(distinct case when c.kind = 'subscriptionFee' then c.member_id end) as TotalMFTs
  108. from charges c
  109. join members m
  110. on m.id = c.member_id
  111. where c.created_at + interval '8' hour > @start3
  112. and c.created_at + interval '8' hour < @start4
  113. and m.imaginary = 'normal'
  114. group by 1) C
  115. on C.Month = B.Month
  116.  
  117. join (
  118. select
  119. MONTH(m.created_at + interval '8' hour) as Month,
  120. count(distinct m.id) as TotalSignups
  121. from members m
  122. where m.created_at + interval '8' hour > @start3
  123. and m.created_at + interval '8' hour < @start4
  124. and m.imaginary = 'normal'
  125. group by 1) D
  126. on D.month = B.Month
Advertisement
Add Comment
Please, Sign In to add comment