ahmedrahil786

TTT -monthly -Rahil

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