Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- select
- extract('week' from A.date) as month,
- count(distinct B.lid),
- count(distinct case when (B.listing_type = 'Individual' and B.insurance_package = 'Trevo-Shield') then B.lid end) as Individual_trevo_Sheild_Cars,
- count(distinct case when (B.listing_type = 'Individual' and B.insurance_package = 'Normal') then B.lid end) as Individual_no_insurance_Cars,
- count(distinct case when B.listing_type = 'Fleet' then B.lid end) as Fleet_Cars
- from
- (select distinct l.id as lid, dates.ymd as date, count(distinct l.id) as cars_P_Day
- from listings l
- join listing_availability la on la.listing_id=l.id
- join (SELECT generate_series(timestamp '2019-10-01', NOW() , interval '1 day')::date ymd) dates on
- dates.ymd between la.start_date::date and la.end_date::date
- where l.status='approved' and l.deleted_at is null
- group by 1,2
- order by dates.ymd desc) A
- left join
- (select distinct l.id as lid,l.status as status,
- cu.id as cuid,
- li.insurance_package_id as insurance_id,
- CASE WHEN cu.id IS NOT NULL THEN 'Fleet' ELSE 'Individual' END listing_type,
- CASE WHEN li.insurance_package_id=2 THEN 'Trevo-Shield' ELSE 'Normal' END insurance_package,
- date_trunc('day', l.created_at)::date as date,
- extract('year' from l.created_at) as Ayear
- from listings l
- LEFT JOIN company_users cu ON cu.user_id=l.user_id
- LEFT join listing_insurance li on li.listing_id = l.id
- WHERE l.status='approved' AND l.deleted_at IS null) B
- on A.lid = B.lid
- group by 1
Advertisement
Add Comment
Please, Sign In to add comment