ahmedrahil786

Temp - KID (Revenues Both) - 6 th Month

Jan 12th, 2020 (edited)
579
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.98 KB | None | 0 0
  1. use socar_malaysia;
  2. set @start := '2020-10-01 00:00';
  3.  
  4.  
  5. select
  6. A.Week,
  7. A.signups,
  8. B.DocUploaded,
  9. B.DocApproved,
  10. D.TotalMFTs,
  11. C.ActiveMembers,
  12. C.Bookings as rentals,
  13. C.ridelength
  14.  
  15. from (select
  16. WEEKOFYEAR(m.created_at + interval '8' hour) as Week,
  17. count(distinct m.id) as signups
  18. from members m
  19. left outer join reservations r on
  20. r.member_id = m.id
  21. where m.created_at + interval '8' hour >= @start
  22. and m.imaginary = 'normal'
  23. group by 1 ) A
  24.  
  25. join (select
  26. WEEKOFYEAR(dl.created_at + interval '8' hour) as Week,
  27. count(distinct case when dl.state not like 'noInput' then dl.member_id end) DocUploaded,
  28. count(distinct case when dl.state = 'approved' then dl.member_id end) DocApproved,
  29. count(distinct case when dl.gender = 'man' then dl.member_id end) MaleApproved,
  30. count(distinct case when dl.gender = 'woman' then dl.member_id end) FemaleApproved
  31. from driver_licenses dl
  32. join members m
  33. on m.id = dl.member_id
  34. where dl.created_at + interval '8' hour >= @start
  35. and m.imaginary = 'normal'
  36. group by 1) B
  37. on B.Week = A.Week
  38.  
  39. join (select
  40. weekofyear(r.return_at + interval '8' hour) as week,
  41. count(distinct case when r.state = 'completed' then r.member_id end) as ActiveMembers,
  42. count(distinct case when r.state = 'completed' then r.id end) as Bookings,
  43. round(sum(timestampdiff(minute, r.start_at, r.end_at)/60),2) as ridelength,
  44. sum(G.amount) as Charges
  45. from reservations r
  46. left outer join (select
  47. ch.reservation_id as rid,
  48. sum(ch.amount) as amount
  49. from payments ch
  50. group by 1
  51. ) G on G.rid = r.id
  52. left join members m on m.id = r.member_id
  53. where r.start_at + interval '8' hour >= @start
  54. and m.imaginary in ('sofam', 'normal')
  55. and r.member_id not in ('125', '127')
  56. and r.start_zone_id not in (2, 3, 101)
  57. and r.state = 'completed'
  58. group by 1) C on
  59. C.Week = B.Week
  60.  
  61. join (
  62. select
  63. WEEKOFYEAR(c.created_at + interval '8' hour) as Week,
  64. count(distinct case when c.kind = 'subscriptionFee' then c.member_id end) as TotalMFTs
  65. from charges c
  66. join members m on m.id = c.member_id
  67. where c.created_at + interval '8' hour >= @start
  68. and m.imaginary = 'normal'
  69. group by 1) D on
  70. D.Week = C.Week
  71. group by 1
  72. order by 1 asc;
  73.  
  74. Select
  75. weekofyear(t1.date) as week,
  76. t1.date,
  77. t1.ncars,
  78. t1.ncars - COALESCE(t2.ncars, t1.ncars) AS diff
  79. from
  80.  
  81. (select
  82. Date(date_sub(czl.log_date , interval 1 day)) as date,
  83. weekday(date_sub(czl.log_date , interval 1 day)) as weekday,
  84. count(distinct czl.car_id ) as ncars
  85. from car_zone_logs czl
  86. where czl.log_date + interval '8' hour >= @start
  87. and czl.car_state = 'normal'
  88. and czl.zone_state = 'normal'
  89. and czl.csa_state = 'normal'
  90. and weekday(date_sub(czl.log_date , interval 1 day)) = 6
  91. group by 1) t1
  92.  
  93. left join
  94.  
  95. (select
  96. Date(date_add(czl.log_date , interval 7 day)) as date,
  97. weekday(date_add(czl.log_date , interval 7 day)) as weekday,
  98. count(distinct czl.car_id ) as ncars
  99. from car_zone_logs czl
  100. where czl.log_date + interval '8' hour >= @start
  101. and czl.car_state = 'normal'
  102. and czl.zone_state = 'normal'
  103. and czl.csa_state = 'normal'
  104. and weekday(date_sub(czl.log_date , interval 1 day)) = 6
  105. group by 1) t2
  106.  
  107. on t1.date = t2.date - 1
  108.  
  109. order by t1.date ;
  110.  
  111.  
  112. set @start2 := '2020-10-01 00:00';
  113.  
  114. select
  115. A.month,
  116. A.signups,
  117. B.DocUploaded,
  118. B.DocApproved,
  119. D.TotalMFTs,
  120. C.ActiveMembers,
  121. C.Bookings as rentals,
  122. C.ridelength
  123. from (select
  124. month(m.created_at + interval '8' hour) as month,
  125. count(distinct m.id) as signups
  126. from members m
  127. left outer join reservations r on r.member_id = m.id
  128. where m.created_at + interval '8' hour >= @start2
  129. and m.imaginary = 'normal'
  130. group by 1 ) A
  131.  
  132. join (select
  133. month(dl.created_at + interval '8' hour) as month,
  134. count(distinct case when dl.state not like 'noInput' then dl.member_id end) DocUploaded,
  135. count(distinct case when dl.state = 'approved' then dl.member_id end) DocApproved,
  136. count(distinct case when dl.gender = 'man' then dl.member_id end) MaleApproved,
  137. count(distinct case when dl.gender = 'woman' then dl.member_id end) FemaleApproved
  138. from driver_licenses dl
  139. join members m on m.id = dl.member_id
  140. where dl.created_at + interval '8' hour >= @start2
  141. and m.imaginary = 'normal'
  142. group by 1) B
  143. on B.month = A.month
  144.  
  145. join (select
  146. month(r.return_at + interval '8' hour) as month,
  147. count(distinct case when r.state = 'completed' then r.member_id end) as ActiveMembers,
  148. count(distinct case when r.state = 'completed' then r.id end) as Bookings,
  149. round(sum(timestampdiff(minute, r.start_at, r.end_at)/60),2) as ridelength,
  150. sum(G.amount) as Charges
  151. from reservations r
  152. left outer join (select
  153. ch.reservation_id as rid,
  154. sum(ch.amount) as amount
  155. from payments ch
  156. group by 1
  157. ) G on G.rid = r.id
  158. left join members m on m.id = r.member_id
  159. where r.start_at + interval '8' hour >= @start2
  160. and m.imaginary in ('sofam', 'normal')
  161. and r.member_id not in ('125', '127')
  162. and r.start_zone_id not in (2, 3, 101)
  163. and r.state = 'completed'
  164. group by 1) C on
  165. C.month = B.month
  166.  
  167. join (
  168. select
  169. month(c.created_at + interval '8' hour) as month,
  170. count(distinct case when c.kind = 'subscriptionFee' then c.member_id end) as TotalMFTs
  171. from charges c
  172. join members m
  173. on m.id = c.member_id
  174. where c.created_at + interval '8' hour >= @start2
  175. and m.imaginary = 'normal'
  176. group by 1) D on
  177. D.month = C.month
  178.  
  179. group by 1
  180.  
  181. order by 1 asc;
  182.  
  183. set @start := '2020-10-01 00:00';
  184.  
  185. Select
  186. t1.date,
  187. t1.ncars,
  188. t1.ncars - COALESCE(t2.ncars, t1.ncars) AS diff_cars,
  189. t1.nzones,
  190. t1.nzones - COALESCE(t2.nzones, t1.nzones) AS diff_zones,
  191. P.cars as avg_cars
  192.  
  193. from
  194.  
  195. (select
  196. Month(date_sub(czl.log_date , interval 1 day)) as date,
  197. LAST_DAY(date_sub(czl.log_date , interval 1 day)) as weekday,
  198. count(distinct czl.car_id ) as ncars,
  199. count(distinct czl.zone_id ) as nzones
  200. from car_zone_logs czl
  201. where czl.log_date + interval '8' hour >= @start
  202. and czl.car_state = 'normal'
  203. and czl.zone_state = 'normal'
  204. and czl.csa_state = 'normal'
  205. and czl.log_date = LAST_DAY(date_sub(czl.log_date , interval 1 day))
  206. group by 1) t1
  207.  
  208. left join
  209.  
  210. (select
  211. Month(date_add(czl.log_date , interval 1 month)) as date,
  212. LAST_DAY(date_add(czl.log_date , interval 1 month)) as weekday,
  213. count(distinct czl.car_id ) as ncars,
  214. count(distinct czl.zone_id ) as nzones
  215. from car_zone_logs czl
  216. where czl.log_date + interval '8' hour >= @start
  217. and czl.car_state = 'normal'
  218. and czl.zone_state = 'normal'
  219. and czl.csa_state = 'normal'
  220. and czl.log_date = LAST_DAY(date_sub(czl.log_date , interval 1 day))
  221. group by 1) t2
  222. on t1.date = t2.date
  223.  
  224. left join
  225.  
  226. (select month(L.date) as month, ROUND(Avg(L.cars),0) as cars from
  227. (select date(date_sub(czl.log_date , interval 1 day)) as date,
  228. count(distinct czl.car_id) as cars
  229. from car_zone_logs czl
  230. where czl.log_date + interval '8' hour >= @start
  231. and czl.zone_state = 'normal'
  232. and czl.csa_state = 'normal'
  233. #where czl.zone_state='normal'
  234. group by 1
  235. order by 1 desc) L
  236. group by 1) P
  237. on P.month = t1.date
  238.  
  239.  
  240. order by t1.date;
  241.  
  242.  
  243. ######## weekly Revenue Related Info
  244.  
  245. set @startDate = '2020-10-01 00:00';
  246.  
  247. select
  248. rm.week as week,
  249. sum(rm.nr) as netrevenues,
  250. sum(rm.rent) as rental,
  251. sum(rm.d2d) as d2d,
  252. sum(O.oneway_amount) as oneway,
  253. sum(C.subscriptionFee) as subscriptionFee,
  254. sum(rm.mileage) as mileage,
  255. sum(A.Amount) as penalty_Repair,
  256. sum(rm.paid_coupon) as Coupon,
  257. IFNULL(B.noa,0) as Accidents,
  258. IFNULL(B.accidentamount,0) as accidentamount,
  259. IFNULL(sum(I.insurance),0) as insurance,
  260. sum(rm.chauffeur) as chauffeur,
  261. sum(F.fuel) as fuel
  262.  
  263. from
  264. (select
  265. weekofyear(r.return_at + interval '8' hour) as week,
  266. sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('rent')),0)) as rent,
  267. sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('d2d')),0)) as d2d,
  268. sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('oneway')),0)) as oneway,
  269. sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('chauffeur')),0)) as chauffeur,
  270. sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('mileage')),0)) as mileage,
  271. sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('subscriptionFee')),0)) as subscriptionFee,
  272. sum(ifnull((select sum(amount) from payments where state='normal' and r.id=reservation_id and paid_type='coupon'),0)) as paid_coupon,
  273. 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)- ifnull((select sum(amount) from payments where state='normal' and reservation_id=r.id and paid_type ='coupon'),0)),2) as nr
  274. from reservations r, members m
  275.  
  276. where r.member_id=m.id
  277. and m.imaginary in ('normal','sofam')
  278. and r.state = 'completed'
  279. and convert_tz(r.return_at, '+0:00','+8:00') >= @startdate
  280. group by 1
  281. ) rm
  282. left join
  283.  
  284. ######### Weekly other amounts
  285. (select
  286. weekofyear(c.created_at + interval '8' hour) as week,
  287. sum(c.amount) as amount
  288. from charges c
  289. where convert_tz(c.created_at, '+0:00','+8:00') >= @startdate
  290. and c.state = 'normal'
  291. and c.kind not in ('rent','d2d','oneway','mileage','subscriptionFee','insurance','chauffeur','fuel','accident')
  292. group by 1) A on A.week = rm.week
  293. left join
  294. (select
  295. weekofyear(c.created_at + interval 8 hour) as week,
  296. sum(c.amount) as oneway_amount
  297. from charges c
  298. where c.kind in ('oneway')
  299. and c.state = 'normal'
  300. and (c.created_at + interval 8 hour) >= @startdate
  301. group by 1) O on O.week = rm.week
  302. left join
  303. (select
  304. A.week as week,
  305. A.noa as noa,
  306. round(Sum(A.amount),0) as accidentamount
  307. from
  308. (
  309. select
  310. weekofyear(c.created_at + interval '8' hour) as week,
  311. c.id as cid,
  312. count(distinct case when c.kind = 'accident' then c.id end) as noa,
  313. sum(c.amount) as amount
  314. from charges c
  315. where convert_tz(c.created_at, '+0:00','+8:00') >= @startdate
  316. and c.state = 'normal'
  317. and c.kind in ('accident')
  318. group by 1) A
  319. group by 1,2) B on A.Week = B.week
  320. left join
  321. (select
  322. weekofyear(c.created_at + interval 8 hour) as week,
  323. sum(c.amount) as insurance
  324. from charges c
  325. where c.kind in ('insurance')
  326. and c.state = 'normal'
  327. and (c.created_at + interval 8 hour) >= @startdate
  328. group by 1) I on I.week = rm.week
  329. left join
  330. (select
  331. A.week as week,
  332. A.Paid_subs as Paid_subs,
  333. round(Sum(A.amount),0) as subscriptionFee
  334. from
  335. (
  336. select
  337. weekofyear(c.created_at + interval '8' hour) as week,
  338. c.id as cid,
  339. count(distinct case when c.kind = 'subscriptionFee' then c.id end) as Paid_subs,
  340. sum(c.amount) as amount
  341. from charges c
  342. where convert_tz(c.created_at, '+0:00','+8:00') >= @startdate
  343. and c.state = 'normal'
  344. and c.kind in ('subscriptionFee')
  345. group by 1) A
  346. group by 1,2) C
  347. on A.week = C.week
  348. left join
  349. (select
  350. weekofyear(c.created_at + interval 8 hour) as week,
  351. sum(c.amount) as fuel
  352. from charges c
  353. where c.kind in ('fuel')
  354. and c.state = 'normal'
  355. and (c.created_at + interval 8 hour) >= @startdate
  356. group by 1) F on F.week = rm.week
  357. group by rm.week ;
  358.  
  359. ######## monthly Revenue Related Info
  360.  
  361.  
  362. set @startDate = '2020-10-01 00:00';
  363.  
  364. select
  365. rm.month as month,
  366. sum(rm.nr) as netrevenues,
  367. sum(rm.rent) as rental,
  368. sum(rm.d2d) as d2d,
  369. sum(O.oneway_amount) as oneway,
  370. sum(C.subscriptionFee) as subscriptionFee,
  371. sum(rm.mileage) as mileage,
  372. sum(A.Amount) as penalty_Repair,
  373. sum(rm.paid_coupon) as Coupon,
  374. IFNULL(B.noa,0) as Accidents,
  375. IFNULL(B.accidentamount,0) as accidentamount,
  376. IFNULL(sum(I.insurance),0) as insurance,
  377. sum(rm.chauffeur) as chauffeur,
  378. sum(F.fuel) as fuel
  379.  
  380. from
  381. (select
  382. month(r.return_at + interval '8' hour) as month,
  383. sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('rent')),0)) as rent,
  384. sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('d2d')),0)) as d2d,
  385. sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('oneway')),0)) as oneway,
  386. sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('chauffeur')),0)) as chauffeur,
  387. sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('mileage')),0)) as mileage,
  388. sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('subscriptionFee')),0)) as subscriptionFee,
  389. sum(ifnull((select sum(amount) from payments where state='normal' and r.id=reservation_id and paid_type='coupon'),0)) as paid_coupon,
  390. 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)- ifnull((select sum(amount) from payments where state='normal' and reservation_id=r.id and paid_type ='coupon'),0)),2) as nr
  391. from reservations r, members m
  392.  
  393. where r.member_id=m.id
  394. and m.imaginary in ('normal','sofam')
  395. and r.state = 'completed'
  396. and convert_tz(r.return_at, '+0:00','+8:00') >= @startdate
  397. group by 1
  398. ) rm
  399. left join
  400. ######### monthly other amounts
  401. (select
  402. month(c.created_at + interval '8' hour) as month,
  403. sum(c.amount) as amount
  404. from charges c
  405. where convert_tz(c.created_at, '+0:00','+8:00') >= @startdate
  406. and c.state = 'normal'
  407. and c.kind not in ('rent','d2d','oneway','mileage','subscriptionFee','insurance','chauffeur','fuel','accident')
  408. group by 1) A on A.month = rm.month
  409. left join
  410. (select
  411. A.month as month,
  412. A.noa as noa,
  413. round(Sum(A.amount),0) as accidentamount
  414. from
  415. (
  416. select
  417. month(c.created_at + interval '8' hour) as month,
  418. c.id as cid,
  419. count(distinct case when c.kind = 'accident' then c.id end) as noa,
  420. sum(c.amount) as amount
  421. from charges c
  422. where convert_tz(c.created_at, '+0:00','+8:00') >= @startdate
  423. and c.state = 'normal'
  424. and c.kind in ('accident')
  425. group by 1) A
  426. group by 1,2) B
  427. on A.month = B.month
  428. left join
  429. (select
  430. month(c.created_at + interval 8 hour) as month,
  431. sum(c.amount) as oneway_amount
  432. from charges c
  433. where c.kind in ('oneway')
  434. and c.state = 'normal'
  435. and (c.created_at + interval 8 hour) >= @startdate
  436. group by 1) O on O.month = rm.month
  437. left join
  438. (select
  439. month(c.created_at + interval 8 hour) as month,
  440. sum(c.amount) as insurance
  441. from charges c
  442. where c.kind in ('insurance')
  443. and c.state = 'normal'
  444. and (c.created_at + interval 8 hour) >= @startdate
  445. group by 1) I on I.month = rm.month
  446. left join
  447. (select
  448. A.month as month,
  449. A.Paid_subs as Paid_subs,
  450. round(Sum(A.amount),0) as subscriptionFee
  451. from
  452. (
  453. select
  454. month(c.created_at + interval '8' hour) as month,
  455. c.id as cid,
  456. count(distinct case when c.kind = 'subscriptionFee' then c.id end) as Paid_subs,
  457. sum(c.amount) as amount
  458. from charges c
  459. where convert_tz(c.created_at, '+0:00','+8:00') >= @startdate
  460. and c.state = 'normal'
  461. and c.kind in ('subscriptionFee')
  462. group by 1) A
  463. group by 1,2) C
  464. on rm.month = C.month
  465. left join
  466. (select
  467. month(c.created_at + interval 8 hour) as month,
  468. sum(c.amount) as fuel
  469. from charges c
  470. where c.kind in ('fuel')
  471. and c.state = 'normal'
  472. and (c.created_at + interval 8 hour) >= @startdate
  473. group by 1) F on F.month = rm.month
  474. group by rm.month
Add Comment
Please, Sign In to add comment