I am trying with Oracle top-N query to get number of rows assign an increasing number to each row in the STUD table after sorting by dClass DESC.
select studName, dClass,row_number() over (order by dClass desc) rn
from STUD where row_number() over (order by dClass desc) <= 3 order by dClass desc;
Below is the ERROR
ERROR at line 6:
ORA-30483: window functions are not allowed here.
Here, What change i have do get the records.
Use a subquery to restrict based on the row number you assign:
SELECT studName, dClass
FROM
(
SELECT studName, dClass, ROW_NUMBER() OVER (ORDER BY dClass desc) rn
FROM STUD
) t
WHERE rn <= 3
ORDER BY dClass DESC;
The reason for the error is that the WHERE
clause is applied before the row number in the SELECT
clause has even been computed. In other word, it is not yet available, but wrapping with a subquery gets around this problem.
If you are using Oracle 12c, this can be done using FETCH FIRST n ROWS ONLY
SELECT studName, dClass
FROM STUD
ORDER BY dClass DESC
FETCH FIRST 3 ROWS ONLY;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.