SQL查询ORDER BY子句报错:执行INSERT查询时遇问题求助
Hey there! Let's break down the ORDER BY error you're facing with your INSERT/SELECT query. From what you've shared, this type of error almost always ties back to how SQL handles grouped queries and ordered results. Here are the most common fixes:
1. Mismatched Columns Between GROUP BY and ORDER BY
SQL Server (especially when running in strict ANSI mode) enforces a rule: any column you use in ORDER BY must either be included in your GROUP BY clause or wrapped in an aggregate function (like SUM(), MAX(), etc.).
For example, if you tried to add ORDER BY CUS.CUSTOMER_NAME to your subquery but didn't include CUS.CUSTOMER_NAME in GROUP BY or aggregate it, you'll get an error. The fix? Either add the column to your GROUP BY list, or use an aggregate function on it (e.g., MAX(CUS.CUSTOMER_NAME)).
2. ORDER BY in Subqueries Without Row Limiting
If you added an ORDER BY directly inside your subquery (z.*), SQL Server will throw an error unless you pair it with a row-limiting clause like TOP, OFFSET, or FETCH NEXT. That's because subqueries are meant to return datasets, not ordered results—outer queries can reorder data anyway.
Instead of sorting inside the subquery, move your ORDER BY to the outer query. If you must sort the subquery (e.g., to grab a specific subset of rows), use TOP 100 PERCENT or OFFSET 0 ROWS FETCH NEXT [large number] ROWS ONLY to make it valid.
3. Incomplete GROUP BY Clause
Your shared query cuts off at GROUP BY...—make sure this clause includes all non-aggregated columns from your SELECT statement. In your case, that means IND.L_SBP_CODE and TDEPO.Type_of_Deposit need to be in the GROUP BY list (since they're not wrapped in aggregate functions). A missing column here will cause errors that can spill over to your ORDER BY logic.
Example of a Fixed Query
Here's how you might adjust your code to resolve these issues:
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 -- Full GROUP BY matching non-aggregated columns ) z ORDER BY z.L_SBP_CODE, z.Type_of_Deposit; -- ORDER BY applied to the outer query
If You Need to Sort the Subquery
If you have a specific reason to sort inside the subquery (like filtering top rows), use this approach:
INSERT Into dbo.[DRC_76_A-05 Deposits SBP Coding] SELECT '20180228' as BusinessDate, z.* FROM ( SELECT TOP 100 PERCENT -- Use OFFSET/FETCH for more modern syntax 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 IND.L_SBP_CODE DESC -- Valid because paired with TOP ) z;
内容的提问来源于stack exchange,提问作者MAunAli

