SQL學習筆記:選取第N條記錄

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;

另外一種是對子語句的的結果再進行排序:spa

SELECT top 1 productname FROM 
    (SELECT TOP 2 productname,unitprice 
     FROM Products order by unitprice desc) AS tbl
order by tbl.unitprice asc
相關文章
相關標籤/搜索