2012-05-26 51 views
0

我有以下表格結構,我希望從預訂表中統計所有房間類型。 我想有這樣的事情: 房型:雙人3 :簡單5 有可能做到這一點?按房間類型劃分的SQL計數

enter image description here

我只設法計從預訂表的所有客房

SELECT COUNT(Room_ID)AS NumarRezervari FROM dbo.Reservation WHERE MONTH(Data_Check_in)=5

回答

2
SELECT Room_Type, COUNT(*) AS NumarRezervari 
FROM dbo.Reservation re 
JOIN dbo.Room ro ON ro.RoomID = re.RoomID 
JOIN dbo.Room_Type rt ON rt.Room_Type_ID = ro.Room_Type_ID 
WHERE MONTH(re.Data_Check_in) = 5 
GROUP BY Room_Type 
1

你需要使用一個GROUP BY條款和幾個連接,就像這樣:

SELECT Room_Type, count(*) FROM Reservation rv 
INNER JOIN Room rm ON rm.Room_ID = rv.Room_ID 
INNER JOIN Room_Type rt ON rm.RoomType_ID = rt.RoomType_ID 
GROUP BY Room_Type 
+0

謝謝你,這是我所需要的 – jonny

1

如果您想要計算amo沒有房間類型,我認爲你可以做這樣的事情:

select count (Room_Type_Id) from Room_Type 
inner join Room on Room.Room_Type_ID = Room_Type.RoomType_ID 
inner join Reservation on Reservation.Room_ID = Room.RoomID 
where ...