ahmedrahil786

Member Retention Project - Rahil

Oct 17th, 2019
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. Select Distinct L.mid as mid,L.name as name, L.MFT_date as MFT_date, L.age as age, count(L.rid) as rid, sum(L.dur) as Dur , Round(Avg(L.dur),2) as Dur,
  2. Round(Sum(L.gross_rev),02) as Gross_rev, Round(Sum(L.net_rev),2) as net_rev, round(Avg(L.net_rev),2) as net_rev from
  3. (select A.mid as mid,A.name as name, A.rid as rid,(A.age) as age, A.dur as dur,IFNULL(E.charges,0) as gross_rev,
  4. IFNULL(F.coupon_Spent,0) as Coupon_Spent, round((IFNULL(E.charges,0) - IFNULL(F.coupon_Spent,0)),2) as net_rev, B.date as MFT_date, A.date as Resv_date1, A.date - B.date as diff from
  5. (select r.id as rid, r.member_id as mid,m.display_name as name, Date(r.start_at + interval 8 hour) as Date, round(sum(timestampdiff(minute, r.start_at, r.end_at)/60),2) as dur,(YEAR(r.start_at) - YEAR(m.birthday)) as age
  6. from reservations r left join members m on m.id = r.member_id
  7. where r.state = 'completed'
  8. and m.imaginary in ('sofam', 'normal')
  9. and r.member_id not in ('125', '127')
  10. group by 1) A
  11. left join
  12. (select distinct r.member_id as mid, min(r.id) as mrid, date(r.return_at + interval 8 hour) as date
  13. from reservations r left join members m on m.id = r.member_id
  14. where r.state = 'completed'
  15. and m.imaginary in ('sofam', 'normal')
  16. and r.member_id not in ('125', '127')
  17. group by 1) B
  18. on A.mid = B.mid
  19. left join
  20. (select c.reservation_id as rid, sum(c.amount) as charges from charges c
  21. where c.state='normal' and c.kind in ('rent','oneway','d2d','mileage')
  22. group by rid) E on A.rid = E.rid
  23. left join
  24. (select p.reservation_id as rid, month(p.created_at + interval '8' hour) as month,Year(p.created_at + interval '8' hour) as year, IFNULL(p.amount,0) as coupon_Spent from payments p
  25. where p.state = 'normal' and p.paid_type = 'coupon'
  26. group by p.reservation_id) F on A.rid = F.rid
  27. order by 1,2 asc) L
  28. where L.diff < 46
  29. group by 1 ;
  30.  
  31.  
  32. ### Breakdown by only one res , country and Type of Coupon
  33.  
  34.  
  35. Select P.mid as mid, P.mrid as min_rid,IFNULL(co.comment,"") as coupon,IFNULL(ma.country,"") as country from
  36. (Select Distinct L.mid as mid,L.mrid as mrid,L.name as name, L.MFT_date as MFT_date, L.age as age, count(L.rid) as rid, sum(L.dur) as Dur , Round(Avg(L.dur),2) as AVG_Dur,
  37. Round(Sum(L.gross_rev),02) as Gross_rev, Round(Sum(L.net_rev),2) as net_rev, round(Avg(L.net_rev),2) as AVG_net_rev from
  38. (select A.mid as mid,A.name as name, A.rid as rid, B.mrid as mrid, (A.age) as age, A.dur as dur,IFNULL(E.charges,0) as gross_rev,
  39. IFNULL(F.coupon_Spent,0) as Coupon_Spent, round((IFNULL(E.charges,0) - IFNULL(F.coupon_Spent,0)),2) as net_rev, B.date as MFT_date, A.date as Resv_date1, A.date - B.date as diff from
  40. (select r.id as rid, r.member_id as mid,m.display_name as name, Date(r.start_at + interval 8 hour) as Date, round(sum(timestampdiff(minute, r.start_at, r.end_at)/60),2) as dur,(YEAR(r.start_at) - YEAR(m.birthday)) as age
  41. from reservations r left join members m on m.id = r.member_id
  42. where r.state = 'completed'
  43. and m.imaginary in ('sofam', 'normal')
  44. and r.member_id not in ('125', '127')
  45. group by 1) A
  46. left join
  47. (select distinct r.member_id as mid, min(r.id) as mrid, date(r.return_at + interval 8 hour) as date
  48. from reservations r left join members m on m.id = r.member_id
  49. where r.state = 'completed'
  50. and m.imaginary in ('sofam', 'normal')
  51. and r.member_id not in ('125', '127')
  52. group by 1) B
  53. on A.mid = B.mid
  54. left join
  55. (select c.reservation_id as rid, sum(c.amount) as charges from charges c
  56. where c.state='normal' and c.kind in ('rent','oneway','d2d','mileage')
  57. group by rid) E on A.rid = E.rid
  58. left join
  59. (select p.reservation_id as rid, month(p.created_at + interval '8' hour) as month,Year(p.created_at + interval '8' hour) as year, IFNULL(p.amount,0) as coupon_Spent from payments p
  60. where p.state = 'normal' and p.paid_type = 'coupon'
  61. group by p.reservation_id) F on A.rid = F.rid
  62. order by 1,2 asc) L
  63. where L.diff < 46
  64. group by 1) P
  65. left join coupons co on co.reservation_id = P.mrid
  66. left join member_appendixes ma on P.mid = ma.member_id
  67. where P.rid = 1
Advertisement
Add Comment
Please, Sign In to add comment