NonplayerCharacter

PostgreSQL | PGExercises.com | Months to repay

Mar 8th, 2022
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- https://pgexercises.com/questions/aggregates/payback.html
  2. -----------------------------
  3. '''
  4. We spent initialoutlay per facility
  5. Every month we spend monthlymaintenance per facility
  6. Every month we receive revenue per facility
  7. Difference between revenue and monthly maintenance is what counts against initialoutlay per facility
  8. '''
  9. -----------------------------
  10.  
  11. with t1 as (select
  12.     name,
  13.     initialoutlay,
  14.     monthlymaintenance,
  15.     sum(slots * (case when memid=0 then guestcost else membercost end))/3 as monthlyrevenue
  16. from cd.facilities f
  17. join cd.bookings b
  18. on f.facid = b.facid
  19. group by 1, 2, 3)
  20.  
  21. select  name,
  22.         initialoutlay/(monthlyrevenue-monthlymaintenance) as months
  23. from t1
  24. order by 1 asc
Add Comment
Please, Sign In to add comment