ahmedrahil786

Revenue Generated per zone - Monthly - Rahil - Customer Stud

Jun 13th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. select
  2. r.id as rid,
  3. r.member_id as mid,
  4. round(sum(timestampdiff(minute, r.start_at, r.end_at)/60),2) as dur,
  5. month(r.start_at + interval '8' hour) as month,
  6. Year(r.start_at + interval '8' hour) as Year,
  7. r.start_zone_id as zoneid,
  8. z.region as region,
  9. count(r.id) as reservations,
  10. IFNULL(round(B.charges,2),0) as gross_rev,
  11. round((IFNULL(B.charges,0) - IFNULL(A.coupon,0)),2) as net_rev,
  12. round(IFNULL((IFNULL(A.coupon,0)/B.charges),0),2) as coupon_rate
  13. from reservations r
  14. join zones z on z.id = r.start_zone_id
  15. join members m on m.id = r.member_id
  16. left join
  17. (select
  18. 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
  19. from payments p
  20. where p.state = 'normal' and p.paid_type = 'coupon'
  21. group by p.reservation_id,month) A
  22. on A.rid = r.id and A.month = month(r.start_at + interval '8' hour) and A.year = Year(r.start_at + interval '8' hour)
  23. left join
  24. (select
  25. c.reservation_id as rid, month(c.created_at + interval '8' hour) as month, Year(c.created_at + interval '8' hour) as year, sum(c.amount) as charges
  26. from charges c
  27. where c.state = 'normal' and c.kind IN ('rent' , 'oneway', 'd2d', 'mileage')
  28. group by c.reservation_id,month) B
  29. on B.rid = r.id and B.month = month(r.start_at + interval '8' hour) and B.year = Year(r.start_at + interval '8' hour)
  30.  
  31. where m.imaginary in ('normal')
  32. and r.member_id not in ('125', '127')
  33. and r.state in ('completed','inUse')
  34. and r.start_at + interval 8 hour >= '2018-01-01'
  35. group by r.id,month,zoneid
  36. order by mid,rid
Advertisement
Add Comment
Please, Sign In to add comment