Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions

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    
like image 211
Kamran Ahmed Avatar asked Aug 03 '13 09:08

Kamran Ahmed


People also ask

How do you solve ORDER BY clause invalid in views inline functions?

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.

Is it invalid to have an ORDER BY clause in a subquery?

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.

Can we use ORDER BY clause in inline view?

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.


1 Answers

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  
like image 73
Himanshu Jansari Avatar answered Oct 04 '22 09:10

Himanshu Jansari