简体   繁体   中英

Top N Query - SQL Server 2008

How would i return Top N Queries from SQL Server. I know how it is done in Oracle

SELECT Empno, Ename, Job, Mgr, Hiredate, Sal FROM (SELECT Empno, Ename, Job, Mgr, Hiredate, Sal FROM Emp ORDER BY NVL(Sal, 0) DESC) WHERE ROWNUM < 6;

But how is the same query written in SQL Server ?. I never worked on SQL Server. So any classic solution is appreciated.

Thanks

在SQL Server中,您可以实现以下行为:

SELECT TOP 6 Empno, Ename, Job, Mgr, Hiredate, Sal FROM Emp ORDER BY NULLIF(Sal, 0) DESC;

这很容易:

SELECT TOP 100 ....

你可以试试

select top <n> ....

我认为您正在寻找的是select top N ,像这样:

SELECT TOP 6 Empno, Ename, Job, Mgr, Hiredate, Sal FROM (SELECT Empno, Ename, Job, Mgr, Hiredate, Sal FROM Emp ORDER BY NVL(Sal, 0) DESC);

Just use TOP in the select to get the first values acording to the order clause, or if it doesn´t exisits, acording to the key or indexes.

SELECT  top <n> *
FROM    Table1
ORDER BY OrderCol <desc>

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM