SQL Server: ORDER BY in subquery with UNION

后端 未结 4 1007
面向向阳花
面向向阳花 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:12

    I know that you could use a CTE (Common Table Expression) , where you can use your order by for the 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
    

    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.

    0 讨论(0)
  • 2020-12-11 16:22

    A bit of a hack, but this will work.

    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
    
    0 讨论(0)
  • 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 :)

    0 讨论(0)
  • 2020-12-11 16:28

    I'm suggesting to create a variable table in the format of the columns you want.

    1. run insert query from origin table into variable table for each table you which to join including all filters and sorting you want to apply.
    2. Return the Variable table

    Example:

    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
    
    0 讨论(0)
提交回复
热议问题