Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2012
    Posts
    188

    Unanswered: Order By In A Union

    This query works perfectly and orders by just as I need
    Code:
    Select
    '1st' As [Type],
    #Uno.ID
    #Uno.Address,
    #Uno.shippingInfo
    FROM #Uno
    LEFT JOIN #Two
    ON #Uno.ID = #Two.ID
    INNER JOIN #Cal
    ON #Uno.[month] = #Cal.[Month]
    AND #Uno.[Year] = #Cal.[Year]
    Order By #Cal.[ID] Asc
    However, when I use it in a Union All so I can pull data from 2 diff tables, the order by statement no longer works. How can I order by data in 2 tables?
    Code:
    Select
    '1st' As [Type],
    #Uno.ID
    #Uno.Address,
    #Uno.shippingInfo
    FROM #Uno
    LEFT JOIN #Two
    ON #Uno.ID = #Two.ID
    INNER JOIN #Cal
    ON #Uno.[month] = #Cal.[Month]
    AND #Uno.[Year] = #Cal.[Year]
    UNION ALL
    Select
    '2nd' As [Type],
    #Three.ID,
    #Three.Address,
    #Three.shippingInfo
    FROM #Three
    INNER JOIN #Cal
    ON #Three.[Month] = #Cal.[Month]
    AND #Three.[Year] = #Cal.[Year]
    Last edited by jo15765; 02-10-15 at 16:28.

  2. #2
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    You put the ORDER BY at the end of the query i.e.
    Code:
    SELECT ...
    FROM   ...
    
    UNION ALL
    
    SELECT ...
    FROM   ...
    
    ORDER
        BY ...
    George
    Home | Blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •