ahmedrahil786

TTT - Cars Infra - Add In search- Look Later - Rahil

Dec 8th, 2019
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. select A.week as week,
  2. Count(distinct A.lid) as cars_listed,
  3. count(distinct case when (A.listing_type = 'Individual' and A.insurance_package = 'Trevo-Shield') then A.lid end) as Individual_trevo_Sheild_Cars,
  4. count(distinct case when (A.listing_type = 'Individual' and A.insurance_package = 'Normal') then A.lid end) as Individual_no_insurance_Cars,
  5. count(distinct case when A.listing_type = 'Fleet' then A.lid end) as Fleet_Cars,
  6. B.Individual_trevo_Sheild_cars as Active_Trevosheild_Cars,
  7. B.Individual_no_insurance_cars as Active_NO_Trevosheild_Cars,
  8. B.Fleet_cars as Active_Fleet_Cars
  9. from
  10. (select distinct l.id as lid,l.status as status,
  11. cu.id as cuid,
  12. li.insurance_package_id as insurance_id,
  13. CASE WHEN cu.id IS NOT NULL THEN 'Fleet' ELSE 'Individual' END listing_type,
  14. CASE WHEN li.insurance_package_id=2 THEN 'Trevo-Shield' ELSE 'Normal' END insurance_package,
  15. extract('week' from l.created_at) as week
  16. from listings l
  17. LEFT JOIN company_users cu ON cu.user_id=l.user_id
  18. LEFT join listing_insurance li on li.listing_id = l.id
  19. WHERE l.status='approved' AND l.deleted_at IS NULL) A
  20. left join
  21. (select I.AWeek as Aweek,
  22. count(I.bid) as Reservations,
  23. count(distinct case when (I.listing_type = 'Individual' and I.insurance_package = 'Trevo-Shield') then I.car_id end) as Individual_trevo_Sheild_cars,
  24. count(distinct case when (I.listing_type = 'Individual' and I.insurance_package = 'Normal') then I.car_id end) as Individual_no_insurance_cars,
  25. count(distinct case when I.listing_type = 'Fleet' then I.car_id end) as Fleet_cars
  26. from
  27. (select
  28. distinct b.id as bid,
  29. extract('week' from b.created_at) as AWeek,
  30. b.listing_id as car_id,
  31. sum(case when b.status not IN('cancelled', 'payment_failed', 'withdrawn', 'rejected', 'accepted') then b.gross_amount end) as comp_gross,
  32. sum(case when b.status not IN('cancelled', 'payment_failed', 'withdrawn', 'rejected', 'accepted') then b.net_amount end) as comp_net,
  33. sum(case when b.status not IN('cancelled', 'payment_failed', 'withdrawn', 'rejected', 'accepted') then b.gross_amount end) -
  34. sum(case when b.status not IN('cancelled', 'payment_failed', 'withdrawn', 'rejected', 'accepted') then b.net_amount end) as coupon,
  35. ROUND((EXTRACT(EPOCH FROM b.booking_end_at) - EXTRACT(EPOCH FROM b.booking_start_at))/3600) as dur,
  36. l.status as listing_status, cu.id as cuid, li.insurance_package_id as insurance_id,
  37. CASE WHEN cu.id IS NOT NULL THEN 'Fleet' ELSE 'Individual' END listing_type,
  38. CASE WHEN li.insurance_package_id=2 THEN 'Trevo-Shield' ELSE 'Normal' END insurance_package
  39. from bookings b
  40. left join listings l on l.id = b.listing_id
  41. LEFT JOIN company_users cu ON cu.user_id=l.user_id
  42. LEFT join listing_insurance li on li.listing_id = l.id
  43. where b.status not IN('cancelled', 'payment_failed', 'withdrawn', 'rejected', 'accepted')
  44. and b.booking_end_at is not null
  45. group by 1,l.status,cuid,li.insurance_package_id) as I
  46. group by 1
  47. order by 1 desc)B
  48. on A.week = B.Aweek
  49. group by A.week, B.Individual_trevo_Sheild_cars,B.Individual_no_insurance_cars,B.Fleet_cars
Advertisement
Add Comment
Please, Sign In to add comment