Advertisement
ahmedrahil786

TTT - REV + RESV - New V2.0

Dec 8th, 2019
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. select I.Amonth as Amonth,
  2. count(I.bid) as Reservations,
  3. count(distinct case when (I.listing_type = 'Individual' and I.insurance_package = 'Trevo-Shield') then I.bid end) as Individual_trevo_Sheild_Resv,
  4. count(distinct case when (I.listing_type = 'Individual' and I.insurance_package = 'Normal') then I.bid end) as Individual_no_insurance_resv,
  5. count(distinct case when I.listing_type = 'Fleet' then I.bid end) as Fleet_resv,
  6. sum(case when (I.listing_type = 'Individual' and I.insurance_package = 'Trevo-Shield') then I.dur end) as Individual_trevo_Sheild_dur,
  7. sum(case when (I.listing_type = 'Individual' and I.insurance_package = 'Normal') then I.dur end) as Individual_no_insurance_dur,
  8. sum(case when I.listing_type = 'Fleet' then I.dur end) as Fleet_dur,
  9. sum(I.comp_gross) as comp_gross,
  10. sum(case when (I.listing_type = 'Individual' and I.insurance_package = 'Trevo-Shield') then I.comp_gross end) as Individual_trevo_Sheild_comp_gross,
  11. sum(case when (I.listing_type = 'Individual' and I.insurance_package = 'Normal') then I.comp_gross end) as Individual_no_insurance_comp_gross,
  12. sum(case when I.listing_type = 'Fleet' then I.comp_gross end) as Fleet_comp_gross,
  13. sum(case when (I.listing_type = 'Individual' and I.insurance_package = 'Trevo-Shield') then I.comp_net end) as Individual_trevo_Sheild_comp_net,
  14. sum(case when (I.listing_type = 'Individual' and I.insurance_package = 'Normal') then I.comp_net end) as Individual_no_insurance_comp_net,
  15. sum(case when I.listing_type = 'Fleet' then I.comp_net end) as Fleet_comp_net,
  16. sum(case when (I.listing_type = 'Individual' and I.insurance_package = 'Trevo-Shield') then I.coupon end) as Individual_trevo_Sheild_coupon,
  17. sum(case when (I.listing_type = 'Individual' and I.insurance_package = 'Normal') then I.coupon end) as Individual_no_insurance_coupon,
  18. sum(case when I.listing_type = 'Fleet' then I.coupon end) as Fleet_coupon
  19. from
  20. (select
  21. distinct b.id as bid,
  22. extract('month' from b.created_at) as Amonth,
  23. b.listing_id as car_id,
  24. sum(case when b.status not IN('cancelled', 'payment_failed', 'withdrawn', 'rejected', 'accepted') then b.gross_amount end) as comp_gross,
  25. sum(case when b.status not IN('cancelled', 'payment_failed', 'withdrawn', 'rejected', 'accepted') then b.net_amount end) as comp_net,
  26. sum(case when b.status not IN('cancelled', 'payment_failed', 'withdrawn', 'rejected', 'accepted') then b.gross_amount end) -
  27. sum(case when b.status not IN('cancelled', 'payment_failed', 'withdrawn', 'rejected', 'accepted') then b.net_amount end) as coupon,
  28. ROUND((EXTRACT(EPOCH FROM b.booking_end_at) - EXTRACT(EPOCH FROM b.booking_start_at))/3600) as dur,
  29. l.status as listing_status, cu.id as cuid, li.insurance_package_id as insurance_id,
  30. CASE WHEN cu.id IS NOT NULL THEN 'Fleet' ELSE 'Individual' END listing_type,
  31. CASE WHEN li.insurance_package_id=2 THEN 'Trevo-Shield' ELSE 'Normal' END insurance_package
  32. from bookings b
  33. left join listings l on l.id = b.listing_id
  34. LEFT JOIN company_users cu ON cu.user_id=l.user_id
  35. LEFT join listing_insurance li on li.listing_id = l.id
  36. where b.status not IN('cancelled', 'payment_failed', 'withdrawn', 'rejected', 'accepted')
  37. group by 1,l.status,cuid,li.insurance_package_id) as I
  38. group by 1
  39. order by 1 asc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement