ahmedrahil786

2nd reservation on the same day Query - Rahil

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