using DISTINCT?

  • hi everyone, I've a problem with using DISTINCT, I'd like to get distinct values from field one but ordering on say field 2, but unless I put field 2 in the select statement it won't accept it, but doing that I return all the records as rhe 2 combined are distinct, can anyone help?

  • As Joe Celko would put it :

    SELECT Squid FROM FOO ORDER BY Britney Spears

    I don't see how the heck the server could handle this one.

    Can you tell us more about the problem you want to solve. Right now I have no clue about what you want, except maybe that you want to drop duplicate rows from a table. If it's the case, just search these forums, you'll get plenty of exemples.

  • as an example as to what I'm after

    First Last id

    ======================

    John Brown 1

    Joe Smith 2

    Jane Brown 3

    Brian Smith 4

    Carol Jones 5

    What I'd like is to return

    Brown

    Jones

    Smith

    in order of the ID

  • Select LastName, MIN(id) As mID FROM dbo.FOO GROUP BY LastName ORDER BY mID

    I still don't get why you want to do this thought... have no clue what you want to do now however.

  • try group by

    SELECT DISTINCT field1

    FROM Table1

    WHERE (conditions)

    GROUP BY field2 - hope this helps

  • Group by DOESN'T guarantee the order of the results. While it often does, this is not the way to do it. The only construct that guarantees the order of the results is ORDER BY.

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply