我有以下表格结构,我想从预订表中计算所有房间类型。我想要这样的东西: Roomtype :double 3 :simple 5 这有可能吗?
我只计算了预订表中的所有房间
SELECT COUNT(Room_ID)AS NumarRezervari
FROM dbo.Reservation WHERE MONTH(Data_Check_in)=5
我有以下表格结构,我想从预订表中计算所有房间类型。我想要这样的东西: Roomtype :double 3 :simple 5 这有可能吗?
我只计算了预订表中的所有房间
SELECT COUNT(Room_ID)AS NumarRezervari
FROM dbo.Reservation WHERE MONTH(Data_Check_in)=5
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
您需要使用一个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
如果您想计算房间类型的数量,我认为您可以执行以下操作:
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 ...