The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP, OFFSET or FOR XML is also specified.
I am getting the above said error while trying to execute the following query. Can anyone please have a look and tell me what am I doing wrong here?
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
This error will be encountered if you add a Sort Type to the SQL Query Builder without changing how the Query works. You will need to instead change the Query to select the Top 100% Grouped by All, and change the Group By on all Fields - this will allow you to export.
The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP, OFFSET or FOR XML is also specified.
The ORDER BY clause is not valid in views, inline functions, derived tables, and subqueries, unless either the TOP or OFFSET and FETCH clauses are also specified. When ORDER BY is used in these objects, the clause is used only to determine the rows returned by the TOP clause or OFFSET and FETCH clauses.
You do not need to use ORDER BY
in inner query after WHERE
clause because you have already used it in 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With