SQL Server:子查询中的 ORDER BY 与 UNION
我有两个查询与 UNION ALL
1 组合在一起:
--Query 1
SELECT Flavor, Color
FROM Friends
--Query 2
SELECT Flavor,
(SELECT TOP 1 Color
FROM Rainbows
WHERE Rainbows.StrangerID = Strangers.StrangerID
ORDER BY Wavelength DESC
) AS Color
FROM Strangers
当然,这两者单独工作都可以正常工作,但是当与 UNION ALL 结合使用时:
SELECT Flavor, Color
FROM Friends
UNION ALL
SELECT Flavor,
(SELECT TOP 1 Color
FROM Rainbows
WHERE Rainbows.StrangerID = Strangers.StrangerID
ORDER BY Wavelength DESC
) AS Color
FROM Strangers
查询失败并出现错误:
消息 104,级别 15,状态 1,第 3 行
如果语句包含 UNION 运算符,ORDER BY 项必须出现在选择列表中。
如何在带有 UNION ALL 的语句中使用 ORDER BY?
可复制粘贴示例
CREATE TABLE Friends (Flavor int, Color int)
CREATE TABLE Strangers (Flavor int, StrangerID int)
CREATE TABLE Rainbows (StrangerID int, Color int, Wavelength int)
go
SELECT Flavor, Color
FROM Friends
UNION ALL
SELECT Flavor,
(SELECT TOP 1 Color
FROM Rainbows
WHERE Rainbows.StrangerID = Strangers.StrangerID
ORDER BY Wavelength DESC
) AS Color
FROM Strangers
go
DROP TABLE Rainbows
DROP TABLE Strangers
DROP TABLE Friends
服务器:消息 104,级别 15,状态 1,第 2 行
如果语句包含 UNION 运算符,ORDER BY 项必须出现在选择列表中。
脚注
- 1人为的假设示例。或不。
另请参阅
i have two queries being combined with a UNION ALL
1:
--Query 1
SELECT Flavor, Color
FROM Friends
--Query 2
SELECT Flavor,
(SELECT TOP 1 Color
FROM Rainbows
WHERE Rainbows.StrangerID = Strangers.StrangerID
ORDER BY Wavelength DESC
) AS Color
FROM Strangers
Both of which, of course, work fine separately, but when combined with a UNION ALL
:
SELECT Flavor, Color
FROM Friends
UNION ALL
SELECT Flavor,
(SELECT TOP 1 Color
FROM Rainbows
WHERE Rainbows.StrangerID = Strangers.StrangerID
ORDER BY Wavelength DESC
) AS Color
FROM Strangers
The query fails with the error:
Msg 104, Level 15, State 1, Line 3
ORDER BY items must appear in the select list if the statement contains a UNION operator.
How do i use an ORDER BY in a statement with a UNION ALL?
Copy-Pasteable Example
CREATE TABLE Friends (Flavor int, Color int)
CREATE TABLE Strangers (Flavor int, StrangerID int)
CREATE TABLE Rainbows (StrangerID int, Color int, Wavelength int)
go
SELECT Flavor, Color
FROM Friends
UNION ALL
SELECT Flavor,
(SELECT TOP 1 Color
FROM Rainbows
WHERE Rainbows.StrangerID = Strangers.StrangerID
ORDER BY Wavelength DESC
) AS Color
FROM Strangers
go
DROP TABLE Rainbows
DROP TABLE Strangers
DROP TABLE Friends
Server: Msg 104, Level 15, State 1, Line 2
ORDER BY items must appear in the select list if the statement contains a UNION operator.
Footnotes
- 1Contrived hypothetical example. Or not.
See also
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有点黑客,但这会起作用。
A bit of a hack, but this will work.
我知道您可以使用 CTE(通用表表达式),您可以在其中使用 CTE 的 order by 。
那应该有帮助。重要的是确保您按内部查询(在本例中为 CTE)中返回的列进行排序。
让我知道它是如何工作的。
I know that you could use a CTE (Common Table Expression) , where you can use your order by for the CTE.
That should help. The important thig is to make sure that you have your order by columns returned in the inner query (in this case the CTE).
Let me know how it works.
实际上,看看我评论的链接中的解决方法,您可能想尝试这个:
...或一些类似类型的东西来试图欺骗引擎不抱怨。
但恐怕我无法测试它;我认为我们大楼里已经没有 2000 个盒子了,无论是虚拟的还是其他的。
编辑:啊!看起来乔和我在偷偷摸摸上有重叠:)
Actually, looking at the workaround from that link I commented, you might want to try this:
...or some similar type of thing to try to fool the engine into not complaining.
But I can't test it, I'm afraid; I don't think we've got a 2000 box left in the building, virtual or otherwise.
EDIT: Ah! Looks like Joe and I overlapped on our sneakiness :)
我建议以您想要的列的格式创建一个变量表。
您要加入的表格,包括您想要的所有过滤器和排序
申请。
例:
I'm suggesting to create a variable table in the format of the columns you want.
table you which to join including all filters and sorting you want
to apply.
Example: