ahmedrahil786

Key Index Dashboard - Monthly

Apr 14th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. #set @start := '2018-12-31 00:00';
  2.  
  3. select
  4. A.Month,
  5. A.signups,
  6. B.DocUploaded,
  7. B.DocApproved,
  8. D.TotalMFTs,
  9. C.ActiveMembers,
  10. A.MFT,
  11. C.Bookings,
  12. C.Charges,
  13. B.MaleApproved,
  14. B.FemaleApproved
  15.  
  16.  
  17. from (select
  18. Month(m.created_at + interval '8' hour) as Month,
  19. count(distinct m.id) as signups,
  20. count(distinct case when m.grade = 'paidMember' then m.id end) as PaidMembers,
  21. count(distinct case when r.state = 'completed' then m.id end ) as MFT
  22.  
  23. from members m
  24.  
  25. left outer join reservations r on
  26. r.member_id = m.id
  27.  
  28. where m.created_at + interval '8' hour >= @start
  29. and m.imaginary = 'normal'
  30. group by 1 ) A
  31.  
  32. join (select
  33. Month(dl.created_at + interval '8' hour) as Month,
  34. count(distinct dl.member_id) as DocUploaded,
  35. count(distinct case when dl.state = 'approved' then dl.member_id end) DocApproved,
  36. count(distinct case when dl.gender = 'man' then dl.member_id end) MaleApproved,
  37. count(distinct case when dl.gender = 'woman' then dl.member_id end) FemaleApproved
  38.  
  39. from driver_licenses dl
  40.  
  41. join members m
  42. on m.id = dl.member_id
  43.  
  44. where dl.created_at + interval '8' hour >= @start
  45. and m.imaginary = 'normal'
  46. group by 1) B
  47. on B.Month = A.Month
  48.  
  49. join (select
  50. Month(r.occupy_start_at + interval '8' hour) as Month,
  51. count(distinct case when r.state = 'completed' then r.member_id end) as ActiveMembers,
  52. count(distinct case when r.state = 'completed' then r.id end) as Bookings,
  53. sum(G.amount) as Charges
  54.  
  55.  
  56. from reservations r
  57.  
  58. left outer join (select
  59. ch.reservation_id as rid,
  60. sum(ch.amount) as amount
  61.  
  62. from payments ch
  63.  
  64.  
  65.  
  66. group by 1
  67.  
  68. ) G on G.rid = r.id
  69.  
  70.  
  71. join members m
  72. on m.id = r.member_id
  73.  
  74. where r.created_at + interval '8' hour >= @start
  75. and m.imaginary = 'normal'
  76.  
  77. group by 1) C on
  78. C.Month = B.Month
  79.  
  80. join (
  81. select
  82. Month(c.created_at + interval '8' hour) as Month,
  83. count(distinct case when c.kind = 'subscriptionFee' then c.member_id end) as TotalMFTs
  84.  
  85. from charges c
  86.  
  87. join members m
  88. on m.id = c.member_id
  89.  
  90. where c.created_at + interval '8' hour >= @start
  91. and m.imaginary = 'normal'
  92. group by 1) D on
  93. D.Month = C.Month
  94.  
  95. group by 1
  96.  
  97. order by 1 asc;
Add Comment
Please, Sign In to add comment