ahmedrahil786

Rahil - MFT Calculations

May 10th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. set @start := '2019-04-01 00:00:00';
  2. set @end := '2019-04-31 23:59:59';
  3.  
  4. Select
  5. B.zoneid,
  6. B.regionname,
  7. Sum(A.TotalMFTs),
  8. (C.AM) as Activemembers
  9. from
  10.  
  11. (select
  12. week(c.created_at + interval '8' hour) as week,
  13. m.id as mid,
  14. count(distinct case when c.kind = 'subscriptionFee' then c.member_id end) as TotalMFTs
  15. from charges c
  16. join members m
  17. on m.id = c.member_id
  18. where c.created_at + interval '8' hour >= @start
  19. and c.created_at + interval '8' hour <= @end
  20. and m.imaginary = 'normal'
  21. and c.kind = 'subscriptionFee'
  22. #and r.state = 'completed'
  23. Group by week,mid) A
  24.  
  25. join
  26.  
  27. (select
  28. distinct m.id as mid,
  29. min(week(r.start_at + interval '8' hour)) as week,
  30. r.id as rid, r.car_id as carid, cc.car_name as carname, z.region as regionname,r.start_zone_id as zoneid
  31. from reservations r
  32. left join members m on m.id = r.member_id
  33. left join cars c on c.id = r.car_id
  34. left join car_classes cc on cc.id = c.car_class_id
  35. left join zones z on z.id = c.zone_id
  36.  
  37. where r.start_at + interval '8' hour >= @start
  38. and r.start_at + interval '8' hour <= @end
  39. and r.state = 'completed'
  40. group by mid) B
  41.  
  42. on A.mid = B.mid
  43.  
  44. left join
  45.  
  46. (select
  47. r.start_zone_id as zoneid,
  48. count(distinct case when r.state = 'completed' then r.member_id end) as AM
  49. from reservations r
  50. join members m on m.id = r.member_id
  51. where r.start_at + interval '8' hour >= @start
  52. and r.start_at + interval '8' hour <= @end
  53. and m.imaginary in ('normal')
  54. and r.state = 'completed'
  55. and r.start_zone_id not in (2,3,101,383,378,697,712,818)
  56. group by zoneid) C
  57.  
  58. on B.zoneid = C.zoneid
  59.  
  60. where C.zoneid not in (2,3,101,383,378,697,712,818)
  61.  
  62. group by B.zoneid
Add Comment
Please, Sign In to add comment