ahmedrahil786

Premium SCDW Customers - Behaviour

Nov 12th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. set @start := now() - interval 3 month;
  2.  
  3. Select P.mid as mid, P.name as name, P.age as age, P.Prem_insurance_resv as Prem_insurance_resv, P.amount as SCDW_Resv_amount, P.Premium_SCDW_amount as Premium_SCDW_Paid, P.dur as SCDW_dur,
  4. Q.resv as Resv_90days, Q.gross_rev as gross_rev_90days, Q.Coupon_Spent as Coupon_Spent_90days,Q.net_rev as net_rev_90days, Q.dur as dur from
  5.  
  6. ###### Finding Premium SCDW Customers
  7.  
  8. (Select A.mid as mid, count(distinct case when A.Premium_SCDW = 1 then A.rid end) as Prem_insurance_resv, SUM(A.Premium_SCDW_amount) as Premium_SCDW_amount , SUM(A.dur) as dur , Sum(A.amount) as amount,
  9. A.name as name , A.age as age from
  10. (select distinct r.id as rid, m.id as mid,m.display_name as name, (YEAR(r.start_at) - YEAR(m.birthday)) as age, round((timestampdiff(minute, r.start_at, r.end_at)/60),2) as dur, sum(c.amount) as amount,
  11. count(distinct case when cm.meta LIKE '%Basic SCDW%' then cm.charge_id end) as Basic_SCDW,
  12. count(distinct case when cm.meta LIKE '%Standard SCDW%' then cm.charge_id end) as Standard_SCDW,
  13. count(distinct case when cm.meta LIKE '%Premium SCDW%' then cm.charge_id end) as Premium_SCDW,
  14. ROUND(ifnull(Sum(distinct case when cm.meta LIKE '%Premium SCDW%' then c.amount end),0),2) as Premium_SCDW_amount
  15. from reservations r
  16. left join charges c on c.reservation_id = r.id
  17. left join members m on m.id = c.member_id
  18. left join charge_meta cm on cm.charge_id = c.id
  19. where m.imaginary in ('sofam', 'normal')
  20. and r.member_id not in ('125', '127')
  21. and r.state in ('completed','inUse')
  22. and Date(r.start_at + interval 8 hour) >= @start
  23. and c.state = 'normal'
  24. group by 1) A
  25. where A.Premium_SCDW = 1
  26. group by 1
  27. order by 2 desc) P
  28.  
  29. left join
  30.  
  31. ###### Mapping with all members last 30 days Reservations
  32.  
  33. (select A.mid as mid, A.name as name,A.age as age, Count(A.rid) as resv, sum(A.dur) as dur, IFNULL(SUM(E.charges),0) as gross_rev,
  34. IFNULL(SUM(F.coupon_Spent),0) as Coupon_Spent,
  35. round((IFNULL(SUM(E.charges),0) - IFNULL(SUM(F.coupon_Spent),0)),2) as net_rev from
  36. (select distinct r.id as rid, r.member_id as mid, m.display_name as name, (YEAR(r.start_at) - YEAR(m.birthday)) as age, round(sum(timestampdiff(minute, r.start_at, r.end_at)/60),2) as dur
  37. from reservations r left join members m on m.id = r.member_id
  38. left join zones z on z.id = r.start_zone_id
  39. left join member_appendixes ma on ma.member_id = r.member_id
  40. where m.imaginary in ('sofam', 'normal')
  41. and m.state = 'normal'
  42. and r.member_id not in ('125', '127')
  43. and z.id not in (2, 3, 101)
  44. and r.state in ('completed', 'inuse')
  45. and Date(r.start_at + interval 8 hour) >= @start
  46. group by 1) A
  47. left join
  48. (select c.reservation_id as rid, sum(c.amount) as charges from charges c
  49. where c.state='normal' and c.kind in ('rent','oneway','d2d','mileage','insurance')
  50. group by c.reservation_id) E on A.rid = E.rid
  51. left join
  52. (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
  53. where p.state = 'normal' and p.paid_type = 'coupon'
  54. group by p.reservation_id) F on A.rid = F.rid
  55. group by 1
  56. order by 1 asc) Q
  57. on Q.mid = P.mid
  58. order by P.mid asc
Add Comment
Please, Sign In to add comment