12

我有两个查询与UNION ALL1相结合:

--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

当然,两者都可以单独工作,但与 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

查询失败并出现以下错误:


如果语句包含 UNION 运算符,则消息 104、级别 15、状态 1、行 3 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


服务器:如果语句包含 UNION 运算符,则消息 104、级别 15、状态 1、行 2 ORDER BY 项必须出现在选择列表中。

脚注

  • 1人为的假设示例。或不。

也可以看看

4

4 回答 4

6

有点hack,但这会起作用。

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 Color FROM 
        (SELECT TOP 1 Color, Wavelength
         FROM Rainbows
         WHERE Rainbows.StrangerID = Strangers.StrangerID
         ORDER BY Wavelength DESC
         ) AS Foo
    ) AS Color
FROM Strangers
go

DROP TABLE Rainbows
DROP TABLE Strangers
DROP TABLE Friends
于 2010-09-10T15:08:35.577 回答
1

我知道您可以使用 CTE(通用表表达式),您可以在其中使用您的 order by 来获取 CTE。

;with results as
(

    SELECT Cassettes.CassetteNumber,
    LastCassetteTransfers.Amount,
    CassetteTransfers.CreatedDate
    FROM Cassettes
    INNER JOIN LastCassetteTransfers
    ON Cassettes.CassetteGUID = LastCassetteTransfers.CassetteGUID

    UNION ALL

    SELECT Cassettes.CassetteNumber,
    (
       SELECT TOP 1 CassetteTransfers.Amount
       FROM CassetteTransfers
       WHERE CassetteTransfers.CassetteGUID = Cassettes.CassetteGUID
       AND CassetteTransfers.Mode = 'ctmLoad'
    ) AS Amount,
    CassetteTransfers.CreatedDate
    FROM Cassettes

)

SELECT CassetNumber, Amount
FROM results
ORDER BY CassetteTransfers.CreatedDate DESC, CassetteTransfers.Amount

那应该有帮助。重要的是确保您在内部查询(在本例中为 CTE)中返回的列排序。

让我知道它是如何工作的。

于 2010-09-10T14:53:56.543 回答
1

实际上,查看我评论过的链接中的解决方法,您可能想试试这个:

SELECT Flavor, Color
FROM Friends

UNION ALL

SELECT Flavor,
(SELECT TOP 1 Color FROM 
    (SELECT Color, Wavelength
    FROM Rainbows
    WHERE Rainbows.StrangerID = Strangers.StrangerID
) X ORDER BY Wavelength DESC) AS Color
FROM Strangers

...或一些类似的事情试图欺骗引擎不抱怨。

但恐怕我无法测试它;我不认为我们在建筑物中留下了一个 2000 的盒子,无论是虚拟的还是其他的。

编辑:啊!看起来乔和我在我们的鬼鬼祟祟上重叠了:)

于 2010-09-10T15:19:07.440 回答
0

我建议以您想要的列的格式创建一个变量表。

  1. 为您要加入的每个表运行从原始表插入查询到变量表中,包括您要应用的所有过滤器和排序。
  2. 返回变量表

例子:

set nocount on
DECLARE @temp_table TABLE(Flavor varchar(20), Color varchar(20))
    
    insert into @temp_table (Flavor,Color)
    /*Apply select query #1 with all filters, joins and sorting */
    SELECT Flavor,Color   FROM Strangers  ORDER BY Wavelength DESC
    
    insert into @temp_table (Flavor,Color)
    /*Apply select query #2 with all filters, joins and sorting */
    SELECT Flavor, Color FROM Friends
    
    /*Return the results pushed into @variable table */
    select * from @temp_table
于 2020-10-18T12:00:05.623 回答