ahmedrahil786

F9-Monthly Index- Loui Help query - Daily Index Dashboard

Jun 12th, 2019
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. ##Without all factors###############################################################################################################################
  2.  
  3. set @startdate1 = '2019-09-01';
  4. set @startdate2 = '2020-01-01';
  5. use socar_malaysia;
  6.  
  7. Select A.month,
  8. B.totalcars as ncars,
  9. A.nuse,
  10. A.nuse_rnd,
  11. A.res_d2d,
  12. A.res_oneway,
  13. A.ncar_used,
  14. A.nzone,
  15. A.nmem,
  16. A.rmil,
  17. A.dur,
  18. A.ad_sdur,
  19. A.revenues,
  20. A.coupons,
  21. A.nr
  22. from
  23.  
  24.  
  25. (select concat(month(convert_tz(r.return_at, '+00:00', '+8:00')),"-","2019") as month,
  26. month(convert_tz(r.return_at, '+00:00', '+8:00')) as month2,
  27. (select count( czl.id) from car_zone_logs czl where month=month(czl.log_date+interval 8 hour) and czl.zone_state='normal') as ncars,
  28. count(r.id) as nuse,
  29. count(if(r.way='round', r.id, null)) as nuse_rnd,
  30. count(if(r.way='d2d', r.id, null)) as res_d2d,
  31. count(if(r.way in('oneway','onewayReturn' ), r.id, null)) as res_oneway,
  32. count(distinct r.car_id) as ncar_used,
  33. count(distinct z.id) as nzone,
  34. count(distinct m.id) as nmem,
  35. #count(if(r.way='oneway', r.id, null)) as res_ow,
  36. sum(ra.mileage) as rmil,
  37. round(sum(timestampdiff(minute, r.start_at, r.end_at)/60),2) as dur,
  38. 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,
  39. 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,
  40. round(sum(ifnull((select sum(amount) from payments where state='normal' and reservation_id=r.id and paid_type ='coupon'),0)),2) as coupons,
  41. 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)
  42. - ifnull((select sum(amount) from payments where state='normal' and reservation_id=r.id and paid_type ='coupon'),0)),2) as nr
  43.  
  44.  
  45. from reservations r, zones z, members m, reservation_appendixes ra
  46.  
  47. Where
  48. r.start_zone_id = z.id
  49. and r.member_id = m.id
  50. and r.id = ra.reservation_id
  51. and m.imaginary in ('sofam', 'normal')
  52. and r.member_id not in ('125', '127')
  53. and z.id not in (2, 3, 101)
  54. and r.state in ('completed')
  55. and r.return_at + interval 8 hour >= @startdate1
  56. and r.return_at + interval 8 hour <= @startdate2
  57.  
  58. group by month
  59. order by month desc) A
  60.  
  61. join
  62. (select
  63. rmonth2 as month2, ncars as ncars, wtotal_ncars as totalcars
  64.  
  65. from (
  66. select rmonth2,
  67. (select count(cz.id) from car_zone_logs cz, zones z
  68. where cz.zone_id=z.id and z.id not in (2,3,101,383,378,712,818,786) and date_format(date_sub(cz.log_date, interval 1 day)+interval 8 hour, "%Y-%m-%d")=mlast
  69. ) as ncars,
  70. (select count(cz.id) from car_zone_logs cz, zones z
  71. where cz.zone_id=z.id and z.id not in (2,3,101,383,378,712,818,786) and date_sub(cz.log_date, interval 1 day)+interval 8 hour >= mfirst and date_sub(cz.log_date, interval 1 day)+interval 8 hour < mlast+interval 1 day
  72. ) as wtotal_ncars
  73.  
  74. from (
  75.  
  76. select
  77. rmonth2, min(date_format(rret,"%Y-%m-%d")) as mfirst, max(date_format(rret,"%Y-%m-%d")) as mlast
  78.  
  79. from (
  80. select
  81. month(r.return_at+interval 8 hour) as rmonth2,
  82. r.start_at+interval 8 hour as rst, r.end_at+interval 8 hour as rend, r.return_at+interval 8 hour as rret
  83. from reservations r, members m, zones z
  84. where
  85. r.state='completed'
  86. and r.member_id=m.id
  87. and m.imaginary in ('normal', 'sofam')
  88. and r.start_zone_id=z.id
  89. and r.start_zone_id not in (2,3,101,383,378,697,712,818,786)
  90. and r.return_at + interval 8 hour >= @startdate1
  91. and r.return_at + interval 8 hour <= @startdate2
  92. ) temp
  93. group by rmonth2
  94. ) temp
  95. group by rmonth2
  96. ) temp
  97. group by rmonth2) B
  98.  
  99. on A.month2 = B.month2
  100.  
  101. order by A.month2 desc;
  102.  
  103.  
  104. ##With all factors###############################################################################################################################
  105.  
  106. set @startdate1 = '2019-09-01';
  107. set @startdate2 = '2020-01-01';
  108. use socar_malaysia;
  109.  
  110. Select A.month,
  111. B.totalcars as ncars,
  112. A.nuse,
  113. A.nuse_rnd,
  114. A.res_d2d,
  115. A.res_oneway,
  116. A.ncar_used,
  117. A.nzone,
  118. A.nmem,
  119. A.rmil,
  120. A.dur,
  121. A.ad_sdur,
  122. A.revenues,
  123. A.coupons,
  124. A.nr
  125. from
  126.  
  127.  
  128. (select concat(month(convert_tz(r.return_at, '+00:00', '+8:00')),"-","2019") as month,
  129. month(convert_tz(r.return_at, '+00:00', '+8:00')) as month2,
  130. (select count( czl.id) from car_zone_logs czl where month=month(czl.log_date+interval 8 hour) and czl.zone_state='normal') as ncars,
  131. count(r.id) as nuse,
  132. count(if(r.way='round', r.id, null)) as nuse_rnd,
  133. count(if(r.way='d2d', r.id, null)) as res_d2d,
  134. count(if(r.way in('oneway','onewayReturn' ), r.id, null)) as res_oneway,
  135. count(distinct r.car_id) as ncar_used,
  136. count(distinct z.id) as nzone,
  137. count(distinct m.id) as nmem,
  138. #count(if(r.way='oneway', r.id, null)) as res_ow,
  139. sum(ra.mileage) as rmil,
  140. round(sum(timestampdiff(minute, r.start_at, r.end_at)/60),2) as dur,
  141. 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,
  142. 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')),0)),2) as revenues,
  143. round(sum(ifnull((select sum(amount) from payments where state='normal' and reservation_id=r.id and paid_type ='coupon'),0)),2) as coupons,
  144. 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')),0)
  145. - ifnull((select sum(amount) from payments where state='normal' and reservation_id=r.id and paid_type ='coupon'),0)),2) as nr
  146.  
  147.  
  148. from reservations r, zones z, members m, reservation_appendixes ra
  149.  
  150. Where
  151. r.start_zone_id = z.id
  152. and r.member_id = m.id
  153. and r.id = ra.reservation_id
  154. and m.imaginary in ('sofam', 'normal')
  155. and r.member_id not in ('125', '127')
  156. and z.id not in (2, 3, 101)
  157. and r.state in ('completed')
  158. and r.return_at + interval 8 hour >= @startdate1
  159. and r.return_at + interval 8 hour <= @startdate2
  160.  
  161. group by month
  162. order by month desc) A
  163.  
  164. join
  165. (select
  166. rmonth2 as month2, ncars as ncars, wtotal_ncars as totalcars
  167.  
  168. from (
  169. select rmonth2,
  170. (select count(cz.id) from car_zone_logs cz, zones z
  171. where cz.zone_id=z.id and z.id not in (2,3,101,383,378,712,818,786) and date_format(date_sub(cz.log_date, interval 1 day)+interval 8 hour, "%Y-%m-%d")=mlast
  172. ) as ncars,
  173. (select count(cz.id) from car_zone_logs cz, zones z
  174. where cz.zone_id=z.id and z.id not in (2,3,101,383,378,712,818,786) and date_sub(cz.log_date, interval 1 day)+interval 8 hour >= mfirst and date_sub(cz.log_date, interval 1 day)+interval 8 hour < mlast+interval 1 day
  175. ) as wtotal_ncars
  176.  
  177. from (
  178.  
  179. select
  180. rmonth2, min(date_format(rret,"%Y-%m-%d")) as mfirst, max(date_format(rret,"%Y-%m-%d")) as mlast
  181.  
  182. from (
  183. select
  184. month(r.return_at+interval 8 hour) as rmonth2,
  185. r.start_at+interval 8 hour as rst, r.end_at+interval 8 hour as rend, r.return_at+interval 8 hour as rret
  186. from reservations r, members m, zones z
  187. where
  188. r.state='completed'
  189. and r.member_id=m.id
  190. and m.imaginary in ('normal', 'sofam')
  191. and r.start_zone_id=z.id
  192. and r.start_zone_id not in (2,3,101,383,378,697,712,818,786)
  193. and r.return_at + interval 8 hour >= @startdate1
  194. and r.return_at + interval 8 hour <= @startdate2
  195. ) temp
  196. group by rmonth2
  197. ) temp
  198. group by rmonth2
  199. ) temp
  200. group by rmonth2) B
  201.  
  202. on A.month2 = B.month2
  203.  
  204. order by A.month2 desc ;
  205.  
  206. ##Without all factors###############################################################################################################################
  207.  
  208. set @startdate1 = '2019-09-01';
  209. set @startdate2 = '2020-01-01';
  210. use socar_malaysia;
  211.  
  212. Select A.month,
  213. B.totalcars as ncars,
  214. A.nuse,
  215. A.nuse_rnd,
  216. A.res_d2d,
  217. A.res_oneway,
  218. A.ncar_used,
  219. A.nzone,
  220. A.nmem,
  221. A.rmil,
  222. A.dur,
  223. A.ad_sdur,
  224. A.revenues,
  225. A.coupons,
  226. A.nr
  227. from
  228.  
  229.  
  230. (select concat(month(convert_tz(r.return_at, '+00:00', '+8:00')),"-","2019") as month,
  231. month(convert_tz(r.return_at, '+00:00', '+8:00')) as month2,
  232. (select count( czl.id) from car_zone_logs czl where month=month(czl.log_date+interval 8 hour) and czl.zone_state='normal') as ncars,
  233. count(r.id) as nuse,
  234. count(if(r.way='round', r.id, null)) as nuse_rnd,
  235. count(if(r.way='d2d', r.id, null)) as res_d2d,
  236. count(if(r.way in('oneway','onewayReturn' ), r.id, null)) as res_oneway,
  237. count(distinct r.car_id) as ncar_used,
  238. count(distinct z.id) as nzone,
  239. count(distinct m.id) as nmem,
  240. #count(if(r.way='oneway', r.id, null)) as res_ow,
  241. sum(ra.mileage) as rmil,
  242. round(sum(timestampdiff(minute, r.start_at, r.end_at)/60),2) as dur,
  243. 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,
  244. 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,
  245. round(sum(ifnull((select sum(amount) from payments where state='normal' and reservation_id=r.id and paid_type ='coupon'),0)),2) as coupons,
  246. 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)
  247. - ifnull((select sum(amount) from payments where state='normal' and reservation_id=r.id and paid_type ='coupon'),0)),2) as nr
  248.  
  249.  
  250. from reservations r, zones z, members m, reservation_appendixes ra
  251.  
  252. Where
  253. r.start_zone_id = z.id
  254. and r.member_id = m.id
  255. and r.id = ra.reservation_id
  256. and m.imaginary in ('sofam', 'normal')
  257. and r.member_id not in ('125', '127')
  258. and z.id not in (2, 3, 101)
  259. and r.state in ('completed')
  260. and r.return_at + interval 8 hour >= @startdate1
  261. and r.return_at + interval 8 hour <= @startdate2
  262.  
  263. group by month
  264. order by month desc) A
  265.  
  266. join
  267. (select
  268. rmonth2 as month2, ncars as ncars, wtotal_ncars as totalcars
  269.  
  270. from (
  271. select rmonth2,
  272. (select count(cz.id) from car_zone_logs cz, zones z
  273. where cz.zone_id=z.id and z.id not in (2,3,101,383,378,712,818,786) and date_format(date_sub(cz.log_date, interval 1 day)+interval 8 hour, "%Y-%m-%d")=mlast
  274. ) as ncars,
  275. (select count(cz.id) from car_zone_logs cz, zones z
  276. where cz.zone_id=z.id and z.id not in (2,3,101,383,378,712,818,786) and date_sub(cz.log_date, interval 1 day)+interval 8 hour >= mfirst and date_sub(cz.log_date, interval 1 day)+interval 8 hour < mlast+interval 1 day
  277. ) as wtotal_ncars
  278.  
  279. from (
  280.  
  281. select
  282. rmonth2, min(date_format(rret,"%Y-%m-%d")) as mfirst, max(date_format(rret,"%Y-%m-%d")) as mlast
  283.  
  284. from (
  285. select
  286. month(r.return_at+interval 8 hour) as rmonth2,
  287. r.start_at+interval 8 hour as rst, r.end_at+interval 8 hour as rend, r.return_at+interval 8 hour as rret
  288. from reservations r, members m, zones z
  289. where
  290. r.state='completed'
  291. and r.member_id=m.id
  292. and m.imaginary in ('normal', 'sofam')
  293. and r.start_zone_id=z.id
  294. and r.start_zone_id not in (2,3,101,383,378,697,712,818,786)
  295. and r.return_at + interval 8 hour >= @startdate1
  296. and r.return_at + interval 8 hour <= @startdate2
  297. ) temp
  298. group by rmonth2
  299. ) temp
  300. group by rmonth2
  301. ) temp
  302. group by rmonth2) B
  303.  
  304. on A.month2 = B.month2
  305.  
  306. order by A.month2 desc
Advertisement
Add Comment
Please, Sign In to add comment