ahmedrahil786

Reservation with Coupon Activation Time

May 8th, 2019
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. select
  2.  
  3. A.rid,
  4. A.memberid,
  5. A.couponname,
  6. A.day,
  7. A.ridelength ,
  8. A.ridelengthinminutes,
  9. A.startingday,
  10. A.couponactivationtime,
  11. A.reservationstarttime,
  12. A.startingtime,
  13. B.regionname,
  14. B.carname,
  15. A.carid,
  16. B.zoneid,
  17. A.state,
  18. A.amount,
  19. E.rev,
  20. F.rev2
  21.  
  22.  
  23. from (select
  24. distinct m.id as memberid,
  25. c.comment as couponname,
  26. sum(p.amount) as amount,
  27. date(r.occupy_start_at + interval '8' hour) as day,
  28. timestampdiff(minute,CONVERT_TZ(r.start_at, '+00:00', '+8:00'), CONVERT_TZ(r.end_at, '+00:00', '+8:00'))/60 as ridelength,
  29. TIMESTAMPDIFF(minute,CONVERT_TZ(r.start_at, '+00:00', '+8:00'), CONVERT_TZ(r.end_at, '+00:00', '+8:00')) as ridelengthinminutes,
  30. weekday(r.occupy_start_at + interval '8' hour) as startingday,
  31. hour(c.activated_at + interval '8' hour) as couponactivationtime,
  32. hour(r.start_at + interval '8' hour) as reservationstarttime,
  33. hour(r.occupy_start_at + interval '8' hour) as startingtime,
  34. r.id as rid,
  35. r.state as state,
  36. r.car_id as carid
  37.  
  38. from members m
  39.  
  40. left outer join reservations r on m.id = r.member_id
  41. LEFT JOIN coupons c ON c.reservation_id = r.id
  42. left outer join payments p on p.reservation_id = r.id
  43.  
  44. where CONVERT_TZ(r.start_at, '+00:00', '+8:00') >= '2019-1-1 00:00'
  45. and CONVERT_TZ(r.start_at, '+00:00', '+8:00') < '2019-7-1'
  46. and m.imaginary = 'normal'
  47. and m.state = 'normal'
  48. and r.state IN ('completed','inuse','reserved')
  49. and p.state = 'normal'
  50. and p.paid_type = 'card'
  51.  
  52. group by m.id,r.id
  53. order by amount desc) A
  54. left outer join
  55.  
  56. (select
  57. c.id as carid,
  58. cc.car_name as carname,
  59. z.region as regionname,
  60. z.id as zoneid
  61. from cars c
  62.  
  63. left outer join car_classes cc on cc.id = c.car_class_id
  64. left outer join zones z on z.id = c.zone_id
  65. left outer join car_zone_logs as cz on c.id = cz.car_id
  66.  
  67. where cz.car_state = 'Normal'
  68. #and z.state = 'normal'
  69.  
  70. group by c.id) B
  71.  
  72. on A.carid = B.carid
  73.  
  74. left join (
  75. select sum(c.amount) as rev, c.reservation_id as rid
  76. from charges c
  77. where c.state = 'normal' and c.kind IN ('rent' , 'oneway', 'd2d', 'mileage')
  78. group by c.reservation_id
  79. ) as E ON A.rid = E.rid
  80.  
  81. left join (
  82. select sum(c.amount) as rev2, c.reservation_id as rid
  83. from charges c
  84. where c.state = 'normal' and c.kind IN ('penalty', 'accident')
  85. group by c.reservation_id
  86. ) as F ON A.rid = F.rid
  87.  
  88. group by A.memberid,B.carid,A.rid
  89.  
  90. order by A.amount desc
Advertisement
Add Comment
Please, Sign In to add comment