ahmedrahil786

TTT - (Automate) Weekly - Order by Year

Dec 29th, 2019
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.12 KB | None | 0 0
  1.  
  2. 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,
  3. 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,
  4. 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,
  5. 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,
  6. 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,
  7. Rev.Fleet_comp_net,Rev.Individual_trevo_Sheild_coupon,Rev.Individual_no_insurance_coupon,Rev.Fleet_coupon, mem.Ayear
  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.Ayear as Ayear,
  102. Count(distinct A.lid) as cars_listed,
  103. count(distinct case when (A.listing_type = 'Individual' and A.insurance_package = 'Trevo-Shield') then A.lid end) as Individual_trevo_Sheild_Cars,
  104. count(distinct case when (A.listing_type = 'Individual' and A.insurance_package = 'Normal') then A.lid end) as Individual_no_insurance_Cars,
  105. count(distinct case when A.listing_type = 'Fleet' then A.lid end) as Fleet_Cars,
  106. B.Individual_trevo_Sheild_cars as Active_Trevosheild_Cars,
  107. B.Individual_no_insurance_cars as Active_NO_Trevosheild_Cars,
  108. B.Fleet_cars as Active_Fleet_Cars
  109. from
  110. (select distinct l.id as lid,l.status as status,
  111. cu.id as cuid,
  112. li.insurance_package_id as insurance_id,
  113. CASE WHEN cu.id IS NOT NULL THEN 'Fleet' ELSE 'Individual' END listing_type,
  114. CASE WHEN li.insurance_package_id=2 THEN 'Trevo-Shield' ELSE 'Normal' END insurance_package,
  115. extract('week' from l.created_at) as week,
  116. extract('year' from l.created_at) as Ayear
  117. from listings l
  118. LEFT JOIN company_users cu ON cu.user_id=l.user_id
  119. LEFT join listing_insurance li on li.listing_id = l.id
  120. WHERE l.status='approved' AND l.deleted_at IS NULL) A
  121. left join
  122. (select I.Aweek as Aweek,
  123. I.Ayear as Ayear,
  124. count(I.bid) as Reservations,
  125. count(distinct case when (I.listing_type = 'Individual' and I.insurance_package = 'Trevo-Shield') then I.car_id end) as Individual_trevo_Sheild_cars,
  126. count(distinct case when (I.listing_type = 'Individual' and I.insurance_package = 'Normal') then I.car_id end) as Individual_no_insurance_cars,
  127. count(distinct case when I.listing_type = 'Fleet' then I.car_id end) as Fleet_cars
  128. from
  129. (select
  130. distinct b.id as bid,
  131. extract('week' from b.created_at) as Aweek,
  132. extract('year' from b.created_at) as Ayear,
  133. b.listing_id as car_id,
  134. sum(case when b.status not IN('cancelled', 'payment_failed', 'withdrawn', 'rejected', 'accepted') then b.gross_amount end) as comp_gross,
  135. sum(case when b.status not IN('cancelled', 'payment_failed', 'withdrawn', 'rejected', 'accepted') then b.net_amount end) as comp_net,
  136. sum(case when b.status not IN('cancelled', 'payment_failed', 'withdrawn', 'rejected', 'accepted') then b.gross_amount end) -
  137. sum(case when b.status not IN('cancelled', 'payment_failed', 'withdrawn', 'rejected', 'accepted') then b.net_amount end) as coupon,
  138. ROUND((EXTRACT(EPOCH FROM b.booking_end_at) - EXTRACT(EPOCH FROM b.booking_start_at))/3600) as dur,
  139. l.status as listing_status, cu.id as cuid, li.insurance_package_id as insurance_id,
  140. CASE WHEN cu.id IS NOT NULL THEN 'Fleet' ELSE 'Individual' END listing_type,
  141. CASE WHEN li.insurance_package_id=2 THEN 'Trevo-Shield' ELSE 'Normal' END insurance_package
  142. from bookings b
  143. left join listings l on l.id = b.listing_id
  144. LEFT JOIN company_users cu ON cu.user_id=l.user_id
  145. LEFT join listing_insurance li on li.listing_id = l.id
  146. where b.status not IN('cancelled', 'payment_failed', 'withdrawn', 'rejected', 'accepted')
  147. and b.booking_end_at is not null
  148. group by 1,2,l.status,cuid,li.insurance_package_id) as I
  149. group by 1,2
  150. order by 1 desc)B
  151. on A.week = B.Aweek and A.Ayear = B.Ayear
  152. group by A.week,A.Ayear, B.Individual_trevo_Sheild_cars,B.Individual_no_insurance_cars,B.Fleet_cars) cars
  153. on cars.Aweek = Hos.Aweek and cars.Ayear = Hos.Ayear
  154.  
  155. /*################################### Revenue and Reservations */
  156.  
  157. left join
  158. (select I.Aweek as Aweek,
  159. I.Ayear as Ayear,
  160. count(I.bid) as Reservations,
  161. count(distinct case when (I.listing_type = 'Individual' and I.insurance_package = 'Trevo-Shield') then I.bid end) as Individual_trevo_Sheild_Resv,
  162. count(distinct case when (I.listing_type = 'Individual' and I.insurance_package = 'Normal') then I.bid end) as Individual_no_insurance_resv,
  163. count(distinct case when I.listing_type = 'Fleet' then I.bid end) as Fleet_resv,
  164. sum(case when (I.listing_type = 'Individual' and I.insurance_package = 'Trevo-Shield') then I.dur end) as Individual_trevo_Sheild_dur,
  165. sum(case when (I.listing_type = 'Individual' and I.insurance_package = 'Normal') then I.dur end) as Individual_no_insurance_dur,
  166. sum(case when I.listing_type = 'Fleet' then I.dur end) as Fleet_dur,
  167. sum(I.comp_gross) as comp_gross,
  168. sum(case when (I.listing_type = 'Individual' and I.insurance_package = 'Trevo-Shield') then I.comp_gross end) as Individual_trevo_Sheild_comp_gross,
  169. sum(case when (I.listing_type = 'Individual' and I.insurance_package = 'Normal') then I.comp_gross end) as Individual_no_insurance_comp_gross,
  170. sum(case when I.listing_type = 'Fleet' then I.comp_gross end) as Fleet_comp_gross,
  171. sum(case when (I.listing_type = 'Individual' and I.insurance_package = 'Trevo-Shield') then I.comp_net end) as Individual_trevo_Sheild_comp_net,
  172. sum(case when (I.listing_type = 'Individual' and I.insurance_package = 'Normal') then I.comp_net end) as Individual_no_insurance_comp_net,
  173. sum(case when I.listing_type = 'Fleet' then I.comp_net end) as Fleet_comp_net,
  174. sum(case when (I.listing_type = 'Individual' and I.insurance_package = 'Trevo-Shield') then I.coupon end) as Individual_trevo_Sheild_coupon,
  175. sum(case when (I.listing_type = 'Individual' and I.insurance_package = 'Normal') then I.coupon end) as Individual_no_insurance_coupon,
  176. sum(case when I.listing_type = 'Fleet' then I.coupon end) as Fleet_coupon
  177. from
  178. (select
  179. distinct b.id as bid,
  180. extract('week' from b.created_at) as Aweek,
  181. extract('year' from b.created_at) as Ayear,
  182. b.listing_id as car_id,
  183. sum(case when b.status not IN('cancelled', 'payment_failed', 'withdrawn', 'rejected', 'accepted') then b.gross_amount end) as comp_gross,
  184. sum(case when b.status not IN('cancelled', 'payment_failed', 'withdrawn', 'rejected', 'accepted') then b.net_amount end) as comp_net,
  185. sum(case when b.status not IN('cancelled', 'payment_failed', 'withdrawn', 'rejected', 'accepted') then b.gross_amount end) -
  186. sum(case when b.status not IN('cancelled', 'payment_failed', 'withdrawn', 'rejected', 'accepted') then b.net_amount end) as coupon,
  187. ROUND((EXTRACT(EPOCH FROM b.booking_end_at) - EXTRACT(EPOCH FROM b.booking_start_at))/3600) as dur,
  188. l.status as listing_status, cu.id as cuid, li.insurance_package_id as insurance_id,
  189. CASE WHEN cu.id IS NOT NULL THEN 'Fleet' ELSE 'Individual' END listing_type,
  190. CASE WHEN li.insurance_package_id=2 THEN 'Trevo-Shield' ELSE 'Normal' END insurance_package
  191. from bookings b
  192. left join listings l on l.id = b.listing_id
  193. LEFT JOIN company_users cu ON cu.user_id=l.user_id
  194. LEFT join listing_insurance li on li.listing_id = l.id
  195. where b.status not IN('cancelled', 'payment_failed', 'withdrawn', 'rejected', 'accepted')
  196. group by 1,l.status,cuid,li.insurance_package_id) as I
  197. group by 1,2
  198. order by 1 asc) Rev
  199. on Rev.Aweek = Hos.Aweek and Rev.Ayear = Hos.Ayear
  200. order by 1,37 asc
Advertisement
Add Comment
Please, Sign In to add comment