ahmedrahil786

Shiva Query - For reservation - R.red_at - for predictions

Mar 18th, 2020
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. set @startDate = '2020-03-10 00:00:00';
  2. select
  3. week(convert_tz(r.end_at, '+00:00', '+8:00'),3) as week,
  4. date_format(convert_tz(r.end_at, '+00:00', '+8:00'),'%Y-%m-%d') as date,
  5. (select count(czl.id) from car_zone_logs czl where date=date_format(czl.log_date, "%Y-%m-%d") and czl.zone_state='normal' group by date_format(czl.log_date, "%Y-%m-%d")) as ncars,
  6. (select count(distinct czl.zone_id) from car_zone_logs czl where date=date_format(czl.log_date+interval 8 hour, "%Y-%m-%d") and czl.zone_state='normal') as nzones,
  7. count(distinct r.car_id) as ncar_used,
  8. count(distinct z.id) as nzone,
  9. count(r.id) as res,
  10. count(if(r.way='d2d', r.id, null)) as res_d2d,
  11. count(if(r.way='oneway', r.id, null)) as res_ow,
  12. round(sum(timestampdiff(minute, r.start_at, r.end_at)/60),2) as dur,
  13. 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,
  14. round(sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('rent','oneway','d2d','mileage','insurance')),0)),2) as revenues,
  15. round(sum(ifnull((select sum(amount) from payments where state='normal' and reservation_id=r.id and paid_type ='coupon'),0)),2) as coupons,
  16. round(sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('rent','oneway','d2d','mileage','insurance')),0)
  17. - ifnull((select sum(amount) from payments where state='normal' and reservation_id=r.id and paid_type ='coupon'),0)),2) as nr,
  18. count(distinct m.id) as dau,
  19. sum(ra.mileage) as rmil
  20. from reservations r, zones z, members m, reservation_appendixes ra
  21. where
  22. r.start_zone_id = z.id
  23. and r.member_id = m.id
  24. and r.id = ra.reservation_id
  25. and m.imaginary in ('sofam', 'normal')
  26. and r.member_id not in ('125', '127')
  27. and z.id not in (2, 3, 101)
  28. and r.state in ('completed','inUse','reserved')
  29. and r.end_at+ interval 8 hour >= @startDate
  30. and r.end_at+ interval 8 hour < date_add(@startDate, interval 2 week)
  31. group by date
  32. order by date desc
Advertisement
Add Comment
Please, Sign In to add comment