Two Solutions to fix issue: ORDER BY items must appear in the select list if the statement contains a UNION, ...
2011-07-28 15:04
537 查看
ORDER BY items must appear in the select list if the statement contains a UNION, INTERSECT or EXCEPT operator.
Solution 1:
To fix this problem I put my Union statement into a derived table by doing the following.
Solution 1:
To fix this problem I put my Union statement into a derived table by doing the following.
SELECT * FROM (
SELECT Column1, Column2 FROM Table_A
UNION ALL
SELECT Column1, Column2 FROM Table_B
) DerivedTableName (Column1, Column2)
Next I added in my dynamic Order By code as such.SELECT Column1, Column2 FROM Table_A
UNION ALL
SELECT Column1, Column2 FROM Table_B
) DerivedTableName (Column1, Column2)
SELECT * FROM (
SELECT Column1, Column2 FROM Table_A
UNION ALL
SELECT Column1, Column2 FROM Table_B
) DerivedTableName (Column1, Column2)
ORDER BY
CASE @OrderBy WHEN ‘Column1ASC’ THEN Column1 END ASC,
CASE @OrderBy WHEN ‘Column1DESC’ THEN Column1 END DESC
Solution2:
SELECT Z.ax, Z.by, Z.cz
FROM (SELECT a,b,c FROM X
UNION
SELECT x,y,z FROM Y)
AS Z Order by a
SELECT Column1, Column2 FROM Table_A
UNION ALL
SELECT Column1, Column2 FROM Table_B
) DerivedTableName (Column1, Column2)
ORDER BY
CASE @OrderBy WHEN ‘Column1ASC’ THEN Column1 END ASC,
CASE @OrderBy WHEN ‘Column1DESC’ THEN Column1 END DESC
Solution2:
SELECT Z.ax, Z.by, Z.cz
FROM (SELECT a,b,c FROM X
UNION
SELECT x,y,z FROM Y)
AS Z Order by a
相关文章推荐
- This is probably a good time to review the order in which SELECT statement clauses are to be specified. Table 10.2 lists all the clauses we have learned thus far, in the order they must be used.
- 解决 ORDER BY expressions must appear in select list
- Linq To xRM Issue(CRM 4.0 Online): The 'select' and 'orderBy' calls may only reference a single common entity type.
- MySQL报错“Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre”
- python: how to delete a given item if it exist in the list
- using JS to control two select(html),the data can be loaded from database and XML,and show in the select
- MySQL报错“Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre”
- #1055 - Expression of SELECT list is not in GROUP BY clause and contains nonaggregated column this i
- Eclipse启动时报错:A Java RunTime Environment (JRE) or Java Development Kit (JDK) must be available in order to run Eclipse. No java virtual machine was found after searching the following locations:…
- Error Code: 1055. Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated
- What is the fastest, case insensitive, way to see if a string contains another string in C#?
- mysql遇见Expression #1 of SELECT list is not in GROUP BY clause and contains nonag
- ORACLE EBS出现In order to access this application, you must install the J2SE Plugin version 1.6.0_07.
- 'union all' two select sentences contain 'order by'
- MySQL: Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column '
- mysql遇见Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre的问题
- Internet Information Services is not installed. You must have Internet Information Services installed in order to use the SharePoint Products and Technologies Configuration Wizard
- ORACLE EBS出现In order to access this application, you must install the J2SE Plugin version 1.6.0_07.
- mysql5.7 报错1055:Expression #1 of SELECT list is not in GROUP BY clause and contains non
- How to fix the issue that GEM_HOME and/or GEM_PATH not set issue for rvm in mac version 10.12