寻找MAX

时间:2012-10-18 11:20:37

标签: sql oracle top-n

  

可能重复:
  Oracle SQL - How to Retrieve highest 5 values of a column

我对一个对SQL有所了解的人有一个简单的问题,但由于我很新,虽然我尝试了很多不同的方法,但我似乎永远无法使语法正确。我想只显示MAX结果的普通酒店。目前它显示所有酒店的平均值。这是我可以使用MAX功能吗?我正在使用oracle

SELECT HOTEL.H_NAME "HOTEL NAME", ROUND(AVG (R_PRICE), 1) "AVERAGE ROOM PRICE"
FROM ROOM JOIN HOTEL ON HOTEL.HOTEL_NO = ROOM.HOTEL_NO
WHERE HOTEL.H_NAME = 'Shangra_La'
OR HOTEL.H_NAME = 'Hilton'
OR HOTEL.H_NAME = 'Sheraton'
GROUP BY HOTEL.H_NAME
ORDER BY HOTEL.H_NAME;

1 个答案:

答案 0 :(得分:0)

在Oracle中,您可以使用ROWNUM伪列。

SELECT * FROM
(
    SELECT 
        HOTEL.H_NAME "HOTEL NAME", 
        ROUND(AVG (R_PRICE), 1) "AVERAGE ROOM PRICE" 
    FROM
        ROOM 
            JOIN HOTEL ON HOTEL.HOTEL_NO = ROOM.HOTEL_NO 
    WHERE HOTEL.H_NAME IN ('Shangra_La','Hilton','Sheraton')
    GROUP BY HOTEL.H_NAME 
) v
WHERE ROWNUM = 1
ORDER BY "AVERAGE ROOM PRICE" DESC;