Skip to content
Advertisement

SQL: how do you calculate occupancy/ number of in-use for a facility?

I am trying to calculate the number of occupied lockers in the changing room. The dataset goes like this:

My ideal output will be the number of occupied lockers per hour:

I am able to manually calculate the number in excel (‘no. of check-in lockers during the hour’ + ‘no. of lockers in-use during the hour’ – ‘no. of check-out during the hour’), but I am not able to calculate it in SQL-server. I have no idea how to retrieve the no. of lockers in-use in the above captioned equation.

By “occupied”, I mean the locker that is currently being used in the hour. For example, if User A check-in (start using) a locker at 8:30am and check-out (release the locker and stop using it) at 10:30am, then for User A, one “occupied” locker will be counted during 8:00am – 8:59am, 9:00am-9:59am and 10:00am-10:59am respectively.

Advertisement

Answer

You need to generate the hours. Then you can do the calculation. If you want any time during the hour:

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement