Home > Net >  Sql Server Statement to calculate accommodation utilisation
Sql Server Statement to calculate accommodation utilisation

Time:06-15

I want to calculate the total number of Occupied accommodation as a percentage of the total accommodtion available. I am using the query below. I dont think the query is right. It returns error near From. Please can anyone help

SELECT ((COUNT(Accommodation.dbo.House.HouseID) / COUNT(B.HouseID)) * 100) AS [Accommodation Utilisation %]
FROM (SELECT COUNT(B.HouseID)
      FROM Accommodation.dbo.House B
      WHERE Accommodation.dbo.House.STATUS = 'Occupied')
FROM Accommodation.dbo.House

CodePudding user response:

Seems like a much easier way to do this would be to use a conditional AVG:

SELECT AVG(CASE H.STATUS WHEN 'Occupied' THEN 1. ELSE 0. END) AS Utilisation --Don't use names that need to be delimit identified.
FROM dbo.House H; --You should already be connnected to the database Accomodation, so it doesn't need to appear here

db<>fiddle

  • Related