ahmedrahil786

WIP - MFT, Oneway, Grabpay - Jason

Jul 9th, 2019
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. set @start := '2019-1-1';
  2. Select P.Week, 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 from
  3. (select
  4. A.week as week,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
  5. from (select
  6. weekofyear(c.created_at + interval '8' hour) as week,
  7. 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,
  8. 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,
  9. count(distinct case when c.kind = 'subscriptionFee' then c.member_id end) as TotalMFTs
  10. from charges c join members m on m.id = c.member_id
  11. join payment_methods pm on pm.member_id = m.id
  12. left outer join member_appendixes ma on ma.id = m.id
  13. where c.created_at + interval '8' hour >= @start
  14. and m.imaginary = 'normal'
  15. group by 1) A
  16. join (
  17. select
  18. weekofyear(r.created_at + interval '8' hour) as week,
  19. sum(case when p.state = 'normal' and p.paid_type IN ('card', 'eWallet') then p.amount end) as NetRev,
  20. 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,
  21. sum(case when p.state = 'normal' then p.amount end) as GrossRev
  22. from reservations r
  23. join members m on m.id = r.member_id and m.email not like '%socar.my%' and m.imaginary = 'normal'
  24. join payments p on p.reservation_id = r.id
  25. where r.created_at + interval '8' hour >= @start and p.state = 'normal'
  26. group by 1 order by 1 asc) B
  27. on B.week = A.week group by 1 order by 1 asc) P
  28. join
  29. (Select Weekofyear(A.date) as week, Count(A.rid) as MFT_Oneway from
  30. (select FB.rid2 as rid, FB.mid as mid, FB.way as way, FB.date as date from
  31. (select r.id as rid2 ,r.start_zone_id as stid, r.end_zone_id as edid,
  32. date(r.start_at + interval '8' hour) as date,
  33. fbo.mid as mid,fbo.rid,fbo.way as way from
  34. reservations r
  35. join
  36. (select
  37. fbi.mid as mid,fbi.rid as rid,fbi.way as way from
  38. (select
  39. distinct r.member_id as mid, min(r.id) as rid, r.way as way
  40. from reservations r join members m on m.id = r.member_id
  41. where r.state = 'completed' and m.imaginary = 'normal' and m.email not like '%socar.my%' group by r.member_id) fbi
  42. where fbi.way in ('oneway','onewayReturn') ) fbo on fbo.rid = r.id
  43. where r.start_zone_id in ('244','245') or r.end_zone_id in ('244','245') )FB
  44. where date(FB.date + interval '8' hour) >= '2019-01-01') A group by week order by week desc ) Q
  45. on P.week = Q.week
Advertisement
Add Comment
Please, Sign In to add comment