ahmedrahil786

Key Index Dashboard - Non - Revenues - V 2.0

Sep 11th, 2019
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.36 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. C.ActiveMembers,
  10. C.Bookings as rentals,
  11. C.ridelength
  12.  
  13. from (select
  14. WEEKOFYEAR(m.created_at + interval '8' hour) as Week,
  15. count(distinct m.id) as signups
  16. from members m
  17. left outer join reservations r on
  18. r.member_id = m.id
  19. where m.created_at + interval '8' hour >= @start
  20. and m.imaginary = 'normal'
  21. group by 1 ) A
  22.  
  23. join (select
  24. WEEKOFYEAR(dl.created_at + interval '8' hour) as Week,
  25. count(distinct case when dl.state not like 'noInput' then dl.member_id end) DocUploaded,
  26. count(distinct case when dl.state = 'approved' then dl.member_id end) DocApproved,
  27. count(distinct case when dl.gender = 'man' then dl.member_id end) MaleApproved,
  28. count(distinct case when dl.gender = 'woman' then dl.member_id end) FemaleApproved
  29. from driver_licenses dl
  30. join members m
  31. on m.id = dl.member_id
  32. where dl.created_at + interval '8' hour >= @start
  33. and m.imaginary = 'normal'
  34. group by 1) B
  35. on B.Week = A.Week
  36.  
  37. join (select
  38. weekofyear(r.return_at + interval '8' hour) as week,
  39. count(distinct case when r.state = 'completed' then r.member_id end) as ActiveMembers,
  40. count(distinct case when r.state = 'completed' then r.id end) as Bookings,
  41. round(sum(timestampdiff(minute, r.start_at, r.end_at)/60),2) as ridelength,
  42. sum(G.amount) as Charges
  43. from reservations r
  44. left outer join (select
  45. ch.reservation_id as rid,
  46. sum(ch.amount) as amount
  47. from payments ch
  48. group by 1
  49. ) G on G.rid = r.id
  50. left join members m on m.id = r.member_id
  51. where r.start_at + interval '8' hour >= @start
  52. and m.imaginary in ('sofam', 'normal')
  53. and r.member_id not in ('125', '127')
  54. and r.start_zone_id not in (2, 3, 101)
  55. and r.state = 'completed'
  56. group by 1) C on
  57. C.Week = B.Week
  58.  
  59. join (
  60. select
  61. WEEKOFYEAR(c.created_at + interval '8' hour) as Week,
  62. count(distinct case when c.kind = 'subscriptionFee' then c.member_id end) as TotalMFTs
  63. from charges c
  64. join members m on m.id = c.member_id
  65. where c.created_at + interval '8' hour >= @start
  66. and m.imaginary = 'normal'
  67. group by 1) D on
  68. D.Week = C.Week
  69. group by 1
  70. order by 1 asc;
  71.  
  72. Select
  73. weekofyear(t1.date) as week,
  74. t1.date,
  75. t1.ncars,
  76. t1.ncars - COALESCE(t2.ncars, t1.ncars) AS diff
  77. from
  78.  
  79. (select
  80. Date(date_sub(czl.log_date , interval 1 day)) as date,
  81. weekday(date_sub(czl.log_date , interval 1 day)) as weekday,
  82. count(distinct czl.car_id ) as ncars
  83. from car_zone_logs czl
  84. where czl.log_date + interval '8' hour >= @start
  85. and czl.car_state = 'normal'
  86. and czl.zone_state = 'normal'
  87. and czl.csa_state = 'normal'
  88. and weekday(date_sub(czl.log_date , interval 1 day)) = 6
  89. group by 1) t1
  90.  
  91. left join
  92.  
  93. (select
  94. Date(date_add(czl.log_date , interval 7 day)) as date,
  95. weekday(date_add(czl.log_date , interval 7 day)) as weekday,
  96. count(distinct czl.car_id ) as ncars
  97. from car_zone_logs czl
  98. where czl.log_date + interval '8' hour >= @start
  99. and czl.car_state = 'normal'
  100. and czl.zone_state = 'normal'
  101. and czl.csa_state = 'normal'
  102. and weekday(date_sub(czl.log_date , interval 1 day)) = 6
  103. group by 1) t2
  104.  
  105. on t1.date = t2.date - 1
  106.  
  107. order by t1.date ;
  108.  
  109.  
  110. set @start2 := '2019-01-01 00:00';
  111.  
  112. select
  113. A.month,
  114. A.signups,
  115. B.DocUploaded,
  116. B.DocApproved,
  117. D.TotalMFTs,
  118. C.ActiveMembers,
  119. C.Bookings as rentals,
  120. C.ridelength
  121. from (select
  122. month(m.created_at + interval '8' hour) as month,
  123. count(distinct m.id) as signups
  124. from members m
  125. left outer join reservations r on r.member_id = m.id
  126. where m.created_at + interval '8' hour >= @start2
  127. and m.imaginary = 'normal'
  128. group by 1 ) A
  129.  
  130. join (select
  131. month(dl.created_at + interval '8' hour) as month,
  132. count(distinct case when dl.state not like 'noInput' then dl.member_id end) DocUploaded,
  133. count(distinct case when dl.state = 'approved' then dl.member_id end) DocApproved,
  134. count(distinct case when dl.gender = 'man' then dl.member_id end) MaleApproved,
  135. count(distinct case when dl.gender = 'woman' then dl.member_id end) FemaleApproved
  136. from driver_licenses dl
  137. join members m on m.id = dl.member_id
  138. where dl.created_at + interval '8' hour >= @start2
  139. and m.imaginary = 'normal'
  140. group by 1) B
  141. on B.month = A.month
  142.  
  143. join (select
  144. month(r.return_at + interval '8' hour) as month,
  145. count(distinct case when r.state = 'completed' then r.member_id end) as ActiveMembers,
  146. count(distinct case when r.state = 'completed' then r.id end) as Bookings,
  147. round(sum(timestampdiff(minute, r.start_at, r.end_at)/60),2) as ridelength,
  148. sum(G.amount) as Charges
  149. from reservations r
  150. left outer join (select
  151. ch.reservation_id as rid,
  152. sum(ch.amount) as amount
  153. from payments ch
  154. group by 1
  155. ) G on G.rid = r.id
  156. left join members m on m.id = r.member_id
  157. where r.start_at + interval '8' hour >= @start2
  158. and m.imaginary in ('sofam', 'normal')
  159. and r.member_id not in ('125', '127')
  160. and r.start_zone_id not in (2, 3, 101)
  161. and r.state = 'completed'
  162. group by 1) C on
  163. C.month = B.month
  164.  
  165. join (
  166. select
  167. month(c.created_at + interval '8' hour) as month,
  168. count(distinct case when c.kind = 'subscriptionFee' then c.member_id end) as TotalMFTs
  169. from charges c
  170. join members m
  171. on m.id = c.member_id
  172. where c.created_at + interval '8' hour >= @start2
  173. and m.imaginary = 'normal'
  174. group by 1) D on
  175. D.month = C.month
  176.  
  177. group by 1
  178.  
  179. order by 1 asc;
  180.  
  181. set @start := '2018-12-31 00:00';
  182.  
  183. Select
  184. t1.date,
  185. t1.ncars,
  186. t1.ncars - COALESCE(t2.ncars, t1.ncars) AS diff_cars,
  187. t1.nzones,
  188. t1.nzones - COALESCE(t2.nzones, t1.nzones) AS diff_zones
  189. from
  190.  
  191. (select
  192. Month(date_sub(czl.log_date , interval 1 day)) as date,
  193. LAST_DAY(date_sub(czl.log_date , interval 1 day)) as weekday,
  194. count(distinct czl.car_id ) as ncars,
  195. count(distinct czl.zone_id ) as nzones
  196. from car_zone_logs czl
  197. where czl.log_date + interval '8' hour >= @start
  198. and czl.car_state = 'normal'
  199. and czl.zone_state = 'normal'
  200. and czl.csa_state = 'normal'
  201. and czl.log_date = LAST_DAY(date_sub(czl.log_date , interval 1 day))
  202. group by 1) t1
  203.  
  204. left join
  205.  
  206. (select
  207. Month(date_add(czl.log_date , interval 1 month)) as date,
  208. LAST_DAY(date_add(czl.log_date , interval 1 month)) as weekday,
  209. count(distinct czl.car_id ) as ncars,
  210. count(distinct czl.zone_id ) as nzones
  211. from car_zone_logs czl
  212. where czl.log_date + interval '8' hour >= @start
  213. and czl.car_state = 'normal'
  214. and czl.zone_state = 'normal'
  215. and czl.csa_state = 'normal'
  216. and czl.log_date = LAST_DAY(date_sub(czl.log_date , interval 1 day))
  217. group by 1) t2
  218.  
  219. on t1.date = t2.date
  220.  
  221. order by t1.date;
Advertisement
Add Comment
Please, Sign In to add comment