ORDER BY子句在视图,内联函数,派生表,子查询和公用表expression式中是无效的
ORDER BY子句在视图,内联函数,派生表,子查询和公用表expression式中是无效的,除非还指定了TOP,OFFSET或FOR XML。
我正在尝试执行以下查询时遇到上述错误。 任何人都可以请看看,并告诉我我在这里做错了什么?
SELECT * FROM ( SELECT Stockmain.VRNOA, item.description as item_description, party.name as party_name, stockmain.vrdate, stockdetail.qty, stockdetail.rate, stockdetail.amount, ROW_NUMBER() OVER (ORDER BY VRDATE) AS RowNum FROM StockMain INNER JOIN StockDetail ON StockMain.stid = StockDetail.stid INNER JOIN party ON party.party_id = stockmain.party_id INNER JOIN item ON item.item_id = stockdetail.item_id WHERE stockmain.etype='purchase' ORDER BY VRDATE DESC ) AS MyDerivedTable WHERE MyDerivedTable.RowNum BETWEEN 1 and 5
您不需要在WHERE
子句后面的内部查询中使用ORDER BY
,因为您已经在ROW_NUMBER() OVER (ORDER BY VRDATE DESC)
使用了它。
SELECT * FROM ( SELECT Stockmain.VRNOA, item.description as item_description, party.name as party_name, stockmain.vrdate, stockdetail.qty, stockdetail.rate, stockdetail.amount, ROW_NUMBER() OVER (ORDER BY VRDATE DESC) AS RowNum --< ORDER BY FROM StockMain INNER JOIN StockDetail ON StockMain.stid = StockDetail.stid INNER JOIN party ON party.party_id = stockmain.party_id INNER JOIN item ON item.item_id = stockdetail.item_id WHERE stockmain.etype='purchase' ) AS MyDerivedTable WHERE MyDerivedTable.RowNum BETWEEN 1 and 5