ahmedrahil786

TTT -Weekly-Rahil

Dec 28th, 2019
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.23 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
  8. from
  9.  
  10. /*################################### Members */
  11.  
  12. (select S.Aweek as Aweek, 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. count(distinct case when u.status not in ('null') then u.id end) as signup
  16. from users u
  17. group by 1) S
  18. left join
  19. (select
  20. extract('week' from uk.created_at) as Aweek,
  21. count(distinct case when uk.status not in ('null') then uk.user_id end) DocUploaded,
  22. count(distinct case when uk.status = 'approved' then uk.user_id end) DocApproved
  23. from user_kyc uk
  24. group by 1) D
  25. on S.Aweek = D.Aweek
  26. left join
  27. (select
  28. extract('week' from b.created_at) as Aweek,
  29. count(distinct case when b.status in ('completed') then b.user_id end) as Active_renters
  30. from bookings b
  31. group by 1) H
  32. on H.Aweek = S.Aweek
  33. left join
  34. (select A.Aweek as Aweek, count(distinct case when B.first_res = A.bookingid then A.userid end) as MFTs from
  35. ((select distinct b.id as bookingid, extract('week' from b.created_at) as Aweek, b.user_id userid
  36. from bookings b left join users u on u.id = b.user_id
  37. where b.status = 'completed'
  38. group by b.id
  39. order by b.id desc) A
  40. left join
  41. (select b.user_id as userid , min(b.id) as first_res
  42. from bookings b where b.status ='completed'
  43. group by userid
  44. order by 2 asc) B
  45. on B.userid = A.userid)
  46. group by A.Aweek) F
  47. on F.Aweek = S.Aweek ) mem
  48.  
  49. /*################################### Hosts */
  50.  
  51. left join
  52.  
  53. (select S.Aweek as Aweek, 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
  54. (select
  55. extract('week' from l.created_at) as Aweek,
  56. count(distinct case when l.status not in ('null') then l.id end) as signup
  57. from listings l
  58. group by 1) S
  59. left join
  60. (select
  61. extract('week' from ld.created_at) as Aweek,
  62. count(distinct case when ld.status not in ('null') then ld.listing_id end) DocUploaded,
  63. count(distinct case when ld.status = 'approved' then ld.listing_id end) DocApproved
  64. from listing_documents ld
  65. group by 1) D
  66. on S.Aweek = D.Aweek
  67. left join
  68. (select
  69. extract('week' from b.created_at) as Aweek,
  70. count(distinct case when b.status in ('completed') then b.listing_id end) as Active_hosts
  71. from bookings b
  72. group by 1) H
  73. on H.Aweek = S.Aweek
  74. left join
  75. (select A.Aweek as Aweek, count(distinct case when B.first_res = A.bookingid then B.listing_id end) as MFTs from
  76. ((select distinct b.id as bookingid, extract('week' from b.created_at) as Aweek, b.listing_id as listing_id
  77. from bookings b
  78. where b.status = 'completed'
  79. group by b.id
  80. order by b.id desc) A
  81. left join
  82. (select b.listing_id as listing_Id , min(b.id) as first_res
  83. from bookings b where b.status ='completed'
  84. group by b.listing_id
  85. order by 2 asc) B
  86. on B.listing_Id = A.listing_Id)
  87. group by A.Aweek) F
  88. on F.Aweek = S.Aweek) Hos
  89. on Hos.Aweek = mem.Aweek
  90.  
  91. /*################################### Cars */
  92.  
  93. left join
  94. (select A.week as Aweek,
  95. Count(distinct A.lid) as cars_listed,
  96. count(distinct case when (A.listing_type = 'Individual' and A.insurance_package = 'Trevo-Shield') then A.lid end) as Individual_trevo_Sheild_Cars,
  97. count(distinct case when (A.listing_type = 'Individual' and A.insurance_package = 'Normal') then A.lid end) as Individual_no_insurance_Cars,
  98. count(distinct case when A.listing_type = 'Fleet' then A.lid end) as Fleet_Cars,
  99. B.Individual_trevo_Sheild_cars as Active_Trevosheild_Cars,
  100. B.Individual_no_insurance_cars as Active_NO_Trevosheild_Cars,
  101. B.Fleet_cars as Active_Fleet_Cars
  102. from
  103. (select distinct l.id as lid,l.status as status,
  104. cu.id as cuid,
  105. li.insurance_package_id as insurance_id,
  106. CASE WHEN cu.id IS NOT NULL THEN 'Fleet' ELSE 'Individual' END listing_type,
  107. CASE WHEN li.insurance_package_id=2 THEN 'Trevo-Shield' ELSE 'Normal' END insurance_package,
  108. extract('week' from l.created_at) as week
  109. from listings l
  110. LEFT JOIN company_users cu ON cu.user_id=l.user_id
  111. LEFT join listing_insurance li on li.listing_id = l.id
  112. WHERE l.status='approved' AND l.deleted_at IS NULL) A
  113. left join
  114. (select I.Aweek as Aweek,
  115. count(I.bid) as Reservations,
  116. count(distinct case when (I.listing_type = 'Individual' and I.insurance_package = 'Trevo-Shield') then I.car_id end) as Individual_trevo_Sheild_cars,
  117. count(distinct case when (I.listing_type = 'Individual' and I.insurance_package = 'Normal') then I.car_id end) as Individual_no_insurance_cars,
  118. count(distinct case when I.listing_type = 'Fleet' then I.car_id end) as Fleet_cars
  119. from
  120. (select
  121. distinct b.id as bid,
  122. extract('week' from b.created_at) as Aweek,
  123. b.listing_id as car_id,
  124. sum(case when b.status not IN('cancelled', 'payment_failed', 'withdrawn', 'rejected', 'accepted') then b.gross_amount end) as comp_gross,
  125. sum(case when b.status not IN('cancelled', 'payment_failed', 'withdrawn', 'rejected', 'accepted') then b.net_amount end) as comp_net,
  126. sum(case when b.status not IN('cancelled', 'payment_failed', 'withdrawn', 'rejected', 'accepted') then b.gross_amount end) -
  127. sum(case when b.status not IN('cancelled', 'payment_failed', 'withdrawn', 'rejected', 'accepted') then b.net_amount end) as coupon,
  128. ROUND((EXTRACT(EPOCH FROM b.booking_end_at) - EXTRACT(EPOCH FROM b.booking_start_at))/3600) as dur,
  129. l.status as listing_status, cu.id as cuid, li.insurance_package_id as insurance_id,
  130. CASE WHEN cu.id IS NOT NULL THEN 'Fleet' ELSE 'Individual' END listing_type,
  131. CASE WHEN li.insurance_package_id=2 THEN 'Trevo-Shield' ELSE 'Normal' END insurance_package
  132. from bookings b
  133. left join listings l on l.id = b.listing_id
  134. LEFT JOIN company_users cu ON cu.user_id=l.user_id
  135. LEFT join listing_insurance li on li.listing_id = l.id
  136. where b.status not IN('cancelled', 'payment_failed', 'withdrawn', 'rejected', 'accepted')
  137. and b.booking_end_at is not null
  138. group by 1,l.status,cuid,li.insurance_package_id) as I
  139. group by 1
  140. order by 1 desc)B
  141. on A.week = B.Aweek
  142. group by A.week, B.Individual_trevo_Sheild_cars,B.Individual_no_insurance_cars,B.Fleet_cars) cars
  143. on cars.Aweek = Hos.Aweek
  144.  
  145. /*################################### Revenue and Reservations */
  146.  
  147. left join
  148. (select I.Aweek as Aweek,
  149. count(I.bid) as Reservations,
  150. count(distinct case when (I.listing_type = 'Individual' and I.insurance_package = 'Trevo-Shield') then I.bid end) as Individual_trevo_Sheild_Resv,
  151. count(distinct case when (I.listing_type = 'Individual' and I.insurance_package = 'Normal') then I.bid end) as Individual_no_insurance_resv,
  152. count(distinct case when I.listing_type = 'Fleet' then I.bid end) as Fleet_resv,
  153. sum(case when (I.listing_type = 'Individual' and I.insurance_package = 'Trevo-Shield') then I.dur end) as Individual_trevo_Sheild_dur,
  154. sum(case when (I.listing_type = 'Individual' and I.insurance_package = 'Normal') then I.dur end) as Individual_no_insurance_dur,
  155. sum(case when I.listing_type = 'Fleet' then I.dur end) as Fleet_dur,
  156. sum(I.comp_gross) as comp_gross,
  157. sum(case when (I.listing_type = 'Individual' and I.insurance_package = 'Trevo-Shield') then I.comp_gross end) as Individual_trevo_Sheild_comp_gross,
  158. sum(case when (I.listing_type = 'Individual' and I.insurance_package = 'Normal') then I.comp_gross end) as Individual_no_insurance_comp_gross,
  159. sum(case when I.listing_type = 'Fleet' then I.comp_gross end) as Fleet_comp_gross,
  160. sum(case when (I.listing_type = 'Individual' and I.insurance_package = 'Trevo-Shield') then I.comp_net end) as Individual_trevo_Sheild_comp_net,
  161. sum(case when (I.listing_type = 'Individual' and I.insurance_package = 'Normal') then I.comp_net end) as Individual_no_insurance_comp_net,
  162. sum(case when I.listing_type = 'Fleet' then I.comp_net end) as Fleet_comp_net,
  163. sum(case when (I.listing_type = 'Individual' and I.insurance_package = 'Trevo-Shield') then I.coupon end) as Individual_trevo_Sheild_coupon,
  164. sum(case when (I.listing_type = 'Individual' and I.insurance_package = 'Normal') then I.coupon end) as Individual_no_insurance_coupon,
  165. sum(case when I.listing_type = 'Fleet' then I.coupon end) as Fleet_coupon
  166. from
  167. (select
  168. distinct b.id as bid,
  169. extract('week' from b.created_at) as Aweek,
  170. b.listing_id as car_id,
  171. sum(case when b.status not IN('cancelled', 'payment_failed', 'withdrawn', 'rejected', 'accepted') then b.gross_amount end) as comp_gross,
  172. sum(case when b.status not IN('cancelled', 'payment_failed', 'withdrawn', 'rejected', 'accepted') then b.net_amount end) as comp_net,
  173. sum(case when b.status not IN('cancelled', 'payment_failed', 'withdrawn', 'rejected', 'accepted') then b.gross_amount end) -
  174. sum(case when b.status not IN('cancelled', 'payment_failed', 'withdrawn', 'rejected', 'accepted') then b.net_amount end) as coupon,
  175. ROUND((EXTRACT(EPOCH FROM b.booking_end_at) - EXTRACT(EPOCH FROM b.booking_start_at))/3600) as dur,
  176. l.status as listing_status, cu.id as cuid, li.insurance_package_id as insurance_id,
  177. CASE WHEN cu.id IS NOT NULL THEN 'Fleet' ELSE 'Individual' END listing_type,
  178. CASE WHEN li.insurance_package_id=2 THEN 'Trevo-Shield' ELSE 'Normal' END insurance_package
  179. from bookings b
  180. left join listings l on l.id = b.listing_id
  181. LEFT JOIN company_users cu ON cu.user_id=l.user_id
  182. LEFT join listing_insurance li on li.listing_id = l.id
  183. where b.status not IN('cancelled', 'payment_failed', 'withdrawn', 'rejected', 'accepted')
  184. group by 1,l.status,cuid,li.insurance_package_id) as I
  185. group by 1
  186. order by 1 asc) Rev
  187. on Rev.Aweek = Hos.Aweek
  188. order by 1 asc
Advertisement
Add Comment
Please, Sign In to add comment