ahmedrahil786

Age group wise number of accidents Vs Age and Reservations

May 12th, 2020
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1.  
  2. Select A.age, count(Distinct rid) as resv , count(Distinct mid) as mid
  3. from
  4. (select
  5. distinct r.id as rid, r.member_id as mid,
  6. (YEAR(r.start_at) - YEAR(m.birthday)) as age,
  7. Date(r.return_at + interval '8' hour) as resv_Date
  8. from reservations r left join members m on r.member_id = m.id
  9. where r.state in ('completed')
  10. and r.return_at + interval 8 hour >= '2017-01-01'
  11. and r.return_at + interval 8 hour <= date_sub(CURDATE(), interval 30 day)
  12. and m.imaginary in ('sofam', 'normal')
  13. and r.member_id not in ('125', '127')
  14. and (YEAR(r.start_at) - YEAR(m.birthday)) < 100
  15. group by 1
  16. order by 2 asc , 1 asc) A
  17. group by 1
  18. order by 1 asc;
  19.  
  20. select I.age as age, count(I.rid) as resv from
  21. (SELECT r.id as rid,
  22. r.member_id as mid,
  23. cc.car_name "car_name",
  24. cc.maker "manufacturer",
  25. (YEAR(r.start_at) - YEAR(m.birthday)) as age,
  26. ROUND(sum(ca.quotation_amount),0) "total_repair_cost",
  27. count(c.registration_no) "number_of_accidents",
  28. crs.name,
  29. round(sum(timestampdiff(minute, r.start_at, r.end_at)/60),2) as dur,
  30. 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,
  31. IFNULL(sum(ra.mileage),0) as distance,
  32. date(ca.accident_at+interval 8 hour ) as "accident date",
  33. date(ca.created_at+ interval 8 hour) "GOS record created"
  34. FROM car_accidents ca
  35. LEFT JOIN reservations r ON r.id=ca.reservation_id
  36. left join members m on m.id = r.member_id
  37. left join reservation_appendixes ra on ra.reservation_id = r.id
  38. LEFT JOIN cars c ON c.id=ca.car_id
  39. LEFT JOIN car_classes cc ON cc.id=c.car_class_id
  40. LEFT JOIN car_repair_shops crs ON crs.id=ca.car_repair_shop_id
  41. WHERE ca.state <> "duplicated"
  42. AND ca.state <> "deleted" and ((ca.insurance_type<>"thirdParty" and ca.is_insurance=1) or ca.is_insurance=0)
  43. AND greatest(date(r.occupy_start_at+interval 8 hour),date(ca.accident_at+interval 8 hour))>= '2017-01-01'
  44. AND greatest(date(r.occupy_start_at+interval 8 hour), date(ca.accident_at+interval 8 hour))<= '2020-05-01'
  45. GROUP BY r.id
  46. ORDER BY avg(ca.quotation_amount) DESC) I
  47. group by 1
Advertisement
Add Comment
Please, Sign In to add comment