SQL Server: ORDER BY in subquery with UNION

后端 未结 4 1015
面向向阳花
面向向阳花 2020-12-11 15:43

i have two queries being combined with a UNION ALL1:

--Query 1
SELECT Flavor, Color
FROM Friends

 

<         


        
4条回答
  •  一向
    一向 (楼主)
    2020-12-11 16:27

    Actually, looking at the workaround from that link I commented, you might want to try this:

    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
    

    ...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 :)

提交回复
热议问题