ahmedrahil786

TTT Search cars weekly- split by host type

Mar 23rd, 2020
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. select
  2. extract('week' from A.date) as month,
  3. count(distinct B.lid),
  4. count(distinct case when (B.listing_type = 'Individual' and B.insurance_package = 'Trevo-Shield') then B.lid end) as Individual_trevo_Sheild_Cars,
  5. count(distinct case when (B.listing_type = 'Individual' and B.insurance_package = 'Normal') then B.lid end) as Individual_no_insurance_Cars,
  6. count(distinct case when B.listing_type = 'Fleet' then B.lid end) as Fleet_Cars
  7. from
  8. (select distinct l.id as lid, dates.ymd as date, count(distinct l.id) as cars_P_Day
  9. from listings l
  10. join listing_availability la on la.listing_id=l.id
  11. join (SELECT generate_series(timestamp '2019-10-01', NOW() , interval '1 day')::date ymd) dates on
  12. dates.ymd between la.start_date::date and la.end_date::date
  13. where l.status='approved' and l.deleted_at is null
  14. group by 1,2
  15. order by dates.ymd desc) A
  16. left join
  17. (select distinct l.id as lid,l.status as status,
  18. cu.id as cuid,
  19. li.insurance_package_id as insurance_id,
  20. CASE WHEN cu.id IS NOT NULL THEN 'Fleet' ELSE 'Individual' END listing_type,
  21. CASE WHEN li.insurance_package_id=2 THEN 'Trevo-Shield' ELSE 'Normal' END insurance_package,
  22. date_trunc('day', l.created_at)::date as date,
  23. extract('year' from l.created_at) as Ayear
  24. from listings l
  25. LEFT JOIN company_users cu ON cu.user_id=l.user_id
  26. LEFT join listing_insurance li on li.listing_id = l.id
  27. WHERE l.status='approved' AND l.deleted_at IS null) B
  28. on A.lid = B.lid
  29. group by 1
Advertisement
Add Comment
Please, Sign In to add comment