Advertisement
ahmedrahil786

Query for All Reservations - Try Combo - Rahil

Jul 23rd, 2019
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. set @startdate1 = '2019-07-15';
  2. set @startdate2 = '2019-08-01';
  3. use socar_malaysia;
  4. select I.date, Count(I.rid) as resv, Count(distinct mid) as members,Sum(I.dur) as dur, Sum(I.ad_sdur) as Ad_sdur,Sum(I.distance) as distance, Round(Sum(I.gross_rev),2) as gross_rev, ROUND(Sum(I.coupon_spent),2) as coupon_spent , ROUND(Sum(I.net_rev),2) as Net_rev from
  5. (select A.return_date as date, A.rid as rid, A.mid as mid, A.region as region, A.city as city, A.car_name as car_name, A.coupon as coupon, A.dur as dur, A.ad_sdur as ad_sdur, A.distance as distance,
  6. A.nuse_rnd as nuse_rnd,A.res_d2d as res_d2d,A.res_oneway as res_oneway , A.resv_hour as res_hour, A.occupy_start_hour as start_hour, A.age as age, IFNULL(E.charges,0) as gross_rev,
  7. IFNULL(F.coupon_Spent,0) as Coupon_Spent,
  8. round((IFNULL(E.charges,0) - IFNULL(F.coupon_Spent,0)),2) as net_rev from
  9. (select
  10. distinct r.id as rid, r.member_id as mid, z.region, z.city, cc.car_name,IFNULL(co.comment,"none") as coupon,
  11. round(sum(timestampdiff(minute, r.start_at, r.end_at)/60),2) as dur,
  12. round(sum(floor(timestampdiff(minute, r.start_at, r.end_at)/60/24)*12+if(mod(timestampdiff(hour, r.start_at, r.end_at),24)>=12,12,mod(timestampdiff(minute, r.start_at, r.end_at)/60,24))),2) as ad_sdur,
  13. IFNULL(sum(ra.mileage),0) as distance,
  14. count(if(r.way='round', r.id, null)) as nuse_rnd,
  15. count(if(r.way='d2d', r.id, null)) as res_d2d,
  16. count(if(r.way='oneway', r.id, null)) as res_oneway,
  17. Date(r.return_at + interval '8' hour) as return_Date,
  18. hour(r.start_at + interval '8' hour) as resv_hour,
  19. hour(r.occupy_start_at + interval '8' hour) as occupy_start_hour,
  20. (YEAR(r.start_at) - YEAR(m.birthday)) as age
  21. from reservations r left join members m on r.member_id = m.id
  22. left join zones z on z.id = r.start_zone_id
  23. left join cars cr on cr.id = r.car_id
  24. left join car_classes cc on cc.id = cr.car_class_id
  25. left join coupons co on co.reservation_id = r.id
  26. left join reservation_appendixes ra on ra.reservation_id = r.id
  27. where r.state in ('completed')
  28. and r.return_at + interval 8 hour >= @startdate1
  29. and r.return_at + interval 8 hour <= @startdate2
  30. and m.imaginary in ('sofam', 'normal')
  31. and r.member_id not in ('125', '127')
  32. and (YEAR(r.start_at) - YEAR(m.birthday)) < 100
  33. group by rid) A
  34. left join
  35. (select c.reservation_id as rid, sum(c.amount) as charges from charges c
  36. where c.state='normal' and c.kind in ('rent','oneway','d2d','mileage')
  37. group by rid) E on A.rid = E.rid
  38. left join
  39. (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
  40. where p.state = 'normal' and p.paid_type = 'coupon'
  41. group by p.reservation_id) F on A.rid = F.rid) I
  42. group by I.date
  43. order by date desc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement