ahmedrahil786

TTT - KID Query

Jun 9th, 2020
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.64 KB | None | 0 0
  1. /* All data are in "Month". CTRL+F to replace to "Week" and "IYYY-IW" */
  2.  
  3. select ren.month, ren.signup, ren.doc_upload, ren.doc_approve, ren.MFT, ren.active_renter, hos.signup, hos.MFT, hos.active_host,
  4. --
  5. lis.cumulative_cars, lis.listed_cars, lis.ind_shield_cars, lis.ind_basic_cars, lis.fleet_cars, lis.available_cars, lis.available_shield_cars,
  6. lis.available_basic_cars, lis.available_fleet_cars, lis.active_cars, lis.active_shield_cars, lis.active_basic_cars, lis.active_fleet_cars,
  7. --
  8. car.signup, car.doc_upload, car.doc_approve, car.MFT,
  9. --
  10. res.requested_booking, res.fulfilled_booking, res.unfulfilled_booking, res.rejected_booking, res.rejected_auto_booking, res.cancel_withdraw_booking,
  11. res.pay_fail_booking, res.came_back, res.completed_booking, res.ind_shield_resv, res.ind_basic_resv, res.fleet_resv, res.D2D, res.non_D2D,
  12. --
  13. dis.total_dist, dis.ind_shield_dist, dis.ind_basic_dist, dis.fleet_dist, dis.total_dur, dis.ind_shield_dur, dis.ind_basic_dur, dis.fleet_dur,
  14. dis.total_adj_dur, dis.ind_shield_adj_dur, dis.ind_basic_adj_dur, dis.fleet_adj_dur,
  15. --
  16. rev.total_gross, rev.ind_shield_gross, rev.ind_basic_gross, rev.fleet_gross, rev.total_coupon, rev.ind_shield_coupon,
  17. rev.ind_basic_coupon, rev.fleet_coupon, rev.total_net, rev.ind_shield_net, rev.ind_basic_net, rev.fleet_net,
  18. rev.payout_host, rev.real_gross, rev.real_net from
  19. --------------------------------------------
  20. --
  21. ----> Renters
  22. --
  23. --------------------------------------------
  24. (select A.month, A.signup, C.doc_upload, C.doc_approve,
  25. D.MFT, E.active_renter from
  26. (select to_char(u.created_at, 'YYYY-MM') as month,
  27. count(distinct u.id) as signup
  28. from users u
  29. group by 1) A
  30. left join
  31. (select to_char(B.overall_date, 'YYYY-MM') as month,
  32. count(distinct case when B.upload_status = 'Yes' then B.id end) as doc_upload,
  33. count(distinct case when B.upload_status = 'Yes' and B.approve_status = 'Yes' then B.id end) as doc_approve from
  34. (select u.created_at, u.id, selfic.status, selfic.date, license.status, license.date, ic.status, ic.date,
  35. case when ic.date > '2020-04-05' and selfic.status is not null and license.status is not null and ic.status is not null then 'Yes'
  36. when selfic.date < '2020-04-06' and selfic.status is not null and license.status is not null then 'Yes'
  37. else 'No' end as upload_status,
  38. case when selfic.status is null and license.status is null and ic.status is null then 'null'
  39. when selfic.date < '2020-04-06' and license.date < '2020-04-06' and selfic.status = 'approved' and license.status = 'approved' then 'Yes'
  40. when ic.date > '2020-04-05' and selfic.status = 'approved' and license.status = 'approved' and ic.status = 'approved' then 'Yes'
  41. else 'No' end as approve_status,
  42. case when (selfic.date > license.date or license.date is null) and (selfic.date > ic.date or ic.date is null) then selfic.date
  43. when (license.date > selfic.date or selfic.date is null) and (license.date > ic.date or ic.date is null) then license.date
  44. when (ic.date > selfic.date or selfic.date is null) and (ic.date > license.date or license.date is null) then ic.date end as overall_date
  45. from users u
  46. left join
  47. (select k.user_id, k.status, k.created_at as date from user_kyc as k
  48. join kyc_types as t on k.kyc_type_id = t.id
  49. where t.slug = 'selfie-ic-passport') as selfic on u.id = selfic.user_id
  50. left join
  51. (select k.user_id, k.status, k.created_at as date from user_kyc as k
  52. join kyc_types as t on k.kyc_type_id = t.id
  53. where t.slug = 'driver-license') as license on u.id = license.user_id
  54. left join
  55. (select k.user_id, k.status, k.created_at as date from user_kyc as k
  56. join kyc_types as t on k.kyc_type_id = t.id
  57. where t.slug = 'ic-passport') as ic on u.id = ic.user_id) B
  58. where B.overall_date is not null
  59. group by 1) C
  60. on A.month = C.month
  61. left join
  62. (SELECT to_char(b.request_start_at, 'YYYY-MM') as month, count(b.id) as MFT FROM bookings b
  63. JOIN users u ON b.user_id = u.id
  64. WHERE b.id IN (SELECT min(id) FROM bookings WHERE status IN ('completed') GROUP BY user_id)
  65. GROUP BY 1) D
  66. on A.month = D.month
  67. left join
  68. (select to_char(b.request_start_at, 'YYYY-MM') as month,
  69. count(distinct case when b.status = 'completed' then b.user_id end) as active_renter
  70. from bookings b
  71. group by 1) E
  72. on A.month = E.month
  73. order by 1 desc) ren
  74. --------------------------------------------
  75. --
  76. ----> Hosts
  77. --
  78. --------------------------------------------
  79. left join
  80. (select B.month, B.signup, C.MFT, D.active_host from
  81. (select to_char(A.created_at, 'YYYY-MM') as month, count(distinct A.user_id) as signup from
  82. (SELECT tmp.user_id, tmp.created_at FROM
  83. (SELECT l.user_id, l.created_at, ROW_NUMBER() OVER (PARTITION BY (l.user_id) ORDER BY created_at) rn
  84. FROM listings l) tmp
  85. WHERE rn = 1) A
  86. group by 1) B
  87. left join
  88. (SELECT to_char(b.request_start_at, 'YYYY-MM') as month, count(b.id) as MFT FROM bookings b
  89. JOIN listings l ON b.listing_id = l.id
  90. WHERE b.id in (SELECT min(b.id) FROM bookings b
  91. join listings l on b.listing_id = l.id
  92. WHERE b.status IN ('completed') GROUP BY l.user_id)
  93. GROUP BY 1) C
  94. on B.month = C.month
  95. left join
  96. (select to_char(b.request_start_at, 'YYYY-MM') as month,
  97. count(distinct case when b.status = 'completed' then l.user_id end) as active_host from listings l
  98. join bookings b on b.listing_id = l.id
  99. group by 1) D
  100. on B.month = D.month
  101. order by 1 desc) hos
  102. on ren.month = hos.month
  103. --------------------------------------------
  104. --
  105. ----> Listings (Component 1)
  106. --
  107. --------------------------------------------
  108. left join
  109. (select E.month, E.cumulative_cars as cumulative_cars,
  110. sum(L.listed_cars) over (rows between unbounded preceding and current row) as listed_cars,
  111. sum(L.ind_shield_cars) over (rows between unbounded preceding and current row) as ind_shield_cars,
  112. sum(L.ind_basic_cars) over (rows between unbounded preceding and current row) as ind_basic_cars,
  113. sum(L.fleet_cars) over (rows between unbounded preceding and current row) as fleet_cars,
  114. E.available_cars as available_cars, E.ind_shield_cars as available_shield_cars,
  115. E.ind_basic_cars as available_basic_cars, E.fleet_cars as available_fleet_cars,
  116. G.active_cars as active_cars, G.ind_shield_cars as active_shield_cars,
  117. G.ind_basic_cars as active_basic_cars, G.fleet_cars as active_fleet_cars
  118. from
  119. (select B.month, case when sum(D.remove_cum_cars) is null then sum(B.cum_cars) else
  120. sum(B.cum_cars) - sum(D.remove_cum_cars) end as cumulative_cars,
  121. case when sum(D.remove_avai_cars) is null then sum(B.avai_cars)
  122. else sum(B.avai_cars) - sum(D.remove_avai_cars) end as available_cars,
  123. case when sum(D.remove_ind_shield_cars) is null then sum(B.avai_ind_shield_cars)
  124. else sum(B.avai_ind_shield_cars) - sum(D.remove_ind_shield_cars) end as ind_shield_cars,
  125. case when sum(D.remove_ind_basic_cars) is null then sum(B.avai_ind_basic_cars)
  126. else sum(B.avai_ind_basic_cars) - sum(D.remove_ind_basic_cars) end as ind_basic_cars,
  127. case when sum(D.remove_fleet_cars) is null then sum(B.avai_fleet_cars)
  128. else sum(B.avai_fleet_cars) - sum(D.remove_fleet_cars) end as fleet_cars from
  129. (select to_char(A.date, 'YYYY-MM') as month, count(A.lid) as cum_cars, count(distinct A.lid) as avai_cars,
  130. count(distinct case when A.category = 'Individual (Trevo-Shield)' then A.lid end) as avai_ind_shield_cars,
  131. count(distinct case when A.category = 'Individual (Basic)' then A.lid end) as avai_ind_basic_cars,
  132. count(distinct case when A.category = 'Fleet' then A.lid end) as avai_fleet_cars from
  133. (select distinct l.id as lid, dates.ymd as date,
  134. case when cu.id is not null then 'Fleet'
  135. when cu.id is null and li.insurance_package_id = 2 then 'Individual (Trevo-Shield)'
  136. else 'Individual (Basic)' end category
  137. from listings l
  138. join listing_availability la on la.listing_id=l.id
  139. join (SELECT generate_series(timestamp '2019-10-01', NOW() , interval '1 day')::date ymd) dates on
  140. dates.ymd between la.start_date::date and la.end_date::date
  141. LEFT JOIN company_users cu ON cu.user_id=l.user_id
  142. LEFT join listing_insurance li on li.listing_id = l.id
  143. where l.status='approved') A
  144. group by 1) B
  145. left join
  146. (select to_char(C.date, 'YYYY-MM') as month, count(C.lid) as remove_cum_cars, count(distinct C.lid) as remove_avai_cars,
  147. count(distinct case when C.category = 'Individual (Trevo-Shield)' then C.lid end) as remove_ind_shield_cars,
  148. count(distinct case when C.category = 'Individual (Basic)' then C.lid end) as remove_ind_basic_cars,
  149. count(distinct case when C.category = 'Fleet' then C.lid end) as remove_fleet_cars from
  150. (select distinct l.id as lid, dates.ymd as date,
  151. case when cu.id is not null then 'Fleet'
  152. when cu.id is null and li.insurance_package_id = 2 then 'Individual (Trevo-Shield)'
  153. else 'Individual (Basic)' end category
  154. from listings l
  155. join listing_availability la on la.listing_id=l.id
  156. join (SELECT generate_series(timestamp '2019-10-01', NOW() , interval '1 day')::date ymd) dates on
  157. dates.ymd between l.deleted_at::date and la.end_date::date
  158. LEFT JOIN company_users cu ON cu.user_id=l.user_id
  159. LEFT join listing_insurance li on li.listing_id = l.id
  160. where l.status='approved' and la.start_date <= dates.ymd) C
  161. group by 1) D
  162. on B.month = D.month
  163. group by 1) E
  164. left join
  165. (select F.month, count(distinct F.car_id) as active_cars,
  166. count(distinct case when F.category = 'Individual (Trevo-Shield)' then F.car_id end) as ind_shield_cars,
  167. count(distinct case when F.category = 'Individual (Basic)' then F.car_id end) as ind_basic_cars,
  168. count(distinct case when F.category = 'Fleet' then F.car_id end) as fleet_cars from
  169. (select distinct to_char(b.request_start_at, 'YYYY-MM') as month, b.listing_id as car_id,
  170. case when cu.id is not null then 'Fleet'
  171. when cu.id is null and li.insurance_package_id = 2 then 'Individual (Trevo-Shield)'
  172. else 'Individual (Basic)' end category
  173. from bookings b
  174. left join listings l on l.id = b.listing_id
  175. left join company_users cu ON cu.user_id = l.user_id
  176. left join listing_insurance li on li.listing_id = l.id
  177. where b.status = 'completed') F
  178. group by 1) G
  179. on E.month = G.month
  180. left join
  181. (select case when I.month is null then K.month else I.month end as month,
  182. case when sum(K.remove_cars) is null then sum(I.all_cars)
  183. when sum(I.all_cars) is null then - sum(K.remove_cars)
  184. else sum(I.all_cars) - sum(K.remove_cars) end as listed_cars,
  185. case when sum(K.remove_ind_shield_cars) is null then sum(I.all_ind_shield_cars)
  186. when sum(I.all_ind_shield_cars) is null then - sum(K.remove_ind_shield_cars)
  187. else sum(I.all_ind_shield_cars) - sum(K.remove_ind_shield_cars) end as ind_shield_cars,
  188. case when sum(K.remove_ind_basic_cars) is null then sum(I.all_ind_basic_cars)
  189. when sum(I.all_ind_basic_cars) is null then - sum(K.remove_ind_basic_cars)
  190. else sum(I.all_ind_basic_cars) - sum(K.remove_ind_basic_cars) end as ind_basic_cars,
  191. case when sum(K.remove_fleet_cars) is null then sum(I.all_fleet_cars)
  192. when sum(I.all_fleet_cars) is null then - sum(K.remove_fleet_cars)
  193. else sum(I.all_fleet_cars) - sum(K.remove_fleet_cars) end as fleet_cars from
  194. (select H.month, count(distinct H.lid) as all_cars,
  195. count(distinct case when H.category = 'Individual (Trevo-Shield)' then H.lid end) as all_ind_shield_cars,
  196. count(distinct case when H.category = 'Individual (Basic)' then H.lid end) as all_ind_basic_cars,
  197. count(distinct case when H.category = 'Fleet' then H.lid end) as all_fleet_cars from
  198. (select l.id as lid, to_char(l.created_at, 'YYYY-MM') as month,
  199. case when cu.id is not null then 'Fleet'
  200. when cu.id is null and li.insurance_package_id = 2 then 'Individual (Trevo-Shield)'
  201. else 'Individual (Basic)' end category
  202. from listings l
  203. left join company_users cu ON cu.user_id = l.user_id
  204. left join listing_insurance li on li.listing_id = l.id
  205. where l.status = 'approved') H
  206. group by 1) I
  207. full join
  208. (select J.month, count(distinct J.lid) as remove_cars,
  209. count(distinct case when J.category = 'Individual (Trevo-Shield)' then J.lid end) as remove_ind_shield_cars,
  210. count(distinct case when J.category = 'Individual (Basic)' then J.lid end) as remove_ind_basic_cars,
  211. count(distinct case when J.category = 'Fleet' then J.lid end) as remove_fleet_cars from
  212. (select l.id as lid, to_char(l.deleted_at, 'YYYY-MM') as month,
  213. case when cu.id is not null then 'Fleet'
  214. when cu.id is null and li.insurance_package_id = 2 then 'Individual (Trevo-Shield)'
  215. else 'Individual (Basic)' end category
  216. from listings l
  217. left join company_users cu ON cu.user_id = l.user_id
  218. left join listing_insurance li on li.listing_id = l.id
  219. where l.status = 'approved' and l.deleted_at is not null) J
  220. group by 1) K
  221. on I.month = K.month
  222. group by 1
  223. order by 1) L
  224. on E.month = L.month
  225. order by 1 desc) lis
  226. on ren.month = lis.month
  227. --------------------------------------------
  228. --
  229. ----> Listings (Component 2)
  230. --
  231. --------------------------------------------
  232. left join
  233. (select A.month, A.signup, C.doc_upload, A.doc_approve, D.MFT from
  234. (select to_char(l.created_at, 'YYYY-MM') as month, count(distinct l.id) as signup,
  235. count(distinct case when l.status = 'approved' then l.id end) as doc_approve
  236. from listings l
  237. group by 1) A
  238. left join
  239. (select to_char(B.created_at, 'YYYY-MM') as month,
  240. count(distinct case when B.upload_status = 'Yes' then B.id end) as doc_upload from
  241. (select l.created_at, l.id, gran.status, gran.date, tax.status, tax.date,
  242. case when gran.status is not null and tax.status is not null then 'Yes' else 'No' end as upload_status,
  243. case when gran.status is null and tax.status is null then 'null'
  244. when gran.status = 'approved' and tax.status = 'approved' then 'Yes'
  245. else 'No' end as approve_status
  246. from listings l
  247. left join
  248. (select ld.listing_id, ld.status, ld.created_at as date from listing_documents ld
  249. join listing_document_types as t on ld.listing_document_type_id = t.id
  250. where t.slug = 'car-grant') as gran on l.id = gran.listing_id
  251. left join
  252. (select ld.listing_id, ld.status, ld.created_at as date from listing_documents ld
  253. join listing_document_types as t on ld.listing_document_type_id = t.id
  254. where t.slug = 'road-tax') as tax on l.id = tax.listing_id) B
  255. group by 1) C
  256. on A.month = C.month
  257. left join
  258. (SELECT to_char(b.request_start_at, 'YYYY-MM') as month, count(b.id) as MFT FROM bookings b
  259. JOIN listings l ON b.listing_id = l.id
  260. WHERE b.id IN (SELECT min(id) FROM bookings WHERE status IN ('completed') GROUP BY listing_id)
  261. GROUP BY 1) D
  262. on A.month = D.month
  263. order by 1 desc) car
  264. on ren.month = car.month
  265. --------------------------------------------
  266. --
  267. ----> Reservations
  268. --
  269. --------------------------------------------
  270. left join
  271. (select A.month, A.unfulfilled_booking + A.accepted_booking + C.completed_booking as requested_booking,
  272. A.accepted_booking + C.completed_booking as fulfilled_booking,
  273. A.unfulfilled_booking, A.rejected_booking, A.rejected_auto_booking, A.cancel_withdraw_booking,
  274. A.pay_fail_booking, E.came_back, C.completed_booking, C.ind_shield_resv, C.ind_basic_resv, C.fleet_resv,
  275. C.D2D, C.non_D2D from
  276. (select to_char(b.created_at, 'YYYY-MM') as month,
  277. count(distinct case when b.status in ('cancelled', 'withdrawn', 'rejected') then b.id end) as unfulfilled_booking,
  278. count(distinct case when b.status = 'rejected' and (b.reason not in ('auto-rejected') or b.reason is null) then b.id end) as rejected_booking,
  279. count(distinct case when b.reason = 'auto-rejected' then b.id end) as rejected_auto_booking,
  280. count(distinct case when b.status in ('cancelled', 'withdrawn') then b.id end) as cancel_withdraw_booking,
  281. count(distinct case when b.status = 'payment_failed' then b.id end) as pay_fail_booking,
  282. count(distinct case when b.status = 'accepted' then b.id end) as accepted_booking
  283. from bookings b
  284. group by 1) A
  285. left join
  286. (select B.month, count(distinct B.bid) as completed_booking,
  287. count(distinct case when B.category = 'Individual (Trevo-Shield)' then B.bid end) as ind_shield_resv,
  288. count(distinct case when B.category = 'Individual (Basic)' then B.bid end) as ind_basic_resv,
  289. count(distinct case when B.category = 'Fleet' then B.bid end) as fleet_resv,
  290. count(distinct B.bdid) as D2D, count(distinct B.bid) - count(distinct B.bdid) as non_D2D from
  291. (select distinct to_char(b.request_start_at, 'YYYY-MM') as month,
  292. b.id as bid, bd.id as bdid, case when cu.id is not null then 'Fleet'
  293. when cu.id is null and li.insurance_package_id = 2 then 'Individual (Trevo-Shield)'
  294. else 'Individual (Basic)' end category
  295. from bookings b
  296. left join listings l on l.id = b.listing_id
  297. LEFT JOIN company_users cu ON cu.user_id=l.user_id
  298. LEFT join listing_insurance li on li.listing_id = l.id
  299. left join booking_deliveries bd on bd.booking_id = b.id
  300. where b.status = 'completed') B
  301. group by 1) C
  302. on A.month = C.month
  303. left join
  304. (select to_char(D.s_created_at, 'YYYY-MM') as month, count(D.user_id) as came_back from
  305. (select s.user_id, min(f.payment_id) as failure_payment_id, min(f.created_at) as f_created_at,
  306. s.payment_id as success_payment_id, s.created_at as s_created_at from
  307. (select pm.user_id, p.id as payment_id, p.status, p.created_at from payments p
  308. join payment_methods pm on pm.id = p.payment_method_id
  309. where p.status= 'failed') f
  310. join
  311. (select pm.user_id, p.id as payment_id, p.status, p.created_at from payments p
  312. join payment_methods pm on pm.id = p.payment_method_id
  313. where p.status='succeeded') s
  314. on f.user_id = s.user_id and s.created_at between f.created_at and f.created_at + interval '30 minutes'
  315. group by s.user_id, s.payment_id, s.created_at) D
  316. group by 1) E
  317. on A.month = E.month
  318. order by 1 desc) res
  319. on ren.month = res.month
  320. --------------------------------------------
  321. --
  322. ----> Distance & Time
  323. --
  324. --------------------------------------------
  325. left join
  326. (select A.month,
  327. sum(A.dist) as total_dist,
  328. sum(case when A.category = 'Individual (Trevo-Shield)' then A.dist end) as ind_shield_dist,
  329. sum(case when A.category = 'Individual (Basic)' then A.dist end) as ind_basic_dist,
  330. sum(case when A.category = 'Fleet' then A.dist end) as fleet_dist,
  331. sum(A.dur) as total_dur,
  332. sum(case when A.category = 'Individual (Trevo-Shield)' then A.dur end) as ind_shield_dur,
  333. sum(case when A.category = 'Individual (Basic)' then A.dur end) as ind_basic_dur,
  334. sum(case when A.category = 'Fleet' then A.dur end) as fleet_dur,
  335. sum(ceil(A.dur)) as total_adj_dur,
  336. sum(case when A.category = 'Individual (Trevo-Shield)' then ceil(A.dur) end) as ind_shield_adj_dur,
  337. sum(case when A.category = 'Individual (Basic)' then ceil(A.dur) end) as ind_basic_adj_dur,
  338. sum(case when A.category = 'Fleet' then ceil(A.dur) end) as fleet_adj_dur from
  339. (select distinct to_char(b.request_start_at, 'YYYY-MM') as month, b.id as bid,
  340. case when cu.id is not null then 'Fleet'
  341. when cu.id is null and li.insurance_package_id = 2 then 'Individual (Trevo-Shield)'
  342. else 'Individual (Basic)' end category,
  343. (EXTRACT(EPOCH FROM b.request_end_at) - EXTRACT(EPOCH FROM b.request_start_at))/86400 as dur,
  344. case when m2.end - m1.start between '0' and '2000' then m2.end - m1.start Else 0 End as dist
  345. from bookings b
  346. left join
  347. (select bi.booking_id as booking_id, bi.mileage as start from booking_inspections bi where bi.type='start') m1
  348. on b.id = m1.booking_id
  349. left join
  350. (select bi.booking_id as booking_id, bi.mileage as end from booking_inspections bi where bi.type= 'end') m2
  351. on b.id = m2.booking_id
  352. left join listings l on l.id = b.listing_id
  353. LEFT JOIN company_users cu ON cu.user_id=l.user_id
  354. LEFT join listing_insurance li on li.listing_id = l.id
  355. where b.status = 'completed') A
  356. group by 1
  357. order by 1 desc) dis
  358. on ren.month = dis.month
  359. --------------------------------------------
  360. --
  361. ----> Revenue (GMV & Real)
  362. --
  363. --------------------------------------------
  364. left join
  365. (select A.month,
  366. sum(A.comp_gross) as total_gross,
  367. sum(case when A.category = 'Individual (Trevo-Shield)' then A.comp_gross end) as ind_shield_gross,
  368. sum(case when A.category = 'Individual (Basic)' then A.comp_gross end) as ind_basic_gross,
  369. sum(case when A.category = 'Fleet' then A.comp_gross end) as fleet_gross,
  370. sum(A.coupon) as total_coupon,
  371. sum(case when A.category = 'Individual (Trevo-Shield)' then A.coupon end) as ind_shield_coupon,
  372. sum(case when A.category = 'Individual (Basic)' then A.coupon end) as ind_basic_coupon,
  373. sum(case when A.category = 'Fleet' then A.coupon end) as fleet_coupon,
  374. sum(A.comp_net) as total_net,
  375. sum(case when A.category = 'Individual (Trevo-Shield)' then A.comp_net end) as ind_shield_net,
  376. sum(case when A.category = 'Individual (Basic)' then A.comp_net end) as ind_basic_net,
  377. sum(case when A.category = 'Fleet' then A.comp_net end) as fleet_net,
  378. sum(A.payout_amount) as payout_host, sum(A.real_gross) as real_gross, sum(A.real_net) as real_net from
  379. (select distinct to_char(b.request_start_at, 'YYYY-MM') as month, b.id as bid,
  380. case when cu.id is not null then 'Fleet'
  381. when cu.id is null and li.insurance_package_id = 2 then 'Individual (Trevo-Shield)'
  382. else 'Individual (Basic)' end category,
  383. sum(case when b.status = 'completed' then b.gross_amount end) as comp_gross,
  384. sum(case when b.status = 'completed' then b.net_amount end) as comp_net,
  385. sum(case when b.status = 'completed' then b.gross_amount end) -
  386. sum(case when b.status = 'completed' then b.net_amount end) as coupon,
  387. sum(po.amount) as payout_amount, sum(b.gross_amount-po.amount) as real_gross, sum(b.net_amount-po.amount) as real_net
  388. from bookings b
  389. left join listings l on l.id = b.listing_id
  390. LEFT JOIN company_users cu ON cu.user_id = l.user_id
  391. LEFT join listing_insurance li on li.listing_id = l.id
  392. left join payouts po ON po.booking_id = b.id
  393. where b.status = 'completed'
  394. group by 1, 2, 3) A
  395. group by 1
  396. order by 1 desc) rev
  397. on ren.month = rev.month
  398. order by 1 desc
  399.  
  400. -------
  401. ----------------
  402. ----------------------------
  403. /* Unique User (Yearly) */
  404. select C.year, C.active_renter, D.active_host from
  405. (select extract('year' from b.request_start_at) as year,
  406. count(distinct case when b.status = 'completed' then b.user_id end) as active_renter
  407. from bookings b
  408. group by 1) C
  409. left join
  410. (select extract('year' from b.request_start_at) as year,
  411. count(distinct case when b.status = 'completed' then l.user_id end) as active_host from listings l
  412. join bookings b on b.listing_id = l.id
  413. group by 1) D
  414. on C.year = D.year
Add Comment
Please, Sign In to add comment