SHOW:
|
|
- or go back to the newest paste.
| 1 | /* Sorting Algorithm */ | |
| 2 | select A.list_id, A.listing_date, A.car_brand, A.car_model, A.host, A.category, | |
| 3 | A.completed_booking as bookings, coalesce((A.fulfilled_booking)::numeric/nullif((A.fulfilled_booking + A.rejected_booking), 0),0) as fulfillment_rate, | |
| 4 | A.listing_price, (A.listing_price - 150) / 150 as price_diff_median, | |
| 5 | (listing_price - case when (31*insured_amount)/13440 < '20' then '20' else (31*insured_amount)/13440 end)/ | |
| 6 | (case when (31*insured_amount)/13440 < '20' then '20' else (31*insured_amount)/13440 end) as price_diff, coalesce(A.review,0) as review, | |
| 7 | coalesce((A.rental_amount - A.payout_amount) / A.rental_amount, 0) as commission from | |
| 8 | (select l.id as list_id, date_trunc('day', l.created_at)::date as listing_date,
| |
| 9 | br.name as car_brand, m.name as car_model, | |
| 10 | case when u.display_name is not null then u.display_name else concat(u.first_name,' ',u.last_name) end as host, | |
| 11 | case when cu.id is not null then 'Fleet' | |
| 12 | when cu.id is null and li.insurance_package_id = 2 then 'Individual (Trevo-Shield)' | |
| 13 | else 'Individual (Basic)' end category, | |
| 14 | count(distinct case when b.status = 'completed' then b.id end) as completed_booking, | |
| 15 | count(distinct case when b.status in ('accepted', 'completed') then b.id end) as fulfilled_booking,
| |
| 16 | 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,
| |
| 17 | lp.base_price as listing_price, li.insured_amount, | |
| 18 | avg(case when b.status = 'completed' and brv.type = 'renter' then brv.rating end) as review, | |
| 19 | sum(case when b.status = 'completed' then bl.amount end) as rental_amount, | |
| 20 | sum(case when b.status = 'completed' then po.amount end) as payout_amount | |
| 21 | from listings l | |
| 22 | left join models m on l.model_id = m.id | |
| 23 | left join brands br on m.brand_id = br.id | |
| 24 | left join listing_pricing lp on lp.listing_id = l.id | |
| 25 | left join listing_insurance li on li.listing_id =l.id | |
| 26 | left join company_users cu ON cu.user_id = l.user_id | |
| 27 | left join users u on l.user_id = u.id | |
| 28 | left join bookings b on b.listing_id = l.id | |
| 29 | left join booking_reviews brv on brv.booking_id = b.id | |
| 30 | left join payouts po ON po.booking_id=b.id | |
| 31 | left JOIN booking_lines bl ON bl.booking_id=b.id AND bl.slug='rental-fee' | |
| 32 | where l.status = 'approved' and l.deleted_at is null | |
| 33 | group by 1,2,3,4,5,6,10,11) A | |
| 34 | /* select percentile_cont(0.5) WITHIN GROUP (ORDER BY lp.base_price) as median_price from listing_pricing lp */ | |
| 35 | ||
| 36 | /* Car Model */ | |
| 37 | select br.name as car_brand, m.name as car_model, | |
| 38 | count(distinct l.id) as cars, | |
| 39 | count(distinct case when b.status not in ('payment_failed') then b.id end) as requested_booking,
| |
| 40 | count(distinct case when b.status in ('accepted', 'completed') then b.id end) as fulfilled_booking,
| |
| 41 | count(distinct case when b.status = 'completed' then b.id end) as completed_booking, | |
| 42 | sum(case when b.status = 'completed' then b.gross_amount end) as gross_amount, | |
| 43 | sum(case when b.status = 'completed' then b.gross_amount end) - sum(case when b.status = 'completed' then b.net_amount end) as coupon, | |
| 44 | sum(case when b.status = 'completed' then b.net_amount end) as net_amount, | |
| 45 | sum(case when b.status = 'completed' then (EXTRACT(EPOCH FROM b.request_end_at) - EXTRACT(EPOCH FROM b.request_start_at))/86400 end) as dur, | |
| 46 | sum(case when b.status = 'completed' then ceil((EXTRACT(EPOCH FROM b.request_end_at) - EXTRACT(EPOCH FROM b.request_start_at))/86400) end) as adj_dur | |
| 47 | from listings l | |
| 48 | left join models m on l.model_id = m.id | |
| 49 | left join brands br on m.brand_id = br.id | |
| 50 | left join bookings b on b.listing_id = l.id | |
| 51 | where l.status = 'approved' | |
| 52 | and l.deleted_at is null | |
| 53 | group by 1,2 | |
| 54 | order by 1,2 | |
| 55 | ||
| 56 | /* Full List */ | |
| 57 | select l.id, case when u.display_name is not null then u.display_name else concat(u.first_name,' ',u.last_name) end as host, | |
| 58 | case when cu.id is not null then 'Fleet' | |
| 59 | when cu.id is null and li.insurance_package_id = 2 then 'Individual (Trevo-Shield)' | |
| 60 | else 'Individual (Basic)' end category, | |
| 61 | m.type as car_type, br.name as car_brand, m.name as car_model, date_trunc('day', l.created_at)::date as listing_date,
| |
| 62 | lp.base_price as listing_price, li.insured_amount, m.year as purchase_year, | |
| 63 | count(distinct case when b.status = 'completed' then b.id end) as completed_booking | |
| 64 | from listings l | |
| 65 | left join models m on l.model_id = m.id | |
| 66 | left join brands br on m.brand_id = br.id | |
| 67 | left join listing_pricing lp on lp.listing_id = l.id | |
| 68 | left join listing_insurance li on li.listing_id =l.id | |
| 69 | left join company_users cu ON cu.user_id = l.user_id | |
| 70 | left join users u on l.user_id = u.id | |
| 71 | left join bookings b on b.listing_id = l.id | |
| 72 | where l.status = 'approved' | |
| 73 | and l.deleted_at is null | |
| 74 | group by 1,2,3,4,5,6,7,8,9,10 | |
| 75 | order by 2 |