ahmedrahil786

Night time price + Hourly Reservations + DL Info = Rahil

Aug 16th, 2019 (edited)
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 30.52 KB | None | 0 0
  1. set @startdate1 = date_sub(CURDATE(), interval 2 year);
  2. set @startdate2 = date_add(CURDATE(), interval 10 day);
  3. use socar_malaysia;
  4. Select A.return_Date, Count(A.rid) as Total_resv,
  5. Sum(A._1_hr) as 1_hr,Sum(A._2_hr) as 2_hr,Sum(A._3_hr) as 3_hr,
  6. Sum(A._4_hr) as 4_hr,Sum(A._5_hr) as 5_hr,Sum(A._6_hr) as 6_hr,Sum(A._7_hr) as 7_hr, Sum(A._8_hr) as 8_hr,Sum(A._9_hr) as 9_hr,Sum(A._10_hr) as 10_hr,
  7. Sum(A._11_hr) as 11_hr,Sum(A._12_hr) as 12_hr,Sum(A._13_hr) as 13_hr,Sum(A._14_hr) as 14_hr,Sum(A._15_hr) as 15_hr,Sum(A._16_hr) as 16_hr,Sum(A._17_hr) as 17_hr,
  8. Sum(A._18_hr) as 18_hr,Sum(A._19_hr) as 19_hr,Sum(A._20_hr) as 20_hr,Sum(A._21_hr) as 21_hr,Sum(A._22_hr) as 22_hr,Sum(A._23_hr) as 23_hr, Sum(A._24_hr) as 24hr
  9. from
  10. (select distinct r.id as rid,
  11. Date(r.return_at + interval '8' hour) as return_Date ,
  12. hour(r.start_at + interval '8' hour) as Resv_start_hour,
  13. count(distinct case when hour(r.start_at + interval '8' hour) = 0 then r.id end) as _24_hr,
  14. count(distinct case when hour(r.start_at + interval '8' hour) = 1 then r.id end) as _1_hr,
  15. count(distinct case when hour(r.start_at + interval '8' hour) = 2 then r.id end) as _2_hr,
  16. count(distinct case when hour(r.start_at + interval '8' hour) = 3 then r.id end) as _3_hr,
  17. count(distinct case when hour(r.start_at + interval '8' hour) = 4 then r.id end) as _4_hr,
  18. count(distinct case when hour(r.start_at + interval '8' hour) = 5 then r.id end) as _5_hr,
  19. count(distinct case when hour(r.start_at + interval '8' hour) = 6 then r.id end) as _6_hr,
  20. count(distinct case when hour(r.start_at + interval '8' hour) = 7 then r.id end) as _7_hr,
  21. count(distinct case when hour(r.start_at + interval '8' hour) = 8 then r.id end) as _8_hr,
  22. count(distinct case when hour(r.start_at + interval '8' hour) = 9 then r.id end) as _9_hr,
  23. count(distinct case when hour(r.start_at + interval '8' hour) = 10 then r.id end) as _10_hr,
  24. count(distinct case when hour(r.start_at + interval '8' hour) = 11 then r.id end) as _11_hr,
  25. count(distinct case when hour(r.start_at + interval '8' hour) = 12 then r.id end) as _12_hr,
  26. count(distinct case when hour(r.start_at + interval '8' hour) = 13 then r.id end) as _13_hr,
  27. count(distinct case when hour(r.start_at + interval '8' hour) = 14 then r.id end) as _14_hr,
  28. count(distinct case when hour(r.start_at + interval '8' hour) = 15 then r.id end) as _15_hr,
  29. count(distinct case when hour(r.start_at + interval '8' hour) = 16 then r.id end) as _16_hr,
  30. count(distinct case when hour(r.start_at + interval '8' hour) = 17 then r.id end) as _17_hr,
  31. count(distinct case when hour(r.start_at + interval '8' hour) = 18 then r.id end) as _18_hr,
  32. count(distinct case when hour(r.start_at + interval '8' hour) = 19 then r.id end) as _19_hr,
  33. count(distinct case when hour(r.start_at + interval '8' hour) = 20 then r.id end) as _20_hr,
  34. count(distinct case when hour(r.start_at + interval '8' hour) = 21 then r.id end) as _21_hr,
  35. count(distinct case when hour(r.start_at + interval '8' hour) = 22 then r.id end) as _22_hr,
  36. count(distinct case when hour(r.start_at + interval '8' hour) = 23 then r.id end) as _23_hr
  37. from reservations r left join members m on r.member_id = m.id
  38. where r.state in ('completed','inUse')
  39. and r.return_at + interval 8 hour >= @startdate1
  40. and r.return_at + interval 8 hour <= @startdate2
  41. and m.imaginary in ('sofam', 'normal')
  42. and r.member_id not in ('125', '127')
  43. group by r.id) A
  44. group by A.return_date
  45. Order by A.return_date desc;
  46.  
  47.  
  48. set @startdate = date_sub(CURDATE(), interval 12 month);
  49. select
  50. WEEKofyear(convert_tz(r.start_at, '+00:00', '+8:00')) as start_week,
  51. DATE(convert_tz(r.start_at, '+00:00', '+8:00')) as start_date,
  52. czl.total_cars as total_cars,
  53. count(distinct r.car_id) as used_cars,
  54. count(r.id) as srid,
  55. count(distinct r.member_id) as dau,
  56. round(SUM(timestampdiff(minute, convert_tz(r.start_at, '+00:00', '+8:00'), convert_tz(r.end_at, '+00:00', '+8:00'))/60),2) as sdur,
  57. round(sum(floor(timestampdiff(minute, convert_tz(r.start_at, '+00:00', '+8:00'), convert_tz(r.end_at, '+00:00', '+8:00'))/60/24)*12+if(mod(timestampdiff(minute, convert_tz(r.start_at, '+00:00', '+8:00'), convert_tz(r.end_at, '+00:00', '+8:00'))/60,24)>=12,12,mod(timestampdiff(minute, convert_tz(r.start_at, '+00:00', '+8:00'), convert_tz(r.end_at, '+00:00', '+8:00'))/60,24))),2) as adur,
  58. round(SUM(c.rev),2) AS rev,
  59. round(SUM(c.rev - IFNULL(p.coupon,0)),2) AS net_rev
  60. from reservations r
  61. left join members on r.member_id = members.id
  62. left join coupons on r.id = coupons.reservation_id
  63. left join (select IFNULL(payments.amount,0) as coupon, payments.reservation_id as rid from payments where payments.state = 'normal' and payments.paid_type = 'coupon') as p on p.rid = r.id
  64. left join (select IFNULL(SUM(charges.amount),0) as rev, charges.reservation_id as rid from charges where charges.state = 'normal' and charges.kind IN ('rent' , 'oneway', 'd2d', 'mileage') group by charges.reservation_id) as c on r.id = c.rid
  65. left join (select log_date, COUNT(car_zone_logs.car_id) as total_cars from car_zone_logs where car_state = 'normal' and zone_state = 'normal' group by log_date) as czl on czl.log_date = DATE(convert_tz(r.start_at,'+00:00', '+8:00'))
  66. where r.state = 'completed' and members.imaginary in ('sofam', 'normal')
  67. and convert_tz(r.start_at, '+00:00', '+8:00') >= @startDate
  68. # Only Monday - Thursday
  69. and DAYNAME(convert_tz(r.start_at, '+00:00', '+8:00')) in ('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday', 'Sunday')
  70. # Bookings made between 9 PM to 8 AM
  71. and (TIME(convert_tz(r.created_at, '+00:00', '+8:00')) between '00:00:00' and '08:00:00' or TIME(convert_tz(r.created_at, '+00:00', '+8:00')) between '21:00:00' and '23:59:59')
  72. # Less than 12 hours
  73. and timestampdiff(minute, r.start_at, r.end_at)/60 < 12
  74. group by start_date
  75. order by start_date desc;
  76.  
  77. # no of members by reject reason /* */
  78.  
  79. set @start = date_sub(CURDATE(), interval 12 month);
  80. Select A.date, A.week, B.DocUploaded,B.DocApproved, A.total as rejected, C.timetaken_to_approve, A.sf_cov_bydoc, A.sf_wo_face, A.sf_wo_doc, A.dl_no_doc,
  81. A.dl_doc_blur,A.dl_expired,A.pdl,A.jpj_blacklist,A.dl_unmatched, A.pic_crop, A.under_21,
  82. A.fake_id, A.etc,A.duplicate_IC,A.IC_in_selfie_blur, A.passport_blur,A.unrelated , A.nullcases as nullcases
  83. from
  84.  
  85. (select P.date as date , weekofyear(P.date) as week,
  86. count(distinct P.mid) as total ,
  87. count(Distinct(if(rcid=1, P.mid, null))) as sf_cov_bydoc,
  88. count(Distinct(if(rcid=2, P.mid, null))) as sf_wo_face,
  89. count(Distinct(if(rcid=3, P.mid, null))) as sf_wo_doc,
  90. count(Distinct(if(rcid=4, P.mid, null))) as dl_no_doc,
  91. count(Distinct(if(rcid=5, P.mid, null))) as dl_doc_blur,
  92. count(Distinct(if(rcid=6, P.mid, null))) as dl_expired,
  93. count(Distinct(if(rcid=7, P.mid, null))) as pdl,
  94. count(Distinct(if(rcid=8, P.mid, null))) as jpj_blacklist,
  95. count(Distinct(if(rcid=9, P.mid, null))) as dl_unmatched,
  96. count(Distinct(if(rcid=10, P.mid, null))) as pic_crop,
  97. count(Distinct(if(rcid=11, P.mid, null))) as under_21,
  98. count(Distinct(if(rcid=12, P.mid, null))) as fake_id,
  99. count(Distinct(if(rcid=13, P.mid, null))) as etc,
  100. count(Distinct(if(rcid=14, P.mid, null))) as duplicate_IC,
  101. count(Distinct(if(rcid=15, P.mid, null))) as IC_in_selfie_blur,
  102. count(Distinct(if(rcid=16, P.mid, null))) as passport_blur,
  103. count(Distinct(if(rcid=17, P.mid, null))) as unrelated,
  104. count(Distinct(if(rcid is null, P.mid, null))) as nullcases
  105. from
  106. (select C.mid as mid , C.reject_category_id as rcid, C.date as date
  107. from
  108. (select distinct dl.member_id as mid, max(dl.id) as dlid from driver_licenses dl
  109. where date(dl.created_at + interval '8' hour) >= @start
  110. group by 1) B
  111. left join
  112. (select dl.member_id as mid, dl.id as dlid,date(dl.created_at + interval 8 hour) as date,
  113. dl.state as state, dl.reject_category_id as reject_category_id , dlc.name as reject_category
  114. from driver_licenses dl
  115. left join driver_license_reject_categories dlc on dlc.id = dl.reject_category_id
  116. group by 1,2) C
  117. on B.dlid = C.dlid
  118. where C.state in ('reject')
  119. group by 1
  120. order by 2 desc )P
  121. group by 1
  122. order by 1 desc) A
  123. left join
  124. (select
  125. Date(dl.created_at + interval '8' hour) as Date,
  126. weekofyear(dl.created_at + interval 8 hour) as week,
  127. count(distinct case when dl.state not in ('noInput','null') then dl.member_id end) DocUploaded,
  128. count(distinct case when dl.state = 'approved' then dl.member_id end) DocApproved
  129. from driver_licenses dl
  130. join members m
  131. on m.id = dl.member_id
  132. where m.imaginary = 'normal'
  133. group by 1) B
  134. on A.date = B.date
  135. left join
  136. (select A.date as date, round(avg(A.average_time_for_Approval),0) as timetaken_to_approve from
  137. (select distinct dl.member_id,
  138. Date(dl.created_at + interval '8' hour) as Date,
  139. (timestampdiff(minute,dl.created_at + interval '8' hour,dl.approved_at + interval '8' hour)) as average_time_for_Approval
  140. from driver_licenses dl
  141. where dl.state = 'approved'
  142. and (timestampdiff(minute,dl.created_at + interval '8' hour,dl.approved_at + interval '8' hour)) <= '240'
  143. order by 1 desc) A
  144. group by 1
  145. order by 1 desc) C
  146. on C.date = A.date
  147. where A.date >= @start ;
  148.  
  149.  
  150.  
  151.  
  152.  
  153. ######## weekly Revenue Related Info
  154.  
  155. Select A.date as date,
  156. sum(A.Basic_SCDW) as Basic_SCDW,
  157. sum(A.Standard_SCDW) as Standard_SCDW,
  158. sum(A.Premium_SCDW) as Premium_SCDW,
  159. Round(sum(A.Total_SCDW_amount),2) as Total_SCDW_amount,
  160. Round(sum(A.Basic_SCDW_amount),2) as Basic_SCDW_amount,
  161. Round(sum(A.Standard_SCDW_amount),2) as Standard_SCDW_amount,
  162. Round(sum(A.Premium_SCDW_amount),2) as Premium_SCDW_amount,
  163. Round(sum(A.Basic_SCDW_amount)/sum(A.Basic_SCDW),2) as BASIC_SCDW_per_resv,
  164. ROUND(sum(A.Standard_SCDW_amount)/sum(A.Standard_SCDW),2) as Standard_SCDW_per_resv,
  165. ROUND(sum(A.Premium_SCDW_amount)/sum(A.Premium_SCDW),2) Premium_SCDW_per_resv
  166.  
  167. from
  168. (select c.reservation_id as rid ,
  169. Date(r.start_at + interval 8 hour) as date,
  170. c.member_id as mid,
  171. cm.meta as meta,
  172. count(distinct case when cm.meta LIKE '%Basic SCDW%' then cm.charge_id end) as Basic_SCDW,
  173. count(distinct case when cm.meta LIKE '%Standard SCDW%' then cm.charge_id end) as Standard_SCDW,
  174. count(distinct case when cm.meta LIKE '%Premium SCDW%' then cm.charge_id end) as Premium_SCDW,
  175. Sum(distinct case when cm.meta LIKE '%SCDW%' then c.amount end) as Total_SCDW_amount,
  176. Sum(distinct case when cm.meta LIKE '%Basic SCDW%' then c.amount end) as Basic_SCDW_amount,
  177. Sum(distinct case when cm.meta LIKE '%Standard SCDW%' then c.amount end) as Standard_SCDW_amount,
  178. Sum(distinct case when cm.meta LIKE '%Premium SCDW%' then c.amount end) as Premium_SCDW_amount
  179. from charges c
  180. left join charge_meta cm on cm.charge_id = c.id
  181. left join reservations r on r.id = c.reservation_id
  182. left join members m on m.id = c.member_id
  183. where c.kind = 'insurance'
  184. and m.imaginary in ('sofam', 'normal')
  185. and r.member_id not in ('125', '127')
  186. and r.state in ('completed','inUse')
  187. and c.state = 'normal'
  188. group by 1 ) A
  189. Where A.date is not null
  190. group by 1
  191. order by 1 desc
  192. ;
  193.  
  194. ###### Reservations and Net Revenues by duration length
  195.  
  196. set @startdate1 = date_sub(CURDATE(), interval 6 month);
  197. set @startdate2 = date_add(CURDATE(), interval 10 day);
  198. use socar_malaysia;
  199.  
  200. select
  201. L.return_Date,
  202. count(L.rid) as total,
  203. sum(L.net_rev) as net_rev,
  204. count(if(L.duration < 1, L.rid, null)) as 00_01_hr,
  205. count(if(L.duration >= 1 and L.duration < 2, L.rid, null)) as 01_02_hr,
  206. count(if(L.duration >= 2 and L.duration < 3, L.rid, null)) as 02_03_hr,
  207. count(if(L.duration >= 3 and L.duration < 4, L.rid, null)) as 03_04_hr,
  208. count(if(L.duration >= 4 and L.duration < 5, L.rid, null)) as 04_05_hr,
  209. count(if(L.duration >= 5 and L.duration < 6, L.rid, null)) as 05_06_hr,
  210. count(if(L.duration >= 6 and L.duration < 7, L.rid, null)) as 06_07_hr,
  211. count(if(L.duration >= 7 and L.duration < 8, L.rid, null)) as 07_08_hr,
  212. count(if(L.duration >= 8 and L.duration < 9, L.rid, null)) as 08_09_hr,
  213. count(if(L.duration >= 9 and L.duration < 10, L.rid, null)) as 09_10_hr,
  214. count(if(L.duration >= 10 and L.duration < 11, L.rid, null)) as 10_11_hr,
  215. count(if(L.duration >= 11 and L.duration < 12, L.rid, null)) as 11_12_hr,
  216. count(if(L.duration >= 12 and L.duration < 24, L.rid, null)) as 12_24_hr,
  217. count(if(L.duration >= 24 and L.duration < 48, L.rid, null)) as 24_48_hr,
  218. count(if(L.duration >= 48, L.rid, null)) as 48__hr ,
  219. ##### Net_rev
  220. Sum(if(L.duration < 1, L.net_rev, null)) as 00_01_hr_amount,
  221. Sum(if(L.duration >= 1 and L.duration < 2, L.net_rev, null)) as 01_02_hr_amount,
  222. Sum(if(L.duration >= 2 and L.duration < 3, L.net_rev, null)) as 02_03_hr_amount,
  223. Sum(if(L.duration >= 3 and L.duration < 4, L.net_rev, null)) as 03_04_hr_amount,
  224. Sum(if(L.duration >= 4 and L.duration < 5, L.net_rev, null)) as 04_05_hr_amount,
  225. Sum(if(L.duration >= 5 and L.duration < 6, L.net_rev, null)) as 05_06_hr_amount,
  226. Sum(if(L.duration >= 6 and L.duration < 7, L.net_rev, null)) as 06_07_hr_amount,
  227. Sum(if(L.duration >= 7 and L.duration < 8, L.net_rev, null)) as 07_08_hr_amount,
  228. Sum(if(L.duration >= 8 and L.duration < 9, L.net_rev, null)) as 08_09_hr_amount,
  229. Sum(if(L.duration >= 9 and L.duration < 10, L.net_rev, null)) as 09_10_hr_amount,
  230. Sum(if(L.duration >= 10 and L.duration < 11, L.net_rev, null)) as 10_11_hr_amount,
  231. Sum(if(L.duration >= 11 and L.duration < 12, L.net_rev, null)) as 11_12_hr_amount,
  232. Sum(if(L.duration >= 12 and L.duration < 24, L.net_rev, null)) as 12_24_hr_amount,
  233. Sum(if(L.duration >= 24 and L.duration < 48, L.net_rev, null)) as 24_48_hr_amount,
  234. Sum(if(L.duration >= 48, L.net_rev, null)) as 48__hr_amount
  235.  
  236. from
  237.  
  238. (select A.return_date as return_date, A.rid2 as rid, A.dur as duration, A.ad_sdur as ad_sdur, IFNULL(E.charges,0) as gross_rev,
  239. IFNULL(F.coupon_Spent,0) as Coupon_Spent,
  240. round((IFNULL(E.charges,0) - IFNULL(F.coupon_Spent,0)),2) as net_rev from
  241. (select
  242. distinct r.id as rid2, r.member_id as mid,
  243. round(sum(timestampdiff(minute, r.start_at, r.end_at)/60),2) as dur,
  244. 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,
  245. Date(r.return_at + interval '8' hour) as return_Date
  246. from reservations r left join members m on r.member_id = m.id
  247. left join reservation_appendixes ra on ra.reservation_id = r.id
  248. where r.state in ('completed')
  249. and r.return_at + interval 8 hour >= @startdate1
  250. and r.return_at + interval 8 hour <= @startdate2
  251. and m.imaginary in ('sofam', 'normal')
  252. and r.member_id not in ('125', '127')
  253. group by rid2) A
  254. left join
  255. (select c.reservation_id as rid, sum(c.amount) as charges from charges c
  256. where c.state='normal' and c.kind in ('rent','oneway','d2d','mileage','insurance')
  257. group by rid) E on A.rid2 = E.rid
  258. left join
  259. (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
  260. where p.state = 'normal' and p.paid_type = 'coupon'
  261. group by p.reservation_id) F on A.rid2 = F.rid) L
  262. group by 1
  263. order by 1 desc;
  264.  
  265.  
  266. ############# Daily Query for Payment Provider
  267.  
  268.  
  269.  
  270. set @startdate1 = date_sub(CURDATE(), interval 6 month);
  271. set @startdate2 = date_add(CURDATE(), interval 10 day);
  272. use socar_malaysia;
  273. Select
  274. date(A.start_Date) as month,
  275. year(A.start_Date) as year,
  276. Count(Distinct A.rid) as Total_attempts,
  277. count(distinct case when A.provider = 'mcp' then A.rid end) as Total_MCP,
  278. count(distinct case when A.provider = 'braintree' then A.rid end) as Total_braintree,
  279. count(distinct case when A.provider = 'grabpay' then A.rid end) as Total_grabpay,
  280. count(distinct case when A.provider = 'stripe' then A.rid end) as Total_stripe,
  281. count(distinct case when A.provider = 'mcp' and A.state not in ('fail') then A.rid end) as MCP_Good,
  282. count(distinct case when A.provider = 'braintree' and A.state not in ('fail') then A.rid end) as braintree_Good,
  283. count(distinct case when A.provider = 'grabpay' and A.state not in ('fail') then A.rid end) as grabpay_Good,
  284. count(distinct case when A.provider = 'stripe' and A.state not in ('fail') then A.rid end) as stripe_Good,
  285. count(distinct case when A.provider = 'mcp' and A.state in ('fail') then A.rid end) as MCP_failed,
  286. count(distinct case when A.provider = 'braintree' and A.state in ('fail') then A.rid end) as braintree_failed,
  287. count(distinct case when A.provider = 'grabpay' and A.state in ('fail') then A.rid end) as grabpay_failed,
  288. count(distinct case when A.provider = 'stripe' and A.state in ('fail') then A.rid end) as stripe_failed
  289. from
  290. (select
  291. distinct r.id as rid, r.member_id as mid, r.state as state, p.paid_type as type, pm.provider as provider,
  292. Date(r.created_at + interval '8' hour) as start_Date, p.paygate as provider_2
  293. from reservations r left join members m on r.member_id = m.id
  294. LEFT JOIN payment_methods pm ON r.payment_method_id = pm.id
  295. left join payments p on p.reservation_id = r.id
  296. where r.created_at + interval 8 hour >= @startdate1
  297. and r.created_at + interval 8 hour <= @startdate2
  298. and r.member_id not in ('125', '127')
  299. group by rid, type) A
  300. group by 1
  301. order by 1 desc;
  302.  
  303. ##### Coupons info W.O.W
  304.  
  305. set @startdate1 = date_sub(CURDATE(), interval 6 week);
  306. set @startdate2 = date_add(CURDATE(), interval 10 day);
  307. use socar_malaysia;
  308. use socar_malaysia;
  309. Select I.week as week,I.coupon as coupon, I.cpid as cpid, count(distinct case when I.mid is not null then I.mid end) as Mau,count(distinct case when I.rid is not null then I.rid end) as total_resv,
  310. Sum(I.dur) as dur, Sum(I.ad_sdur) as ad_sdur, Sum(nuse_rnd) as nuse_rnd, Sum(res_d2d) as res_d2d,sum(res_oneway) as res_oneway,
  311. sum(gross_rev) as gross_rev,sum(Coupon_Spent) as Coupon_Spent,sum(net_rev) as net_rev,sum(mft) as mft from
  312. (select A.week as week, A.mid as mid, A.rid2 as rid, A.coupon as coupon,A.cpid as cpid, A.dur as dur, A.ad_sdur as ad_sdur,
  313. A.nuse_rnd as nuse_rnd,A.res_d2d as res_d2d,A.res_oneway as res_oneway ,IFNULL(E.charges,0) as gross_rev, IFNULL(F.coupon_Spent,0) as Coupon_Spent,
  314. round((IFNULL(E.charges,0) - IFNULL(F.coupon_Spent,0)),2) as net_rev,if(rid2=firstRes,1,0) as mft from
  315. (select
  316. distinct r.id as rid2, r.member_id as mid,IFNULL(co.coupon_policy_id,"none") as cpid,IFNULL(co.comment,"none") as coupon,
  317. round(sum(timestampdiff(minute, r.start_at, r.end_at)/60),2) as dur,
  318. 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,
  319. count(if(r.way='round', r.id, null)) as nuse_rnd,
  320. count(if(r.way='d2d', r.id, null)) as res_d2d,
  321. count(if(r.way in('oneway','onewayReturn' ), r.id, null)) as res_oneway,
  322. Date(r.return_at + interval '8' hour) as return_Date,
  323. weekofyear(r.return_at + interval '8' hour) as week,
  324. Year(r.return_at + interval '8' hour) as Year,
  325. ifnull((select min(id) from reservations where state='completed' and r.member_id=member_id group by member_id),0) as firstRes
  326. from reservations r left join members m on r.member_id = m.id
  327. left join coupons co on co.reservation_id = r.id
  328. where r.state in ('completed','inUse','reserved')
  329. and r.return_at + interval 8 hour >= @startdate1
  330. and r.return_at + interval 8 hour <= @startdate2
  331. and m.imaginary in ('sofam', 'normal')
  332. and r.member_id not in ('125', '127')
  333. and (YEAR(r.start_at) - YEAR(m.birthday)) < 100
  334. group by rid2) A
  335. left join
  336. (select c.reservation_id as rid, sum(c.amount) as charges from charges c
  337. where c.state='normal' and c.kind in ('rent','oneway','d2d','mileage')
  338. group by rid) E on A.rid2 = E.rid
  339. left join
  340. (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
  341. where p.state = 'normal' and p.paid_type = 'coupon'
  342. group by p.reservation_id) F on A.rid2 = F.rid) I
  343. where I.coupon not in ('none')
  344. and I.week > Weekofyear(now()) - 7
  345. Group by I.week, I.cpid
  346. order by I.week, gross_rev desc;
  347.  
  348. #################################################################################Fuel Product Dashboard
  349.  
  350. set @startdate1 = date_sub(CURDATE(), interval 3 month);
  351. set @startdate2 = date_add(CURDATE(), interval 10 day);
  352. use socar_malaysia;
  353. select I.date as date, Q.cars as cars, count(I.rid) as resv,
  354. sum(I.gross_rev) as gross, sum(I.Coupon_Spent) as Coupon_Spent, Sum(I.net_rev) as net_rev, round(sum(I.mileage),2) as mileage,
  355. round(sum(I.mileage)+Sum(I.fuel),2) as Total_mileage_fuel , Round(Sum(I.fuel),2) as fuel, round(count(I.rid)/Q.cars,2) as RESPCPD,round(sum(I.dur)/Q.cars,2) as DURPCPD,round(sum(I.ad_sdur)/Q.cars,2) as AD_DURPCPD,
  356. round(sum(I.fuel_optedin)*100/count(I.rid),2) as opt_in_PCT,Round(sum(I.mileage_paid_resv)*100/count(I.rid),2) as mileage_paid_PCT,
  357. round((sum(I.mileage)+Sum(I.fuel))/count(I.rid),2) as Total_mileage_fuel_per_res , Round(Sum(I.fuel)/count(I.rid),2) as fuel_per_res, round(sum(I.gross_rev)/Q.cars,2) as Gross_REVPCPD,
  358. round(Sum(I.net_rev)/Q.cars,2) as NRPCPD, Round(Sum(I.fuel)/Q.cars,2) as fuel_PCPD, round(sum(I.mileage)/Q.cars,2) as mileage_PCPD, round((sum(I.mileage)+Sum(I.fuel))/Q.cars,2) as Total_mileage_fuel_DCPD,
  359. sum(I.distance) as distance, ROUND(round((sum(I.mileage)+Sum(I.fuel)),2)/sum(I.distance),4) EMF_P_FP_P_KM
  360. from
  361. (select A.return_date as Date, A.rid2 as rid, A.dur as dur, A.ad_sdur as ad_sdur, A.distance as distance,
  362. IFNULL(G.mileage,0) as mileage , Ifnull(H.fuel,0) as fuel ,IFNULL(G.excess_km,0) as excess_km ,
  363. IFNULL(H.fuel_optedin,0) as fuel_optedin ,
  364. IFNULL(G.mileage_paid,0) as mileage_paid_resv,
  365. IFNULL(E.charges,0) as gross_rev,
  366. IFNULL(F.coupon_Spent,0) as Coupon_Spent,
  367. round((IFNULL(E.charges,0) - IFNULL(F.coupon_Spent,0)),2) as net_rev from
  368. (select
  369. distinct r.id as rid2, r.member_id as mid, IFNULL(sum(ra.mileage),0) as distance,
  370. round(sum(timestampdiff(minute, r.start_at, r.end_at)/60),2) as dur,
  371. 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,
  372. Date(r.return_at + interval '8' hour) as return_Date
  373. from reservations r left join members m on r.member_id = m.id
  374. left join reservation_appendixes ra on ra.reservation_id = r.id
  375. where r.state in ('completed')
  376. and r.return_at + interval 8 hour >= @startdate1
  377. and r.return_at + interval 8 hour <= @startdate2
  378. and m.imaginary in ('sofam', 'normal')
  379. and r.member_id not in ('125', '127')
  380. group by rid2) A
  381. left join
  382. (select c.reservation_id as rid, sum(c.amount) as mileage, count(distinct c.reservation_id) as mileage_paid, SUBSTRING_INDEX(SUBSTRING_INDEX(c.etc, 'excess: ', -1), '= distance', 1) as excess_km from charges c
  383. where c.state='normal' and c.kind in ('mileage')
  384. group by rid) G on A.rid2 = G.rid
  385. left join
  386. (select c.reservation_id as rid, sum(c.amount) as fuel, count(distinct c.reservation_id) as fuel_optedin from charges c
  387. where c.state='normal' and c.kind in ('fuel')
  388. group by rid) H on A.rid2 = H.rid
  389. left join
  390. (select c.reservation_id as rid, sum(c.amount) as charges from charges c
  391. where c.state='normal' and c.kind in ('rent','oneway','d2d','mileage','insurance','chauffeur','fuel')
  392. group by rid) E on A.rid2 = E.rid
  393. left join
  394. (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
  395. where p.state = 'normal' and p.paid_type = 'coupon'
  396. group by p.reservation_id) F on A.rid2 = F.rid) I
  397. left join
  398. (select date(czl.log_date + interval 8 hour ) as date, count(czl.id) as cars
  399. from car_zone_logs czl
  400. where czl.zone_state='normal'
  401. and czl.log_date + interval 8 hour >= @startdate1
  402. and czl.log_date + interval 8 hour <= @startdate2
  403. group by 1
  404. order by 1 desc) Q
  405. on Q.Date = I.Date
  406. group by 1
  407. order by 1 desc ;
  408.  
  409. ########################## NRPCPD , RESPCPD , DURPCPD by City
  410.  
  411. set @startdate1 = date_sub(CURDATE(), interval 2 month);
  412. set @startdate2 = date_add(CURDATE(), interval 10 day);
  413. use socar_malaysia;
  414. Select I.date as date, I.city as city, weekofyear(I.date) as week, year(I.date) as year,
  415. (Q.cars - L.Common_block_Cars) as cars, Round(I.net_rev,2) as Net_rev, ROUND(I.Total_reservations/(Q.cars-L.Common_block_Cars),2) as RESPCPD,
  416. ROUND(I.gross_rev/(Q.cars-L.Common_block_Cars),2) as Gross_RevPCPD, ROUND(I.Coupon_Spent/(Q.cars-L.Common_block_Cars),2) as Coupon_RevPCPD, ROUND(I.net_rev/(Q.cars-L.Common_block_Cars),2) as net_RevPCPD,
  417. Round(I.Dur/(Q.cars-L.Common_block_Cars),2) as DURPCPD, Round(I.ad_sdur/(Q.cars-L.Common_block_Cars),2) as AD_DURPCPD,
  418. Round(I.net_rev/I.Total_reservations,2) Net_BS, Round(I.net_rev/I.ad_sdur,2) NRPH
  419. from
  420. (select date(A.return_date) as date, A.city as city, count(A.rid2) as Total_reservations,
  421. count(distinct A.mid) as Total_Unique_Members, sum(A.distance) as distance, sum(A.dur) as dur, sum(A.ad_sdur) as ad_sdur,
  422. IFNULL(Sum(E.charges),0) as gross_rev,
  423. IFNULL(sum(F.coupon_Spent),0) as Coupon_Spent,
  424. round((IFNULL(sum(E.charges),0) - IFNULL(sum(F.coupon_Spent),0)),2) as net_rev from
  425. (select
  426. distinct r.id as rid2, r.member_id as mid, r.way as way, z.city as city,
  427. IFNULL(sum(ra.mileage),0) as distance,
  428. round(sum(timestampdiff(minute, r.start_at, r.end_at)/60),2) as dur,
  429. 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,
  430. Date(r.return_at + interval '8' hour) as return_Date,
  431. month(r.return_at + interval '8' hour) as month
  432. from reservations r left join members m on r.member_id = m.id
  433. left join reservation_appendixes ra on ra.reservation_id = r.id
  434. left join zones z on z.id = r.start_zone_id
  435. where r.state in ('completed')
  436. and r.return_at + interval 8 hour >= @startdate1
  437. and r.return_at + interval 8 hour <= @startdate2
  438. and m.imaginary in ('sofam', 'normal')
  439. and r.member_id not in ('125', '127')
  440. and (YEAR(r.start_at) - YEAR(m.birthday)) < 100
  441. group by rid2) A
  442. left join
  443. (select c.reservation_id as rid, sum(c.amount) as charges from charges c
  444. where c.state='normal' and c.kind in ('rent','oneway','d2d','mileage')
  445. group by rid) E on A.rid2 = E.rid
  446. left join
  447. (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
  448. where p.state = 'normal' and p.paid_type = 'coupon'
  449. group by p.reservation_id) F on A.rid2 = F.rid
  450. group by 1,2 ) I
  451. left join
  452. (select date(czl.log_date + interval 8 hour ) as date, z.city as city, count(czl.id) as cars from car_zone_logs czl left join cars cr on cr.id = czl.car_id left join zones z on z.id = czl.zone_id
  453. left join car_classes cc on cc.id = cr.car_class_id where czl.zone_state='normal'
  454. and czl.log_date + interval 8 hour >= @startdate1
  455. and czl.log_date + interval 8 hour <= @startdate2
  456. group by 1,2
  457. order by 1,2 desc) Q
  458. on Q.date = I.date and Q.city = I.city
  459. left join
  460. (select date, city, count(Distinct(if(rst<=date, if(rend>=date,cid,null),null))) as Common_block_Cars
  461. from
  462. (select date_format(r.return_at, "%Y-%m-%d") as date
  463. from reservations r
  464. where r.start_at + interval 8 hour >= @startdate1
  465. and r.start_at + interval 8 hour <= @startdate2
  466. group by date)d
  467. join
  468. (select r.id as rid, r.car_id as cid, r.state, z.city as city,
  469. date_format(r.start_at+interval 8 hour, "%Y-%m-%d") as rst, date_format(r.return_at+interval 8 hour,"%Y-%m-%d") as rrt, date_format(r.end_at+interval 8 hour,"%Y-%m-%d") as rend
  470. , timestampdiff(day, r.start_at, r.end_at) as dur
  471. from reservations r ,zones z
  472. where member_id=125
  473. and r.start_zone_id = z.id
  474. and r.start_at + interval 8 hour >= @startdate1
  475. and r.start_at + interval 8 hour <= @startdate2
  476. and r.state in ('completed', 'inuse')
  477. )r
  478. where dur >=7
  479. group by date ,city) L
  480. on L.city = I.city and L.date = I.date
  481. Where L.city in ('JB','PG','KL','SL')
  482. group by 1,2
  483. order by 1 desc ;
  484.  
  485. ####################### Simedarby cars - Partnership
  486.  
  487.  
  488. set @startdate1 = date_sub(CURDATE(), interval 2 month);
  489. set @startdate2 = date_add(CURDATE(), interval 10 day);
  490. use socar_malaysia;
  491. Select I.week as week, I.car_name as car_name, I.maker as maker, Round(I.net_rev,2) as Net_rev,
  492. ROUND(I.Total_reservations/Q.cars,2) as RESPCPD, ROUND(I.net_rev/Q.cars,2) as net_RevPCPD, Round(I.ad_sdur/Q.cars,2) as AD_DURPCPD from
  493. (select weekofyear(A.return_date) as week, A.car_name as car_name, A.maker as maker, count(A.rid2) as Total_reservations,
  494. count(distinct A.mid) as Total_Unique_Members, sum(A.distance) as distance, sum(A.dur) as dur, sum(A.ad_sdur) as ad_sdur,
  495. IFNULL(Sum(E.charges),0) as gross_rev,
  496. IFNULL(sum(F.coupon_Spent),0) as Coupon_Spent,
  497. round((IFNULL(sum(E.charges),0) - IFNULL(sum(F.coupon_Spent),0)),2) as net_rev from
  498. (select
  499. distinct r.id as rid2, r.member_id as mid, r.way as way, cc.car_name as car_name, cc.maker as maker, cc.id as ccid, cr.id as crid,
  500. IFNULL(sum(ra.mileage),0) as distance,
  501. round(sum(timestampdiff(minute, r.start_at, r.end_at)/60),2) as dur,
  502. 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,
  503. Date(r.return_at + interval '8' hour) as return_Date,
  504. month(r.return_at + interval '8' hour) as month
  505. from reservations r left join members m on r.member_id = m.id
  506. left join reservation_appendixes ra on ra.reservation_id = r.id
  507. left join cars cr on cr.id = r.car_id
  508. left join car_classes cc on cc.id = cr.car_class_id
  509. where r.state in ('completed')
  510. and r.return_at + interval 8 hour >= @startdate1
  511. and r.return_at + interval 8 hour <= @startdate2
  512. and m.imaginary in ('sofam', 'normal')
  513. and r.member_id not in ('125', '127')
  514. and (YEAR(r.start_at) - YEAR(m.birthday)) < 100
  515. and (cc.id in (29,30,31) or cr.id in ('2262'))
  516. group by 1) A
  517. left join
  518. (select c.reservation_id as rid, sum(c.amount) as charges from charges c
  519. where c.state='normal' and c.kind in ('rent','oneway','d2d','mileage','insurance','chauffeur','fuel')
  520. group by rid) E on A.rid2 = E.rid
  521. left join
  522. (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
  523. where p.state = 'normal' and p.paid_type = 'coupon'
  524. group by p.reservation_id) F on A.rid2 = F.rid
  525. group by 1,2 ) I
  526. left join
  527. (select weekofyear(czl.log_date + interval 8 hour ) as week, cc.car_name as car_name, count(czl.id) as cars from car_zone_logs czl left join cars cr on cr.id = czl.car_id
  528. left join car_classes cc on cc.id = cr.car_class_id where czl.zone_state='normal'
  529. and czl.log_date + interval 8 hour >= @startdate1
  530. and czl.log_date + interval 8 hour <= @startdate2
  531. and (cc.id in (29,30,31) or cr.id in ('2262'))
  532. group by 1,2
  533. order by 1,2 desc) Q
  534. on Q.week = I.week and Q.car_name = I.car_name
  535. group by 1,2
  536. order by 1 desc ;
Add Comment
Please, Sign In to add comment