ahmedrahil786

WIP Tracker - M.O.M - Version 2.0

Aug 28th, 2019
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.99 KB | None | 0 0
  1. set @start := '2019-1-1';
  2. Select P.month, P.TotalMFTs,P.GrabMFT, P.mft_21,Q.MFT_Oneway, ROUND(P.NetRev,2) as Net_rev,ROUND(P.GrabPay,2) as Grabpay_Rev, ROUND(P.GrossRev,2) as Gross_rev, Round((P.GrabMFT/P.TotalMFTs)*100,2) as Grabpay_MFT_PCT_Total,
  3. Round(ROUND(P.GrabPay,2)/ROUND(P.NetRev,2),2) as Grabpay_PCT_NetRev, R.Diff_MFT_Oneway_MOM as Diff_MFT_Oneway_MOM, R.MFT_PCT_DIFF as MFT_PCT_DIFF from
  4. (select
  5. A.month as month,A.TotalMFTs as TotalMFTs,A.GrabMFT as GrabMFT,A.mft_21 as mft_21,IFNULL(B.NetRev,0) as NetRev,IFNULL(B.GrabPay,0) as GrabPay,B.GrossRev as GrossRev
  6. from (select
  7. Month(c.created_at + interval '8' hour) as month,
  8. count(distinct case when c.kind = 'subscriptionFee' and str_to_date(m.birthday, '%Y%m%d') >= NOW() - interval '21' year then c.member_id end) as mft_21,
  9. count(distinct case when c.kind = 'subscriptionFee' and pm.card_type = 'eWallet' and (c.created_at + interval '8' hour) >= '2019-6-12' then c.member_id end) as GrabMFT,
  10. count(distinct case when c.kind = 'subscriptionFee' then c.member_id end) as TotalMFTs
  11. from charges c join members m on m.id = c.member_id
  12. join payment_methods pm on pm.member_id = m.id
  13. left outer join member_appendixes ma on ma.id = m.id
  14. where c.created_at + interval '8' hour >= @start
  15. and m.imaginary = 'normal'
  16. group by 1) A
  17. join (
  18. select
  19. month(r.created_at + interval '8' hour) as month,
  20. sum(case when p.state = 'normal' and p.paid_type IN ('card', 'eWallet') then p.amount end) as NetRev,
  21. sum(case when p.state = 'normal' and p.paid_type IN ('eWallet') and (r.start_at + interval '8' hour) >= '2019-6-12' then p.amount end) as GrabPay,
  22. sum(case when p.state = 'normal' then p.amount end) as GrossRev
  23. from reservations r
  24. join members m on m.id = r.member_id and m.email not like '%socar.my%' and m.imaginary = 'normal'
  25. join payments p on p.reservation_id = r.id
  26. where r.created_at + interval '8' hour >= @start and p.state = 'normal'
  27. group by 1 order by 1 asc) B
  28. on B.month = A.month group by 1 order by 1 asc) P
  29. join
  30. (Select month(A.date) as month, Count(A.rid) as MFT_Oneway from
  31. (select FB.rid2 as rid, FB.mid as mid, FB.way as way, FB.date as date from
  32. (select r.id as rid2 ,r.start_zone_id as stid, r.end_zone_id as edid,
  33. date(r.start_at + interval '8' hour) as date,
  34. fbo.mid as mid,fbo.rid,fbo.way as way from
  35. reservations r
  36. join
  37. (select
  38. fbi.mid as mid,fbi.rid as rid,fbi.way as way from
  39. (select
  40. distinct r.member_id as mid, min(r.id) as rid, r.way as way
  41. from reservations r join members m on m.id = r.member_id
  42. where r.state = 'completed' and m.imaginary = 'normal' and m.email not like '%socar.my%' group by r.member_id) fbi
  43. where fbi.way in ('oneway','onewayReturn') ) fbo on fbo.rid = r.id
  44. where r.start_zone_id in ('244','245') or r.end_zone_id in ('244','245') )FB
  45. where date(FB.date + interval '8' hour) >= '2019-01-01') A group by month order by month desc ) Q
  46. on P.month = Q.month
  47. left join
  48. (Select
  49. t1.month as month,
  50. t1.MFT_Oneway as MFT_Oneway,
  51. t1.MFT_Oneway - COALESCE(t2.MFT_Oneway, t1.MFT_Oneway) AS Diff_MFT_Oneway_MOM,
  52. IFNULL(Round(((t1.MFT_Oneway - COALESCE(t2.MFT_Oneway, t1.MFT_Oneway))/t2.MFT_Oneway)*100,2),"") as MFT_PCT_DIFF
  53. from
  54.  
  55. (Select month(A.date) as month, Count(A.rid) as MFT_Oneway from
  56. (select FB.rid2 as rid, FB.mid as mid, FB.way as way, FB.date as date from
  57. (select r.id as rid2 ,r.start_zone_id as stid, r.end_zone_id as edid,
  58. date(r.start_at + interval '8' hour) as date,
  59. fbo.mid as mid,fbo.rid,fbo.way as way from
  60. reservations r
  61. join
  62. (select
  63. fbi.mid as mid,fbi.rid as rid,fbi.way as way from
  64. (select
  65. distinct r.member_id as mid, min(r.id) as rid, r.way as way
  66. from reservations r join members m on m.id = r.member_id
  67. where r.state = 'completed' and m.imaginary = 'normal' and m.email not like '%socar.my%' group by r.member_id) fbi
  68. where fbi.way in ('oneway','onewayReturn') ) fbo on fbo.rid = r.id
  69. where r.start_zone_id in ('244','245') or r.end_zone_id in ('244','245') )FB
  70. where date(FB.date + interval '8' hour) >= '2019-01-01') A group by month order by month desc ) t1
  71.  
  72. left join
  73. #Month(date_add(czl.log_date , interval 1 month)) as date,
  74. (Select month(date_add(A.date, interval 1 month)) as month, Count(A.rid) as MFT_Oneway from
  75. (select FB.rid2 as rid, FB.mid as mid, FB.way as way, FB.date as date from
  76. (select r.id as rid2 ,r.start_zone_id as stid, r.end_zone_id as edid,
  77. date(r.start_at + interval '8' hour) as date,
  78. fbo.mid as mid,fbo.rid,fbo.way as way from
  79. reservations r
  80. join
  81. (select
  82. fbi.mid as mid,fbi.rid as rid,fbi.way as way from
  83. (select
  84. distinct r.member_id as mid, min(r.id) as rid, r.way as way
  85. from reservations r join members m on m.id = r.member_id
  86. where r.state = 'completed' and m.imaginary = 'normal' and m.email not like '%socar.my%' group by r.member_id) fbi
  87. where fbi.way in ('oneway','onewayReturn') ) fbo on fbo.rid = r.id
  88. where r.start_zone_id in ('244','245') or r.end_zone_id in ('244','245') )FB
  89. where date(FB.date + interval '8' hour) >= '2019-01-01') A group by month order by month desc ) t2
  90. on t1.month = t2.month
  91. order by t1.month desc) R
  92. on R.month = P.month
Advertisement
Add Comment
Please, Sign In to add comment