ahmedrahil786

KID - Non Revenues - Group by Year

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