Advertisement
ahmedrahil786

Key Index Dashboard - Weekly and Monthly - Non Revenues

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