ahmedrahil786

MFT, Bookings, Net Rev, Gross Rev - Query Error

Apr 12th, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. #set @start := '2018-6-31 00:00';
  2. #set @end := '2018-12-31 00:00';
  3.  
  4. select
  5. D.Week,
  6. D.TotalMFTs,
  7. C.ActiveMembers,
  8. C.Bookings,
  9. C.NonCancelledBookings,
  10. C.Netrev,
  11. C.Grossrev,
  12. CAST((D.TotalMFTs/C.ActiveMembers)*100 AS DECIMAL(18,2)) as MFT_DAUPct
  13.  
  14.  
  15. from (select
  16. WEEKOFYEAR(r.start_at + interval '8' hour) as Week,
  17. count(distinct case when r.state = 'completed' then r.member_id end) as ActiveMembers,
  18. count(distinct case when r.state = 'completed' then r.id end) as Bookings,
  19. count(distinct case when r.state NOT IN ('canceled', 'fail') then r.id end) as NonCancelledBookings,
  20. #sum(timestampdiff(hour,CONVERT_TZ(r.occupy_start_at, '+00:00', '+8:00'), CONVERT_TZ(r.occupy_end_at, '+00:00', '+8:00'))) as ridelength,
  21. sum(G.NetRev) as Netrev,
  22. sum(G.GrossRev) as Grossrev
  23.  
  24.  
  25. from reservations r
  26.  
  27. join members m
  28. on m.id = r.member_id
  29.  
  30. left join (select
  31. ch.reservation_id as rid,
  32. sum(case when ch.state = 'normal' and ch.paid_type = 'card' then ch.amount end) as NetRev,
  33. sum(case when ch.state = 'normal' then ch.amount end) as GrossRev
  34.  
  35. from payments ch
  36.  
  37. group by 1
  38. ) G on G.rid = r.id
  39.  
  40. where r.start_at + interval '8' hour >= @start
  41. and r.start_at + interval '8' hour < @end
  42. and m.imaginary = 'normal'
  43. group by 1) C
  44.  
  45. join (
  46. select
  47. WEEKOFYEAR(c.created_at + interval '8' hour) as Week,
  48. count(distinct case when c.kind = 'subscriptionFee' then c.member_id end) as TotalMFTs,
  49. sum(c.amount) as amount
  50.  
  51. from charges c
  52.  
  53. join members m
  54. on m.id = c.member_id
  55.  
  56. where c.created_at + interval '8' hour >= @start
  57. and c.created_at + interval '8' hour < @end
  58. and m.imaginary = 'normal'
  59. group by 1) D
  60. on D.Week = C.Week
  61.  
  62. group by 1
  63.  
  64. order by 1 asc;
Advertisement
Add Comment
Please, Sign In to add comment