ahmedrahil786

TTT (DAILY) - REVENUES

Mar 5th, 2020
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. select cars.date, cars.cars_P_Day, booking.Bookings , (booking.Bookings -booking.D2D) as not_D2D , booking.D2D, booking.unique_cars, booking.nmembers,
  2. distance.distance as distance, booking.dur as duration, booking.comp_gross as gross_rev, booking.comp_net as net_rev, booking.coupon as coupon_rev
  3. from
  4. (select dates.ymd as date, count(distinct l.id) as cars_P_Day
  5. from listings l
  6. join listing_availability la on la.listing_id=l.id
  7. join (SELECT generate_series(timestamp '2019-10-01', NOW() , interval '1 day')::date ymd) dates on
  8. dates.ymd between la.start_date::date and la.end_date::date
  9. where l.status='approved' and l.deleted_at is null
  10. group by dates.ymd
  11. order by dates.ymd desc) cars
  12. left join
  13. (Select B.date as date, Count(distinct B.bid) as Bookings, Count(distinct B.bdid) as D2D, SUM(B.dur) as dur, Count(distinct B.car_id) as unique_cars,
  14. Count(distinct B.users) as nmembers,
  15. Sum(B.comp_gross) as comp_gross, sum(comp_net) as comp_net, sum(coupon) as coupon from
  16. (select
  17. distinct b.id as bid, bd.delivery_id as bdid,
  18. date_trunc('day', b.request_end_at)::date as date,
  19. b.listing_id as car_id,b.user_id as users,
  20. sum(case when b.status not IN('cancelled', 'payment_failed', 'withdrawn', 'rejected', 'accepted') then b.gross_amount end) as comp_gross,
  21. sum(case when b.status not IN('cancelled', 'payment_failed', 'withdrawn', 'rejected', 'accepted') then b.net_amount end) as comp_net,
  22. sum(case when b.status not IN('cancelled', 'payment_failed', 'withdrawn', 'rejected', 'accepted') then b.gross_amount end) -
  23. sum(case when b.status not IN('cancelled', 'payment_failed', 'withdrawn', 'rejected', 'accepted') then b.net_amount end) as coupon,
  24. ROUND((EXTRACT(EPOCH FROM b.booking_end_at) - EXTRACT(EPOCH FROM b.booking_start_at))/3600) as dur
  25. from bookings b
  26. left join booking_deliveries bd on bd.booking_id = b.id
  27. where b.status not IN('cancelled', 'payment_failed', 'withdrawn', 'rejected', 'accepted')
  28. group by 1,2
  29. order by 1 desc) B
  30. group by 1
  31. order by 1 desc) booking
  32. on booking.date = cars.date
  33. left join
  34. (
  35. select L.date as date, Sum(L.distance) as distance from
  36. (select distinct b.id, date_trunc('day', b.created_at)::date as date,
  37. Case when m2.ends - m1.start between '0' and '1000' then m2.ends - m1.start Else 0 End as distance
  38. from
  39. bookings b left join
  40. (select bi.booking_id as bookingid, bi.mileage as start from booking_inspections bi where bi.type='start') m1
  41. on b.id = m1.bookingid
  42. left join
  43. (select bi.booking_id as bookingid, bi.mileage as ends from booking_inspections bi where bi.type='end') m2
  44. on m1.bookingid = m2.bookingid
  45. where b.status not IN('cancelled', 'payment_failed', 'withdrawn', 'rejected', 'accepted')
  46. and b.id is not null
  47. and b.booking_start_at is not null
  48. order by 1 desc)L
  49. group by 1
  50. order by 1 desc) distance
  51. on distance.date = booking.date
  52. group by 1,2,3,4,5,6,7,8,9,10,11,12
  53. order by 1 desc
Advertisement
Add Comment
Please, Sign In to add comment