#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2006
    Posts
    144
    Rep Power
    8

    UNION for ordered tables


    Hi,

    I'm trying to run this code and it doesn't work. I'm sure it's quite self-explanatory of what it's trying to do
    Code:
    select * from equity.dbo.accounts
    order by currency
    union all
    select * from equity.dbo.accounts
    order by entity
    Is there a way to do this?
    Cheers,
    Fred Liu
  2. #2
  3. SQL Consultant
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2003
    Location
    Toronto Canada
    Posts
    27,167
    Rep Power
    4274
    Originally Posted by fred2028
    I'm sure it's quite self-explanatory of what it's trying to do
    yup, it is

    what is ~not~ self-explanatory is why you want to do that

    once you bring an entire table of data into your front-end application, why can't you just re-sort it there?

    alternatively, why can't you just run two queries here?
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Location
    Kansas City
    Posts
    13
    Rep Power
    0
    You can only have the order by on the 2nd query in a union.

    Code:
    select * from equity.dbo.accounts union all select * from equity.dbo.accounts order by entity
  6. #4
  7. SQL Consultant
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2003
    Location
    Toronto Canada
    Posts
    27,167
    Rep Power
    4274
    Originally Posted by DeveloperBase
    You can only have the order by on the 2nd query in a union.
    technically speaking, it isn't on the 2nd query in the union -- it's on the entire union
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

IMN logo majestic logo threadwatch logo seochat tools logo