ahmedrahil786

TTT - AUTO - NEW Weekly Index

Mar 23rd, 2020
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.64 KB | None | 0 0
  1. select mem.Aweek,mem.hosts_Signup as mem_Signup,mem.DocUploaded as mem_DocUploaded,mem.DocApproved as mem_DocApproved,mem.renter_First_Reservation,mem.Active_renters,
  2. Hos.hosts_Signup,Hos.DocUploaded as hos_Signup,Hos.DocUploaded as Hos_DocUploaded,Hos.DocApproved as hos_DocUploaded,Hos.Host_First_Rent,Hos.Active_Hosts,
  3. cars.cars_listed, cars.Individual_trevo_Sheild_Cars, cars.Individual_no_insurance_Cars,cars.Fleet_Cars,cars.Active_Trevosheild_Cars,cars.Active_NO_Trevosheild_Cars,cars.Active_Fleet_Cars,
  4. Rev.Reservations,Rev.Individual_trevo_Sheild_Resv, Rev.Individual_no_insurance_resv, Rev.Fleet_resv,Rev.Individual_trevo_Sheild_dur,Rev.Individual_no_insurance_dur,Rev.Fleet_dur,
  5. Rev.comp_gross,Rev.Individual_trevo_Sheild_comp_gross,Rev.Individual_no_insurance_comp_gross,Rev.Fleet_comp_gross,Rev.Individual_trevo_Sheild_comp_net,Rev.Individual_no_insurance_comp_net,
  6. Rev.Fleet_comp_net,Rev.Individual_trevo_Sheild_coupon,Rev.Individual_no_insurance_coupon,Rev.Fleet_coupon, mem.Ayear, cars.Insearch_cars,cars.Individual_Insearch_trevo_Sheild_Cars,
  7. cars.Individual_Insearch_no_insurance_Cars, cars.Insearch_Fleet_Cars
  8. from
  9.  
  10. /*################################### Members */
  11.  
  12. (select S.Aweek as Aweek, S.Ayear as Ayear, S.signup as hosts_Signup,D.DocUploaded as DocUploaded, D.DocApproved as DocApproved, F.MFTs as renter_First_Reservation, H.Active_renters as Active_renters from
  13. (select
  14. extract('week' from u.created_at) as Aweek,
  15. extract('year' from u.created_at) as Ayear,
  16. count(distinct case when u.status not in ('null') then u.id end) as signup
  17. from users u
  18. group by 1,2) S
  19. left join
  20. (select
  21. extract('week' from uk.created_at) as Aweek,
  22. extract('year' from uk.created_at) as Ayear,
  23. count(distinct case when uk.status not in ('null') then uk.user_id end) DocUploaded,
  24. count(distinct case when uk.status = 'approved' then uk.user_id end) DocApproved
  25. from user_kyc uk
  26. group by 1,2) D
  27. on S.Aweek = D.Aweek and S.Ayear = D.Ayear
  28. left join
  29. (select
  30. extract('week' from b.created_at) as Aweek,
  31. extract('year' from b.created_at) as Ayear,
  32. count(distinct case when b.status in ('completed') then b.user_id end) as Active_renters
  33. from bookings b
  34. group by 1,2) H
  35. on H.Aweek = S.Aweek and H.Ayear = S.Ayear
  36. left join
  37. (select A.Aweek as Aweek, A.Ayear as Ayear, count(distinct case when B.first_res = A.bookingid then A.userid end) as MFTs from
  38. ((select distinct b.id as bookingid, extract('week' from b.created_at) as Aweek, extract('year' from b.created_at) as Ayear, b.user_id userid
  39. from bookings b left join users u on u.id = b.user_id
  40. where b.status = 'completed'
  41. group by b.id
  42. order by b.id desc) A
  43. left join
  44. (select b.user_id as userid , min(b.id) as first_res
  45. from bookings b where b.status ='completed'
  46. group by userid
  47. order by 2 asc) B
  48. on B.userid = A.userid)
  49. group by A.Aweek,A.Ayear) F
  50. on F.Aweek = S.Aweek and F.Ayear = S.Ayear ) mem
  51.  
  52. /*################################### Hosts */
  53.  
  54. left join
  55.  
  56. (select S.Aweek as Aweek, S.Ayear as Ayear, S.signup as hosts_Signup,D.DocUploaded as DocUploaded , D.DocApproved as DocApproved, F.MFTs as Host_First_Rent, H.Active_Hosts as Active_Hosts from
  57. (select
  58. extract('week' from l.created_at) as Aweek,
  59. extract('year' from l.created_at) as Ayear,
  60. count(distinct case when l.status not in ('null') then l.id end) as signup
  61. from listings l
  62. group by 1,2) S
  63. left join
  64. (select
  65. extract('week' from ld.created_at) as Aweek,
  66. extract('year' from ld.created_at) as Ayear,
  67. count(distinct case when ld.status not in ('null') then ld.listing_id end) DocUploaded,
  68. count(distinct case when ld.status = 'approved' then ld.listing_id end) DocApproved
  69. from listing_documents ld
  70. group by 1,2) D
  71. on S.Aweek = D.Aweek and S.Ayear = D.Ayear
  72. left join
  73. (select
  74. extract('week' from b.created_at) as Aweek,
  75. extract('year' from b.created_at) as Ayear,
  76. count(distinct case when b.status in ('completed') then b.listing_id end) as Active_hosts
  77. from bookings b
  78. group by 1,2) H
  79. on H.Aweek = S.Aweek and H.Ayear = S.Ayear
  80. left join
  81. (select A.Aweek as Aweek, A.Ayear as Ayear, count(distinct case when B.first_res = A.bookingid then B.listing_id end) as MFTs from
  82. ((select distinct b.id as bookingid, extract('week' from b.created_at) as Aweek, extract('year' from b.created_at) as Ayear, b.listing_id as listing_id
  83. from bookings b
  84. where b.status = 'completed'
  85. group by b.id
  86. order by b.id desc) A
  87. left join
  88. (select b.listing_id as listing_Id , min(b.id) as first_res
  89. from bookings b where b.status ='completed'
  90. group by b.listing_id
  91. order by 2 asc) B
  92. on B.listing_Id = A.listing_Id)
  93. group by A.Aweek,A.Ayear) F
  94. on F.Aweek = S.Aweek and F.Ayear = S.Ayear) Hos
  95. on Hos.Aweek = mem.Aweek and Hos.Ayear = mem.Ayear
  96.  
  97. /*################################### Cars */
  98.  
  99. left join
  100. (select A.week as Aweek,
  101. A.year as Ayear,
  102. A.cars_listed as cars_listed,
  103. A.Individual_trevo_Sheild_Cars as Individual_trevo_Sheild_Cars,
  104. A.Individual_no_insurance_Cars as Individual_no_insurance_Cars,
  105. A.Fleet_Cars as Fleet_Cars,
  106. C.Insearch_cars as Insearch_cars,
  107. C.Individual_Insearch_trevo_Sheild_Cars as Individual_Insearch_trevo_Sheild_Cars,
  108. C.Individual_Insearch_no_insurance_Cars as Individual_Insearch_no_insurance_Cars,
  109. C.Insearch_Fleet_Cars as Insearch_Fleet_Cars,
  110. B.Individual_trevo_Sheild_cars as Active_Trevosheild_Cars,
  111. B.Individual_no_insurance_cars as Active_NO_Trevosheild_Cars,
  112. B.Fleet_cars as Active_Fleet_Cars
  113. from
  114. (select
  115. extract('week' from P.date) as week,
  116. extract('year' from P.date) as year,
  117. count(distinct Q.lid) as cars_listed,
  118. count(distinct case when (Q.listing_type = 'Individual' and Q.insurance_package = 'Trevo-Shield') then Q.lid end) as Individual_trevo_Sheild_Cars,
  119. count(distinct case when (Q.listing_type = 'Individual' and Q.insurance_package = 'Normal') then Q.lid end) as Individual_no_insurance_Cars,
  120. count(distinct case when Q.listing_type = 'Fleet' then Q.lid end) as Fleet_Cars
  121. from
  122. (select distinct l.id as lid, dates.ymd as date, count(distinct l.id) as cars_P_Day
  123. from listings l
  124. join (SELECT generate_series(timestamp '2019-10-01', NOW() , interval '1 day')::date ymd) dates on
  125. dates.ymd > l.created_at::date
  126. where l.status='approved' and l.deleted_at is null
  127. group by 1,2
  128. order by dates.ymd desc) P
  129. left join
  130. (select distinct l.id as lid,l.status as status,
  131. cu.id as cuid,
  132. li.insurance_package_id as insurance_id,
  133. CASE WHEN cu.id IS NOT NULL THEN 'Fleet' ELSE 'Individual' END listing_type,
  134. CASE WHEN li.insurance_package_id=2 THEN 'Trevo-Shield' ELSE 'Normal' END insurance_package,
  135. date_trunc('day', l.created_at)::date as date,
  136. extract('year' from l.created_at) as Ayear
  137. from listings l
  138. LEFT JOIN company_users cu ON cu.user_id=l.user_id
  139. LEFT join listing_insurance li on li.listing_id = l.id
  140. WHERE l.status='approved' AND l.deleted_at IS null) Q
  141. on P.lid = Q.lid
  142. group by 1,2) A
  143. left join
  144. (select I.Aweek as Aweek,
  145. I.Ayear as Ayear,
  146. count(I.bid) as Reservations,
  147. count(distinct case when (I.listing_type = 'Individual' and I.insurance_package = 'Trevo-Shield') then I.car_id end) as Individual_trevo_Sheild_cars,
  148. count(distinct case when (I.listing_type = 'Individual' and I.insurance_package = 'Normal') then I.car_id end) as Individual_no_insurance_cars,
  149. count(distinct case when I.listing_type = 'Fleet' then I.car_id end) as Fleet_cars
  150. from
  151. (select
  152. distinct b.id as bid,
  153. extract('week' from b.created_at) as Aweek,
  154. extract('year' from b.created_at) as Ayear,
  155. b.listing_id as car_id,
  156. sum(case when b.status not IN('cancelled', 'payment_failed', 'withdrawn', 'rejected', 'accepted') then b.gross_amount end) as comp_gross,
  157. sum(case when b.status not IN('cancelled', 'payment_failed', 'withdrawn', 'rejected', 'accepted') then b.net_amount end) as comp_net,
  158. sum(case when b.status not IN('cancelled', 'payment_failed', 'withdrawn', 'rejected', 'accepted') then b.gross_amount end) -
  159. sum(case when b.status not IN('cancelled', 'payment_failed', 'withdrawn', 'rejected', 'accepted') then b.net_amount end) as coupon,
  160. ROUND((EXTRACT(EPOCH FROM b.booking_end_at) - EXTRACT(EPOCH FROM b.booking_start_at))/3600) as dur,
  161. l.status as listing_status, cu.id as cuid, li.insurance_package_id as insurance_id,
  162. CASE WHEN cu.id IS NOT NULL THEN 'Fleet' ELSE 'Individual' END listing_type,
  163. CASE WHEN li.insurance_package_id=2 THEN 'Trevo-Shield' ELSE 'Normal' END insurance_package
  164. from bookings b
  165. left join listings l on l.id = b.listing_id
  166. LEFT JOIN company_users cu ON cu.user_id=l.user_id
  167. LEFT join listing_insurance li on li.listing_id = l.id
  168. where b.status not IN('cancelled', 'payment_failed', 'withdrawn', 'rejected', 'accepted')
  169. and b.booking_end_at is not null
  170. group by 1,2,l.status,cuid,li.insurance_package_id) as I
  171. group by 1,2
  172. order by 1 desc)B
  173. on A.week = B.Aweek and A.year = B.Ayear
  174. left join
  175. (select
  176. extract('week' from A.date) as week,
  177. extract('year' from A.date) as year,
  178. count(distinct B.lid) as Insearch_cars,
  179. count(distinct case when (B.listing_type = 'Individual' and B.insurance_package = 'Trevo-Shield') then B.lid end) as Individual_Insearch_trevo_Sheild_Cars,
  180. count(distinct case when (B.listing_type = 'Individual' and B.insurance_package = 'Normal') then B.lid end) as Individual_Insearch_no_insurance_Cars,
  181. count(distinct case when B.listing_type = 'Fleet' then B.lid end) as Insearch_Fleet_Cars
  182. from
  183. (select distinct l.id as lid, dates.ymd as date, count(distinct l.id) as cars_P_Day
  184. from listings l
  185. join listing_availability la on la.listing_id=l.id
  186. join (SELECT generate_series(timestamp '2019-09-01', NOW() , interval '1 day')::date ymd) dates on
  187. dates.ymd between la.start_date::date and la.end_date::date
  188. where l.status='approved' and l.deleted_at is null
  189. group by 1,2
  190. order by dates.ymd desc) A
  191. left join
  192. (select distinct l.id as lid,l.status as status,
  193. cu.id as cuid,
  194. li.insurance_package_id as insurance_id,
  195. CASE WHEN cu.id IS NOT NULL THEN 'Fleet' ELSE 'Individual' END listing_type,
  196. CASE WHEN li.insurance_package_id=2 THEN 'Trevo-Shield' ELSE 'Normal' END insurance_package,
  197. date_trunc('day', l.created_at)::date as date,
  198. extract('year' from l.created_at) as Ayear
  199. from listings l
  200. LEFT JOIN company_users cu ON cu.user_id=l.user_id
  201. LEFT join listing_insurance li on li.listing_id = l.id
  202. WHERE l.status='approved' AND l.deleted_at IS null) B
  203. on A.lid = B.lid
  204. group by 1,2) C
  205. on C.week = A.week and C.year = A.year
  206. group by 1,2,3,4,5,6,7,8,9,10,11,12,13) cars
  207. on cars.Aweek = Hos.Aweek and cars.Ayear = Hos.Ayear
  208.  
  209. /*################################### Revenue and Reservations */
  210.  
  211. left join
  212. (select I.Aweek as Aweek,
  213. I.Ayear as Ayear,
  214. count(I.bid) as Reservations,
  215. count(distinct case when (I.listing_type = 'Individual' and I.insurance_package = 'Trevo-Shield') then I.bid end) as Individual_trevo_Sheild_Resv,
  216. count(distinct case when (I.listing_type = 'Individual' and I.insurance_package = 'Normal') then I.bid end) as Individual_no_insurance_resv,
  217. count(distinct case when I.listing_type = 'Fleet' then I.bid end) as Fleet_resv,
  218. sum(case when (I.listing_type = 'Individual' and I.insurance_package = 'Trevo-Shield') then I.dur end) as Individual_trevo_Sheild_dur,
  219. sum(case when (I.listing_type = 'Individual' and I.insurance_package = 'Normal') then I.dur end) as Individual_no_insurance_dur,
  220. sum(case when I.listing_type = 'Fleet' then I.dur end) as Fleet_dur,
  221. sum(I.comp_gross) as comp_gross,
  222. sum(case when (I.listing_type = 'Individual' and I.insurance_package = 'Trevo-Shield') then I.comp_gross end) as Individual_trevo_Sheild_comp_gross,
  223. sum(case when (I.listing_type = 'Individual' and I.insurance_package = 'Normal') then I.comp_gross end) as Individual_no_insurance_comp_gross,
  224. sum(case when I.listing_type = 'Fleet' then I.comp_gross end) as Fleet_comp_gross,
  225. sum(case when (I.listing_type = 'Individual' and I.insurance_package = 'Trevo-Shield') then I.comp_net end) as Individual_trevo_Sheild_comp_net,
  226. sum(case when (I.listing_type = 'Individual' and I.insurance_package = 'Normal') then I.comp_net end) as Individual_no_insurance_comp_net,
  227. sum(case when I.listing_type = 'Fleet' then I.comp_net end) as Fleet_comp_net,
  228. sum(case when (I.listing_type = 'Individual' and I.insurance_package = 'Trevo-Shield') then I.coupon end) as Individual_trevo_Sheild_coupon,
  229. sum(case when (I.listing_type = 'Individual' and I.insurance_package = 'Normal') then I.coupon end) as Individual_no_insurance_coupon,
  230. sum(case when I.listing_type = 'Fleet' then I.coupon end) as Fleet_coupon
  231. from
  232. (select
  233. distinct b.id as bid,
  234. extract('week' from b.created_at) as Aweek,
  235. extract('year' from b.created_at) as Ayear,
  236. b.listing_id as car_id,
  237. sum(case when b.status not IN('cancelled', 'payment_failed', 'withdrawn', 'rejected', 'accepted') then b.gross_amount end) as comp_gross,
  238. sum(case when b.status not IN('cancelled', 'payment_failed', 'withdrawn', 'rejected', 'accepted') then b.net_amount end) as comp_net,
  239. sum(case when b.status not IN('cancelled', 'payment_failed', 'withdrawn', 'rejected', 'accepted') then b.gross_amount end) -
  240. sum(case when b.status not IN('cancelled', 'payment_failed', 'withdrawn', 'rejected', 'accepted') then b.net_amount end) as coupon,
  241. ROUND((EXTRACT(EPOCH FROM b.booking_end_at) - EXTRACT(EPOCH FROM b.booking_start_at))/3600) as dur,
  242. l.status as listing_status, cu.id as cuid, li.insurance_package_id as insurance_id,
  243. CASE WHEN cu.id IS NOT NULL THEN 'Fleet' ELSE 'Individual' END listing_type,
  244. CASE WHEN li.insurance_package_id=2 THEN 'Trevo-Shield' ELSE 'Normal' END insurance_package
  245. from bookings b
  246. left join listings l on l.id = b.listing_id
  247. LEFT JOIN company_users cu ON cu.user_id=l.user_id
  248. LEFT join listing_insurance li on li.listing_id = l.id
  249. where b.status not IN('cancelled', 'payment_failed', 'withdrawn', 'rejected', 'accepted')
  250. group by 1,l.status,cuid,li.insurance_package_id) as I
  251. group by 1,2
  252. order by 1 asc) Rev
  253. on Rev.Aweek = Hos.Aweek and Rev.Ayear = Hos.Ayear
  254. order by 1,37 asc
Advertisement
Add Comment
Please, Sign In to add comment