ahmedrahil786

MFTs, MAUs. RESV by City - Rahil

Jan 3rd, 2020
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. set @startdate1 = '2017-01-01';
  2. set @startdate2 = '2020-01-01';
  3. use socar_malaysia;
  4.  
  5. Select A.year as year, A.month as month, A.city as city , A.duration as duration, A.resv as resv, A.mau as mau, B.cars as cars , C.totalmfts as totalmfts from
  6. (select year(A.return_date) as year, month(A.return_date) as month, A.city as city, count(distinct A.mid) as mau, Sum(A.dur) as duration, Count(A.rid2) as resv from
  7. (select
  8. distinct r.id as rid2, r.member_id as mid, z.city as city,
  9. round(sum(timestampdiff(minute, r.start_at, r.end_at)/60),2) as dur,
  10. 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,
  11. Date(r.return_at + interval '8' hour) as return_Date
  12. from reservations r left join members m on r.member_id = m.id
  13. left join reservation_appendixes ra on ra.reservation_id = r.id
  14. left join zones z on r.start_zone_id = z.id
  15. where r.state in ('completed')
  16. and r.return_at + interval 8 hour >= @startdate1
  17. and r.return_at + interval 8 hour <= @startdate2
  18. and m.imaginary in ('sofam', 'normal')
  19. and r.member_id not in ('125', '127')
  20. group by rid2) A
  21. where A.city not in ("-")
  22. group by 1,2,3
  23. order by 1,2 asc ) A
  24. left join
  25. (select year(czl.log_date + interval 8 hour ) as year, month(czl.log_date + interval 8 hour ) as month, z.city as city, count(distinct czl.car_id) as cars
  26. from car_zone_logs czl
  27. left join cars cr on cr.id = czl.car_id
  28. left join car_classes cc on cc.id = cr.car_class_id
  29. left join zones z on z.id = czl.zone_id
  30. where czl.zone_state = 'normal'
  31. and czl.log_date + interval 8 hour >= @startdate1
  32. and czl.log_date + interval 8 hour <= @startdate2
  33. group by 1,2,3
  34. order by 1,2 asc) B
  35. on A.year = B.year and A.month = B.month and A.city = B.city
  36.  
  37. left join
  38. (Select
  39. Year(A.date) as year,
  40. Month(A.date) as month,
  41. count(A.mid) as totalmfts,
  42. IFNULL(B.city,"") as City
  43. from
  44. (select
  45. distinct c.member_id as mid,
  46. date(c.created_at + interval '8' hour) as date
  47. from charges c
  48. where c.kind = 'subscriptionFee'
  49. and c.created_at + interval '8' hour >= @start) A
  50. left join
  51. (select
  52. distinct r.member_id as mid,
  53. min(r.id) as rid,
  54. z.city as city
  55. from reservations r
  56. join members m on r.member_id = m.id
  57. join zones z on r.start_zone_id = z.id
  58. where m.imaginary in ('sofam', 'normal')
  59. and r.member_id not in ('125', '127')
  60. and r.start_at + interval '8' hour >= @start
  61. group by r.member_id) B
  62. on A.mid = B.mid
  63. where B.city not in ("-")
  64. group by 1,2,4
  65. order by 1,2 asc) C
  66. on A.year = C.year and A.month = C.month and A.city = C.city
Advertisement
Add Comment
Please, Sign In to add comment