ahmedrahil786

Leon Query - MFT _ 21

Mar 28th, 2019
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. #set @start := '2019-3-1';
  2.  
  3. select
  4. A.Date,
  5. A.month,
  6. #A.day,
  7. A.TotalMFTs,
  8. A.mft_21,
  9. B.ActiveMembers,
  10. B.Bookings,
  11. B.NonCancelledBookings,
  12. #B.hours,
  13. B.NetRev,
  14. B.Netrev_21,
  15. B.GrossRev,
  16. CAST((A.TOtalMFTs/B.ActiveMembers)*100 AS DECIMAL(18,2)) as MFT_DAUPct
  17. from (select
  18. Date(c.created_at + interval '8' hour) as Date,
  19. month(c.created_at + interval '8' hour) as month,
  20. DAY(c.created_at + interval '8' hour) as Day,
  21. 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,
  22. count(distinct case when c.kind = 'subscriptionFee' then c.member_id end) as TotalMFTs
  23.  
  24. from charges c
  25. join members m
  26. on m.id = c.member_id
  27. left outer join member_appendixes ma
  28. on ma.id = m.id
  29. where c.created_at + interval '8' hour >= @start
  30. #and c.created_at + interval '8' hour <= '2018-5-2 00:00'
  31. and m.imaginary = 'normal'
  32. group by 1) A
  33. join (
  34. select
  35. DAY(r.created_at + interval '8' hour) as Day,
  36. count(distinct case when r.state NOT IN ('canceled', 'fail') then r.member_id end) as ActiveMembers,
  37. count(distinct case when r.state = 'completed' then r.id end) as Bookings,
  38. count(distinct case when r.state NOT IN ('canceled', 'fail') then r.id end) as NonCancelledBookings,
  39. count(distinct case when r.state = 'fail' then r.id end) as FailedBookings,
  40. count(distinct case when r.state = 'fail' then r.member_id end) as UniqueFailedsids,
  41. sum(timestampdiff(hour,r.start_at, r.end_at)) as hours,
  42. sum(case when p.state = 'normal' and p.paid_type = 'card' then p.amount end) as NetRev,
  43. sum(case when p.state = 'normal' and p.paid_type = 'card' and str_to_date(m.birthday, '%Y%m%d') >= NOW() - interval '21' year then p.amount end) as NetRev_21,
  44. sum(case when p.state = 'normal' then p.amount end) as GrossRev
  45. from reservations r
  46. join members m
  47. on m.id = r.member_id and m.email not like '%socar.my%' and m.imaginary = 'normal'
  48. join payments p
  49. on p.reservation_id = r.id
  50. where r.created_at + interval '8' hour >= @start
  51. and p.state = 'normal'
  52. group by 1
  53. order by 1 asc) B
  54. on B.Day = A.Day
  55. group by 1
  56. order by 1 asc
  57. ;
Advertisement
Add Comment
Please, Sign In to add comment