ahmedrahil786

Jevin Query - Key Index Dashboard

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