-3

I'm getting an error when I'm executing the below query:

INSERT Into dbo.[DRC_76_A-05 Deposits SBP Coding] 
SELECT '20180228' as BusinessDate,z.* 
FROM ( SELECT IND.L_SBP_CODE, TDEPO.Type_of_Deposit, 
              ISNULL(SUM(AC.WORKING_BALANCE),0) AS AMOUNT 
       FROM InsightSource.BS.ACCOUNT AC 
       LEFT JOIN InsightSource.BS.CUSTOMER CUS ON AC.CUSTOMER = CUS.[@ID] 
       INNER JOIN KMBL.DBO.TYPE_OF_DEPOSITS TDEPO ON AC.CATEGORY = TDEPO.Cat_ID 
       LEFT JOIN InsightSource.BS.INDUSTRY_LocalRef IND ON CUS.INDUSTRY = IND.[@Id] 
       GROUP BY IND.L_SBP_CODE, TDEPO.Type_of_Deposit 
       ORDER BY TDEPO.Type_of_Deposit ASC ) Z

Error:

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

5
  • Add top N to the view definition Apr 30, 2018 at 7:19
  • show the rest of you code ...
    – ScaisEdge
    Apr 30, 2018 at 7:19
  • instead of screenshot, post the sql here. Apr 30, 2018 at 7:26
  • @scaisEdge INSERT Into dbo.[DRC_76_A-05 Deposits SBP Coding] Select '20180228' as BusinessDate,z.* From ( SELECT IND.L_SBP_CODE, TDEPO.Type_of_Deposit, ISNULL(SUM(AC.WORKING_BALANCE),0) AS AMOUNT FROM InsightSource.BS.ACCOUNT AC LEFT JOIN InsightSource.BS.CUSTOMER CUS ON AC.CUSTOMER = CUS.[@ID] INNER JOIN KMBL.DBO.TYPE_OF_DEPOSITS TDEPO ON AC.CATEGORY = TDEPO.Cat_ID LEFT JOIN InsightSource.BS.INDUSTRY_LocalRef IND ON CUS.INDUSTRY = IND.[@Id] GROUP BY IND.L_SBP_CODE, TDEPO.Type_of_Deposit ORDER BY TDEPO.Type_of_Deposit ASC )Z
    – MAunAli
    Apr 30, 2018 at 7:27
  • uodate your question and add your code .. in question not in comment
    – ScaisEdge
    Apr 30, 2018 at 7:30

2 Answers 2

1

You should move the ORDER BY outside the subquery

  INSERT Into dbo.[DRC_76_A-05 Deposits SBP Coding] 
  Select '20180228' as BusinessDate,z.* 
  From ( 
      SELECT IND.L_SBP_CODE
            , TDEPO.Type_of_Deposit
            , ISNULL(SUM(AC.WORKING_BALANCE),0) AS AMOUNT 
      FROM InsightSource.BS.ACCOUNT AC 
      LEFT JOIN InsightSource.BS.CUSTOMER CUS ON AC.CUSTOMER = CUS.[@ID] 
      INNER JOIN KMBL.DBO.TYPE_OF_DEPOSITS TDEPO ON AC.CATEGORY = TDEPO.Cat_ID 
      LEFT JOIN InsightSource.BS.INDUSTRY_LocalRef IND ON CUS.INDUSTRY = IND.[@Id] 
      GROUP BY IND.L_SBP_CODE, TDEPO.Type_of_Deposit 
 ) Z
  ORDER BY Z.Type_of_Deposit ASC
0

The error message is self explanatory :

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

You are doing an ORDER BY in a subquery. It is useless in all cases, so you can simply get rid of it.

Do your ORDER BY in your main query, when all your data is gathered and filtered

3
  • if i comment that line in subquery it compile successfully but in my ssrs report results are coming not in a order by form.
    – MAunAli
    Apr 30, 2018 at 7:29
  • read my answer again: you have to move your ORDER BY in your main query.
    – Thomas G
    Apr 30, 2018 at 7:30
  • @MAunAli You could insert an order by in the main query
    – xCloudx8
    Apr 30, 2018 at 7:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.