ahmedrahil786

Study of Accidents - Rahil

Sep 23rd, 2019
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. set @startDate = "2019-01-01 00:00:00";
  2.  
  3. select A.date as date, A.rid as rid, A.mid as mid, A.age, A.resv_Start_hour as resv_Start_hour,A.dur as dur, A.region as region,
  4. A.city as city, A.carname as carname, A.kind as kind,IFNULL(E.charges,0) as gross_rev,
  5. IFNULL(F.coupon_Spent,0) as Coupon_Spent,
  6. round((IFNULL(E.charges,0) - IFNULL(F.coupon_Spent,0)),2) as net_rev from
  7.  
  8. (select
  9. distinct c.reservation_id as rid,
  10. m.id as mid,
  11. (YEAR(r.start_at) - YEAR(m.birthday)) as age,
  12. date(c.created_at + interval '8' hour) as date,
  13. hour(r.start_at + interval '8' hour) as resv_Start_hour,
  14. round(sum(timestampdiff(minute, r.start_at, r.end_at)/60),2) as dur,
  15. z.region as region, z.city as city, cc.car_name as carname,c.kind as kind
  16. from charges c
  17. left join members m on m.id = c.member_id
  18. left join reservations r on r.id = c.reservation_id
  19. left join zones z on z.id = r.start_zone_id
  20. left join cars cr on cr.id = r.car_id
  21. left join car_classes cc on cc.id = cr.car_class_id
  22. where c.kind = 'accident'
  23. and date(c.created_at + interval '8' hour) >= @startdate
  24. group by 1) A
  25. left join
  26. (select c.reservation_id as rid, sum(c.amount) as charges from charges c
  27. where c.state='normal' and c.kind in ('rent','oneway','d2d','mileage')
  28. group by rid) E on A.rid = E.rid
  29. left join
  30. (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
  31. where p.state = 'normal' and p.paid_type = 'coupon'
  32. group by p.reservation_id) F on A.rid = F.rid
  33. where A.rid is not null
  34. group by 2
Advertisement
Add Comment
Please, Sign In to add comment