ahmedrahil786

MFT_23 - Rebecca

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