我有这个酒店,房间,预订和客人的数据库 我需要得到平均价格高于所有酒店所有房间平均价格的酒店的平均价格。 这是我的数据的样子
Hotel (hotelNo,hotelName,hotelAddress)
Room (hotelNo,roomNo,type,price)
Guest (guestNo,guestName,guestAddress)
Booking (hotelNo,guestNo,dateFrom,dateTo,roomNo)
这是我的查询
SELECT (
SELECT hotelName
from hotel
where hotel.hotelNo = room.hotelNo
) AS "Hotel Name",
AVG(price) t
FROM room
where t<(SELECT AVG(price)from room)
Group by hotelNo
答案 0 :(得分:1)
尝试使用以下内容:
select hotelName
from hotel
where hotelNo in (
select hotelNo
from Room
group by hotelNo
having avg( price ) > select avg( price ) from Room
)
答案 1 :(得分:0)
使用TDQD构建查询 - 测试驱动的查询设计。
SELECT HotelNo, AVG(Price) AS AvgPrice
FROM Room
GROUP BY HotelNo;
SELECT AVG(Price) AS AvgPrice
FROM Room;
SELECT HotelNo
FROM Room
GROUP BY HotelNo
HAVING AVG(Price) > (SELECT AVG(Price) FROM Room)
SELECT HotelNo, HotelName, HotelAddress
FROM Hotel AS H
JOIN (SELECT HotelNo
FROM Room
GROUP BY HotelNo
HAVING AVG(Price) > (SELECT AVG(Price) FROM Room)
) AS E
ON H.HotelNo = E.HotelNo
还有其他方法可以编写最后一个查询。
这是TDQD,因为您可以测试每个阶段以确保获得预期结果,从而建立最终复杂查询正确的信心。如果出现问题,如果您逐步开发查询,则可以减少撤消。