Northwind数据库,选取价格第二高的产品。
有两种方法,一个是用Row_Number()函数:
SELECT productname FROM (SELECT TOP 2 productname, Row_Number() OVER (ORDER BY unitprice desc) AS rownum FROM Products) AS tbl WHERE rownum = 2;
另一种是对子语句的的结果再进行排序:
SELECT top 1 productname FROM (SELECT TOP 2 productname,unitprice FROM Products order by unitprice desc) AS tbl order by tbl.unitprice asc