ahmedrahil786

2nd reservation on the same day Query_2 with times - Rahil

Jan 10th, 2020
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. set @startdate1 = '2019-09-01';
  2. set @startdate2 = '2020-02-01';
  3.  
  4. set @row_number := 0;
  5.  
  6. Select I.* from
  7. (SELECT
  8. @row_number:= CASE
  9. WHEN @customer_no = A.mid
  10. THEN @row_number + 1
  11. ELSE 1
  12. END AS num,
  13. @customer_no:= A.mid mid,
  14. A.rid,
  15. A.return_date as return_date,
  16. A.start_date as start_date,
  17. A.dur as duration,
  18. A.age as age,
  19. A.resv_hour as start_hour,
  20. A.Start_time as start_time,
  21. A.return_time as return_time,
  22. A.region as region,
  23. IFNULL(E.charges,0) as gross_rev,
  24. IFNULL(F.coupon_Spent,0) as Coupon_Spent,
  25. round((IFNULL(E.charges,0) - IFNULL(F.coupon_Spent,0)),2) as net_rev
  26. FROM
  27. (select
  28. distinct r.id as rid, r.member_id as mid,YEAR(r.start_at) - YEAR(m.birthday) as age,z.region as region,hour(r.start_at + interval '8' hour) as resv_hour, Time(r.start_at + interval '8' hour) as Start_time,
  29. Time(r.return_at + interval '8' hour) as return_time,
  30. round(sum(timestampdiff(minute, r.start_at, r.end_at)/60),2) as dur,
  31. Date(r.start_at + interval '8' hour) as start_date,
  32. Date(r.return_at + interval '8' hour) as return_date
  33. from reservations r left join members m on r.member_id = m.id
  34. left join reservation_appendixes ra on ra.reservation_id = r.id
  35. left join zones z on z.id = r.start_zone_id
  36. where r.state in ('completed')
  37. and r.return_at + interval 8 hour >= @startdate1
  38. and r.return_at + interval 8 hour <= @startdate2
  39. and m.imaginary in ('sofam', 'normal')
  40. and r.member_id not in ('125', '127')
  41. and ra.performer not in ('admin')
  42. group by rid) A
  43. left join
  44. (select c.reservation_id as rid, sum(c.amount) as charges from charges c
  45. where c.state='normal' and c.kind in ('rent','oneway','d2d','mileage','insurance')
  46. group by rid) E on A.rid = E.rid
  47. left join
  48. (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
  49. where p.state = 'normal' and p.paid_type = 'coupon'
  50. group by p.reservation_id) F on A.rid = F.rid
  51. ORDER BY start_date, mid,rid) I
  52. where I.num in (1,2)
  53. order by start_date desc
Advertisement
Add Comment
Please, Sign In to add comment