首页 > 代码库 > SQL学习笔记:选取第N条记录

SQL学习笔记:选取第N条记录

Northwind数据库,选取价格第二高的产品。

有两种方法,一个是用Row_Number()函数:

SELECT productname FROM     (SELECT TOP 2 productname, Row_Number() OVER (ORDER BY unitprice desc) AS rownum     FROM Products) AS tblWHERE rownum = 2;

另一种是对子语句的的结果再进行排序:

SELECT top 1 productname FROM     (SELECT TOP 2 productname,unitprice      FROM Products order by unitprice desc) AS tblorder by tbl.unitprice asc