SQL server 2008
Hello
here is my query which returns the result
SELECT * FROM Rooms
WHERE RoomID in
(SELECT t1.RoomId FROM
(Rooms t1 INNER JOIN
(SELECT RoomID, SUM(quantity) AS QTY FROM Room_Item GROUP BY RoomID
HAVING SUM(Quantity) = 0) t2 ON t1.RoomID = t2.RoomID))
above written query will return me the roomid of the rooms which dosen’t have any items in it (quantity = 0),
but now i want to filter out the result by buildings,
i got the list of room for specific building as below
select roomid from rooms where buildingblockid in (select buidingblockid from buildingblock where buildigID = 1)
so my query will be
return the roomid from rooms table where allocated items are 0 and filter out room by building number = 1
tables structure is as below – ONLY ESSENTIAL FIELDS ARE SHOWN
rooms => roomid(PK), buildingblockID(FK), roomname
room_item => roomitemid(PK), roomid(FK), itemid(FK), quantity
item => itemid(PK), itemname
buildingblock => buildingblockid(PK), buildingID(FK)
building => buildingID(PK), buildingName
You could create derived table to find rooms having no items, and join it to buildingWiseRoom to filter building one.
UPDATE as table structure changed: