ahmedrahil786

High Spent + Loyal + Festive Inactive = 3 Queries

Dec 18th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.55 KB | None | 0 0
  1. ############# High Spent users
  2.  
  3.  
  4. select P.mid as mid , P.last_active_date as last_active_date, IFNULL(E.charges,0) as gross_rev,
  5. IFNULL(F.coupon_Spent,0) as Coupon_Spent,
  6. round((IFNULL(E.charges,0) - IFNULL(F.coupon_Spent,0)),2) as net_rev, Q.resv as resv ,
  7. Round(round((IFNULL(E.charges,0) - IFNULL(F.coupon_Spent,0)),2) /Q.resv,2) as ABS from
  8. (Select A.mid as mid, A.date as last_active_date from
  9. (select
  10. distinct r.member_id as mid,
  11. MAX(date(r.return_at + interval '8' hour)) as date,
  12. count(r.id) as resv
  13. from reservations r
  14. left join members m on m.id = r.member_id
  15. where r.return_at + interval '8' hour >= date_sub(CURDATE(), interval 90 day)
  16. and m.imaginary in ('sofam', 'normal')
  17. and r.member_id not in ('125', '127')
  18. group by 1
  19. order by 1 asc) A
  20. where A.date <= date_sub(CURDATE(), interval 15 day)) P
  21. left join
  22. (select c.member_id as mid, sum(c.amount) as charges from charges c
  23. where c.state='normal' and c.kind in ('rent','oneway','d2d','mileage','insurance')
  24. group by mid) E on P.mid = E.mid
  25. left join
  26. (select p.member_id as mid, IFNULL(p.amount,0) as coupon_Spent from payments p
  27. where p.state = 'normal' and p.paid_type = 'coupon'
  28. group by p.member_id) F on P.mid = F.mid
  29. left join
  30. (select
  31. distinct r.member_id as mid,
  32. count(r.id) as resv
  33. from reservations r
  34. left join members m on m.id = r.member_id
  35. where m.imaginary in ('sofam', 'normal')
  36. and r.member_id not in ('125', '127')
  37. group by 1
  38. order by 1 asc) Q
  39. on Q.mid = P.mid
  40. where Q.resv >= 3
  41. and Round(round((IFNULL(E.charges,0) - IFNULL(F.coupon_Spent,0)),2) /Q.resv,2) > 60
  42.  
  43. ############# Loyal Inactive members
  44.  
  45.  
  46. set @start := '2019-10-01';
  47. select P.mid as mid , P.last_active_date as last_active_date, IFNULL(E.charges,0) as gross_rev,
  48. IFNULL(F.coupon_Spent,0) as Coupon_Spent,
  49. round((IFNULL(E.charges,0) - IFNULL(F.coupon_Spent,0)),2) as net_rev, Q.resv as resv ,
  50. Round(round((IFNULL(E.charges,0) - IFNULL(F.coupon_Spent,0)),2) /Q.resv,2) as ABS from
  51. (Select A.mid as mid, A.date as last_active_date from
  52. (select distinct c.member_id as mid from coupons c
  53. where c.coupon_policy_id in ('314','315', '316','317')) B
  54. left join
  55. (select
  56. distinct r.member_id as mid,
  57. MAX(date(r.return_at + interval '8' hour)) as date,
  58. count(r.id) as resv
  59. from reservations r
  60. left join members m on m.id = r.member_id
  61. where r.return_at + interval '8' hour >= date_sub(CURDATE(), interval 180 day)
  62. and m.imaginary in ('sofam', 'normal')
  63. and r.member_id not in ('125', '127')
  64. group by 1
  65. order by 1 asc) A
  66. on A.mid = B.mid
  67. where A.date <= date_sub(CURDATE(), interval 15 day)) P
  68. left join
  69. (select c.member_id as mid, sum(c.amount) as charges from charges c
  70. where c.state='normal' and c.kind in ('rent','oneway','d2d','mileage','insurance')
  71. group by mid) E on P.mid = E.mid
  72. left join
  73. (select p.member_id as mid, IFNULL(p.amount,0) as coupon_Spent from payments p
  74. where p.state = 'normal' and p.paid_type = 'coupon'
  75. group by p.member_id) F on P.mid = F.mid
  76. left join
  77. (select
  78. distinct r.member_id as mid,
  79. count(r.id) as resv
  80. from reservations r
  81. left join members m on m.id = r.member_id
  82. where m.imaginary in ('sofam', 'normal')
  83. and r.member_id not in ('125', '127')
  84. group by 1
  85. order by 1 asc) Q
  86. on Q.mid = P.mid
  87. where Q.resv >= 3
  88. and Round(round((IFNULL(E.charges,0) - IFNULL(F.coupon_Spent,0)),2) /Q.resv,2) > 60
  89.  
  90. ############# Festive users
  91.  
  92. select P.mid as mid , P.last_active_date as last_active_date, IFNULL(E.charges,0) as gross_rev,
  93. IFNULL(F.coupon_Spent,0) as Coupon_Spent,
  94. round((IFNULL(E.charges,0) - IFNULL(F.coupon_Spent,0)),2) as net_rev, Q.resv as resv ,
  95. Round(round((IFNULL(E.charges,0) - IFNULL(F.coupon_Spent,0)),2) /Q.resv,2) as ABS from
  96. (Select A.mid as mid, A.date as last_active_date from
  97. (select
  98. distinct r.member_id as mid,
  99. MAX(date(r.return_at + interval '8' hour)) as date,
  100. count(r.id) as resv
  101. from reservations r
  102. left join members m on m.id = r.member_id
  103. where date(r.return_at + interval '8' hour) in ('2018-12-21','2018-12-22','2018-12-23','2018-12-24','2018-12-25','2018-12-26','2018-12-27','2018-12-28',
  104. '2018-12-29','2018-12-30','2018-12-31','2019-01-01','2019-01-02','2019-01-03','2019-01-31','2019-02-01','2019-02-02','2019-02-03','2019-02-04','2019-02-05',
  105. '2019-02-06','2019-02-07','2019-02-08','2019-02-09','2019-02-10','2019-02-11','2019-02-12','2019-02-13','2019-06-01','2019-06-02','2019-06-03','2019-06-04','2019-06-05','2019-06-06',
  106. '2019-06-07','2019-06-08','2019-06-09','2019-06-10','2019-08-10','2019-08-11','2019-08-13','2019-08-27','2019-08-28','2019-08-29','2019-08-30','2019-08-31','2019-09-01','
  107. 2019-09-07','2019-09-08','2019-09-09','2019-09-10','2019-09-16','2019-09-17','2019-09-18','2019-09-19','2019-09-26','2019-09-27','2019-09-28','2019-09-29')
  108. and m.imaginary in ('sofam', 'normal')
  109. and r.member_id not in ('125', '127')
  110. group by 1
  111. order by 1 asc) A
  112. where A.date <= date_sub(CURDATE(), interval 15 day)) P
  113. left join
  114. (select c.member_id as mid, sum(c.amount) as charges from charges c
  115. where c.state='normal' and c.kind in ('rent','oneway','d2d','mileage','insurance')
  116. group by mid) E on P.mid = E.mid
  117. left join
  118. (select p.member_id as mid, IFNULL(p.amount,0) as coupon_Spent from payments p
  119. where p.state = 'normal' and p.paid_type = 'coupon'
  120. group by p.member_id) F on P.mid = F.mid
  121. left join
  122. (select
  123. distinct r.member_id as mid,
  124. count(r.id) as resv
  125. from reservations r
  126. left join members m on m.id = r.member_id
  127. where m.imaginary in ('sofam', 'normal')
  128. and r.member_id not in ('125', '127')
  129. group by 1
  130. order by 1 asc) Q
  131. on Q.mid = P.mid
  132. where Q.resv >= 3
  133. and Round(round((IFNULL(E.charges,0) - IFNULL(F.coupon_Spent,0)),2) /Q.resv,2) > 60
Advertisement
Add Comment
Please, Sign In to add comment