ahmedrahil786

F1- New Daily Index File share tracker

Jun 13th, 2019 (edited)
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 7.94 KB | None | 0 0
  1. #With all factors ###############################################################################################################################
  2.  
  3. set @startdate1 = date_sub(CURDATE(), interval 10 day);
  4. set @startdate2 = date_add(CURDATE(), interval 10 day);
  5. use socar_malaysia;
  6.  
  7. select
  8. A.date,
  9. A.ncars,
  10. A.nuse,
  11. A.nuse_rnd,
  12. A.res_d2d,
  13. A.res_oneway,
  14. B.ncars_used,
  15. B.ncar_returned,
  16. A.nzone,
  17. A.nmem,
  18. A.rmil,
  19. A.dur,
  20. A.ad_sdur,
  21. A.revenues,
  22. A.coupons,
  23. A.nr
  24. from
  25. (select date_format(convert_tz(r.return_at, '+00:00', '+8:00'),'%Y-%m-%d') as date,
  26. (select count(czl.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 ncars,
  27. count(r.id) as nuse,
  28. count(if(r.way='round', r.id, null)) as nuse_rnd,
  29. count(if(r.way='d2d', r.id, null)) as res_d2d,
  30. count(if(r.way in('oneway','onewayReturn' ), r.id, null)) as res_oneway,
  31. count(distinct r.car_id) as ncar_used,
  32. count(distinct z.id) as nzone,
  33. count(distinct m.id) as nmem,
  34. #count(if(r.way='oneway', r.id, null)) as res_ow,
  35. sum(ra.mileage) as rmil,
  36. round(sum(timestampdiff(minute, r.start_at, r.end_at)/60),2) as dur,
  37. 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,
  38. round(sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('rent','oneway','d2d','mileage','insurance','penalty','fine','reservationFee','cancelFee','parkingFee','etc','accident','chauffeur','fuel')),0)),2) as revenues,
  39. round(sum(ifnull((select sum(amount) from payments where state='normal' and reservation_id=r.id and paid_type ='coupon'),0)),2) as coupons,
  40. round(sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('rent','oneway','d2d','mileage','insurance','penalty','fine','reservationFee','cancelFee','parkingFee','etc','accident','chauffeur','fuel')),0)
  41. - ifnull((select sum(amount) from payments where state='normal' and reservation_id=r.id and paid_type ='coupon'),0)),2) as nr
  42.  
  43.  
  44. from reservations r, zones z, members m, reservation_appendixes ra
  45.  
  46. Where
  47.     r.start_zone_id = z.id
  48.     and r.member_id = m.id
  49.     and r.id = ra.reservation_id
  50.     and m.imaginary in ('sofam', 'normal')
  51.     and r.member_id not in ('125', '127')
  52.     and z.id not in (2, 3, 101)
  53.     and r.state in ('completed')
  54.     and r.return_at + interval 8 hour >= @startdate1
  55.     and r.return_at + interval 8 hour <= @startdate2
  56.  
  57. group by date
  58. order by date desc) A
  59.  
  60. left join
  61.  
  62. ((select temp.rday as date,
  63.   (select count(czl.id) from car_zone_logs czl where rday=date_format(czl.log_date, "%Y-%m-%d") and czl.zone_state='normal') as ncars,
  64.   temp.ncars_used,
  65.   (select count(distinct car_id) from reservations r, members m
  66.       where r.state = 'completed'
  67.         and date_format(r.return_at+interval 8 hour, "%Y-%m-%d") = temp.rday
  68.         and r.member_id=m.id
  69.         and m.imaginary in ('sofam', 'normal')
  70.         and r.start_zone_id not in (2,3,101)
  71.   ) ncar_returned
  72. from (
  73.   select rday,count(distinct cid) as ncars_used
  74.   from (select date_format(dt.rday, "%Y-%m-%d") as rday, resv.rid as rid, resv.cid as cid, resv.rsh as rsh, resv.reh as reh
  75.     from (select concat(date_format(r.return_at+interval 8 hour, "%Y-%m-%d"), " 00:00:00") as rday from reservations r
  76.       where r.return_at is not null and r.return_at+interval 8 hour > @startdate1
  77.       group by rday
  78.     ) dt,
  79.     (select r.id as rid, r.car_id as cid, r.start_at+interval 8 hour as rsh, r.end_at+interval 8 hour as reh
  80.       from reservations r, members m
  81.       where r.member_id=m.id
  82.         and m.imaginary in ('sofam', 'normal')
  83.         and r.state in ('completed', 'inuse')
  84.         and r.start_zone_id not in (2,3,101)
  85.         and r.end_at+interval 8 hour >= @startdate1
  86.     ) resv
  87. where resv.rsh < (dt.rday+interval 1 day) and resv.reh > dt.rday) temp
  88. group by rday
  89. order by rday desc) temp)) B
  90. on A.Date = B.Date
  91. group by A.date
  92. order by A.date desc
  93. ;
  94.  
  95. #####Only reservation Related################################################################################################################################
  96.  
  97. set @startdate1 = date_sub(CURDATE(), interval 10 day);
  98. set @startdate2 = date_add(CURDATE(), interval 10 day);
  99. use socar_malaysia;
  100.  
  101. select
  102. A.date,
  103. A.ncars,
  104. A.nuse,
  105. A.nuse_rnd,
  106. A.res_d2d,
  107. A.res_oneway,
  108. B.ncars_used,
  109. B.ncar_returned,
  110. A.nzone,
  111. A.nmem,
  112. A.rmil,
  113. A.dur,
  114. A.ad_sdur,
  115. A.revenues,
  116. A.coupons,
  117. A.nr
  118. from
  119. (select date_format(convert_tz(r.return_at, '+00:00', '+8:00'),'%Y-%m-%d') as date,
  120. (select count(czl.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 ncars,
  121. count(r.id) as nuse,
  122. count(if(r.way='round', r.id, null)) as nuse_rnd,
  123. count(if(r.way='d2d', r.id, null)) as res_d2d,
  124. count(if(r.way in('oneway','onewayReturn' ), r.id, null)) as res_oneway,
  125. count(distinct r.car_id) as ncar_used,
  126. count(distinct z.id) as nzone,
  127. count(distinct m.id) as nmem,
  128. #count(if(r.way='oneway', r.id, null)) as res_ow,
  129. sum(ra.mileage) as rmil,
  130. round(sum(timestampdiff(minute, r.start_at, r.end_at)/60),2) as dur,
  131. 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,
  132. round(sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('rent','oneway','d2d','mileage','insurance','chauffeur','fuel')),0)),2) as revenues,
  133. round(sum(ifnull((select sum(amount) from payments where state='normal' and reservation_id=r.id and paid_type ='coupon'),0)),2) as coupons,
  134. round(sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('rent','oneway','d2d','mileage','insurance','chauffeur','fuel')),0)
  135. - ifnull((select sum(amount) from payments where state='normal' and reservation_id=r.id and paid_type ='coupon'),0)),2) as nr
  136.  
  137.  
  138. from reservations r, zones z, members m, reservation_appendixes ra
  139.  
  140. Where
  141.     r.start_zone_id = z.id
  142.     and r.member_id = m.id
  143.     and r.id = ra.reservation_id
  144.     and m.imaginary in ('sofam', 'normal')
  145.     and r.member_id not in ('125', '127')
  146.     and z.id not in (2, 3, 101)
  147.     and r.state in ('completed')
  148.     and r.return_at + interval 8 hour >= @startdate1
  149.     and r.return_at + interval 8 hour <= @startdate2
  150.  
  151. group by date
  152. order by date desc) A
  153.  
  154. left join
  155.  
  156. ((select temp.rday as date,
  157.   (select count(czl.id) from car_zone_logs czl where rday=date_format(czl.log_date, "%Y-%m-%d") and czl.zone_state='normal') as ncars,
  158.   temp.ncars_used,
  159.   (select count(distinct car_id) from reservations r, members m
  160.       where r.state = 'completed'
  161.         and date_format(r.return_at+interval 8 hour, "%Y-%m-%d") = temp.rday
  162.         and r.member_id=m.id
  163.         and m.imaginary in ('sofam', 'normal')
  164.         and r.start_zone_id not in (2,3,101)
  165.   ) ncar_returned
  166. from (
  167.   select rday,count(distinct cid) as ncars_used
  168.   from (select date_format(dt.rday, "%Y-%m-%d") as rday, resv.rid as rid, resv.cid as cid, resv.rsh as rsh, resv.reh as reh
  169.     from (select concat(date_format(r.return_at+interval 8 hour, "%Y-%m-%d"), " 00:00:00") as rday from reservations r
  170.       where r.return_at is not null and r.return_at+interval 8 hour > @startdate1
  171.       group by rday
  172.     ) dt,
  173.     (select r.id as rid, r.car_id as cid, r.start_at+interval 8 hour as rsh, r.end_at+interval 8 hour as reh
  174.       from reservations r, members m
  175.       where r.member_id=m.id
  176.         and m.imaginary in ('sofam', 'normal')
  177.         and r.state in ('completed', 'inuse')
  178.         and r.start_zone_id not in (2,3,101)
  179.         and r.end_at+interval 8 hour >= @startdate1
  180.     ) resv
  181. where resv.rsh < (dt.rday+interval 1 day) and resv.reh > dt.rday) temp
  182. group by rday
  183. order by rday desc) temp)) B
  184. on A.Date = B.Date
  185. group by A.date
  186. order by A.date desc
  187. ;
Advertisement
Add Comment
Please, Sign In to add comment