ahmedrahil786

MFT - Region - Duration - City - Bookings Info - Rahil

Jun 18th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. set @start := '2019-1-1';
  2.  
  3. select
  4. A.mid,
  5. A.month,
  6. A.week,
  7. count(A.mid) as totalmfts,
  8. B.ridelength,
  9. B.carname,
  10. IFNULL(B.city,"") as City,
  11. IFNULL(B.region,"") as Region,
  12. IFNULL(E.charges,0) as gross_rev,
  13. IFNULL(F.coupon_Spent,0) as Coupon_Spent,
  14. round((IFNULL(E.charges,0) - IFNULL(F.coupon_Spent,0)),2) as net_rev
  15. from
  16. (select
  17. distinct c.member_id as mid,
  18. Month(c.created_at + interval '8' hour) as month,
  19. WEEKOFYEAR(c.created_at + interval '8' hour) as Week
  20. from charges c
  21. where c.kind = 'subscriptionFee'
  22. and c.created_at + interval '8' hour >= @start) A
  23. left join
  24. (select
  25. distinct r.member_id as mid,
  26. min(r.id) as rid,
  27. timestampdiff(minute,CONVERT_TZ(r.start_at, '+00:00', '+8:00'), CONVERT_TZ(r.end_at, '+00:00', '+8:00'))/60 as ridelength,
  28. z.city as city,
  29. z.region as region,
  30. cc.car_name as carname
  31. from reservations r
  32. join members m on r.member_id = m.id
  33. join zones z on r.start_zone_id = z.id
  34. join cars ca on ca.id = r.car_id
  35. join car_classes cc on cc.id = ca.car_class_id
  36. where m.imaginary in ('sofam', 'normal')
  37. and r.member_id not in ('125', '127')
  38. and r.start_at + interval '8' hour >= @start
  39. and r.state = 'completed'
  40. group by r.member_id) B
  41. on A.mid = B.mid
  42. left join
  43. (select c.reservation_id as rid, sum(c.amount) as charges from charges c
  44. where c.state='normal' and c.kind in ('rent','oneway','d2d','mileage') and c.created_at + interval '8' hour >= @start
  45. group by rid) E on B.rid = E.rid
  46. left join
  47. (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
  48. where p.state = 'normal' and p.paid_type = 'coupon'
  49. group by p.reservation_id) F on F.rid = E.rid
  50. where B.city is not null
  51. group by A.mid
Advertisement
Add Comment
Please, Sign In to add comment