Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2003
    Posts
    223

    Unanswered: How to keep original order when querying a table?

    Hello, everyone:

    I have a table like:

    ColName
    b
    b
    b
    d
    d
    d
    a
    a
    c
    c
    c

    I use DISTINCT to filter duplicated row. I want to get the return by original order like:
    b
    d
    a
    c

    However, SQL Server re-order it if using DISTINCT and return like:
    a
    b
    c
    d

    Can any one have the idea to handle that? Thanks

    ZYT

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    if your table has only that one column, you cannot do it

    besides, there is no "original order" in a table

    a table has no order
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Mar 2003
    Posts
    223
    Hi, Rudy:

    There are the identity column and other columns in the table.

    ZYT

  4. #4
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Code:
    select ColName
         , min(identitycolumn) as yourid
      from yourtable
    group
        by ColName
    order
        by yourid
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  5. #5
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    I think you meant:

    order by min(identitycolumn)
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

Posting Permissions

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