ahmedrahil786

2,3,4 Bookings in 14 days - Rahil

May 13th, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.27 KB | None | 0 0
  1. ################WAU, Number of customers with 2 , 3 , 4 bookings in a week (Group by date)
  2.  
  3. set @start1 := '2020-02-01';
  4. use socar_malaysia;
  5.  
  6.  
  7. select
  8. P.min_date as min_date,
  9. count(distinct case when P.rid = 1 then P.mid end) as 1_Booking,
  10. count(distinct case when P.rid = 2 then P.mid end) as 2_Booking,
  11. count(distinct case when P.rid = 3 then P.mid end) as 3_Booking,
  12. count(distinct case when P.rid = 4 then P.mid end) as 4_Booking,
  13. count(distinct case when P.rid = 5 then P.mid end) as 5_Booking,
  14. count(distinct case when P.rid > 5 then P.mid end) as More_than_6_Booking
  15.  
  16. from
  17. (Select Distinct L.mid as mid,L.name as name, L.min_date as min_date, L.age as age, count(L.rid) as rid, sum(L.dur) as Dur , Round(Avg(L.dur),2) as avg_Dur,
  18. 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
  19. (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,
  20. 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 min_date, A.date as Resv_date1, A.date - B.date as diff from
  21. (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
  22. from reservations r left join members m on m.id = r.member_id
  23. where r.state = 'completed'
  24. and m.imaginary in ('sofam', 'normal')
  25. and r.member_id not in ('125', '127')
  26. group by 1) A
  27. left join
  28. ################## To find WAU, Please enter start date and it will calculate 7 days from there
  29. (select distinct r.member_id as mid, min(r.id) as mrid,
  30. date(r.return_at + interval 8 hour) as date
  31. from reservations r left join members m on m.id = r.member_id
  32. where r.state = 'completed'
  33. and r.return_at + interval 8 hour >= @start1
  34. and r.return_at + interval 8 hour <= date_add(@start1, interval 7 day)
  35. and m.imaginary in ('sofam', 'normal')
  36. and r.member_id not in ('125', '127')
  37. group by 1) B
  38. on A.mid = B.mid
  39. ############## Total spent
  40. left join
  41. (select c.reservation_id as rid, sum(c.amount) as charges from charges c
  42. where c.state='normal' and c.kind in ('rent','oneway','d2d','mileage')
  43. group by rid) E on A.rid = E.rid
  44. left join
  45. (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
  46. where p.state = 'normal' and p.paid_type = 'coupon'
  47. group by p.reservation_id) F on A.rid = F.rid
  48. order by 1,2 asc) L
  49. where L.diff < 15
  50. and L.diff > 0
  51. group by 1) P
  52. group by 1;
  53.  
  54. ################WAU, Number of customers with 2 , 3 , 4 bookings in a week (No Grouping by date )
  55.  
  56. set @start1 := '2020-01-01';
  57. use socar_malaysia;
  58.  
  59.  
  60. select
  61. count(distinct case when P.rid = 1 then P.mid end) as 1_Booking,
  62. count(distinct case when P.rid = 2 then P.mid end) as 2_Booking,
  63. count(distinct case when P.rid = 3 then P.mid end) as 3_Booking,
  64. count(distinct case when P.rid = 4 then P.mid end) as 4_Booking,
  65. count(distinct case when P.rid = 5 then P.mid end) as 5_Booking,
  66. count(distinct case when P.rid > 5 then P.mid end) as More_than_6_Booking
  67.  
  68. from
  69. (Select Distinct L.mid as mid,L.name as name, L.min_date as min_date, L.age as age, count(L.rid) as rid, sum(L.dur) as Dur , Round(Avg(L.dur),2) as avg_Dur,
  70. 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
  71. (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,
  72. 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 min_date, A.date as Resv_date1, A.date - B.date as diff from
  73. (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
  74. from reservations r left join members m on m.id = r.member_id
  75. where r.state = 'completed'
  76. and m.imaginary in ('sofam', 'normal')
  77. and r.member_id not in ('125', '127')
  78. group by 1) A
  79. left join
  80. ################## To find WAU, Please enter start date and it will calculate 7 days from there
  81. (select distinct r.member_id as mid, min(r.id) as mrid,
  82. date(r.return_at + interval 8 hour) as date
  83. from reservations r left join members m on m.id = r.member_id
  84. where r.state = 'completed'
  85. and r.return_at + interval 8 hour >= @start1
  86. and r.return_at + interval 8 hour <= date_add(@start1, interval 7 day)
  87. and m.imaginary in ('sofam', 'normal')
  88. and r.member_id not in ('125', '127')
  89. group by 1) B
  90. on A.mid = B.mid
  91. ############## Total spent
  92. left join
  93. (select c.reservation_id as rid, sum(c.amount) as charges from charges c
  94. where c.state='normal' and c.kind in ('rent','oneway','d2d','mileage')
  95. group by rid) E on A.rid = E.rid
  96. left join
  97. (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
  98. where p.state = 'normal' and p.paid_type = 'coupon'
  99. group by p.reservation_id) F on A.rid = F.rid
  100. order by 1,2 asc) L
  101. where L.diff < 15
  102. and L.diff > 0
  103. group by 1) P ;
Advertisement
Add Comment
Please, Sign In to add comment