Weeding out duplicate values (using DISTINCT)

Posted on 2005-05-12
Last Modified: 2010-03-19
Environment: MS SQL Server 2000

Say I have a table like the following:


'SELECT DISTINCT userid, colour, flavour' blocks the display of row 4 (since every element in that row was already returned with row 3), but not row 2 (since the flavour value differs).  How do I return only one row per userid value?

I haven't found any working examples of DISTINCT ORDER BY (which sounds like it would be useful).

Thanks in advance,

Question by:caylt
    LVL 28

    Accepted Solution

    SELECT *
    FROM YourTable A
    WHERE A.Identity = (SELECT MAX(Identity) FROM YourTable B
                                  WHERE A.UserID = B.UserID)
    LVL 34

    Expert Comment

    by:Brian Crowe
    It sounds like is that you want a single entry per userid/color/flavor combo

    SELECT Min(identity), userid, colour, flavour
    FROM myTable
    GROUP BY userid, colour, flavour
    LVL 34

    Expert Comment

    by:Brian Crowe
    oops like i misread...

    SELECT *
    FROM myTable
    WHERE myTable.identity IN (SELECT TOP 1 identity FROM myTable B
         WHERE B.userid = myTable.userid
         ORDER BY {insert your order here to determine which record is listed first})
    LVL 4

    Expert Comment

    for performance reasons i would use the following
    SELECT identity, userid, colour, flavour
    (SELECT Min(identity) as identity, userid, colour, flavour
    FROM myTable
    GROUP BY userid, colour, flavour) as innerTable
    order By 1
    LVL 4

    Expert Comment

    The GROUP BY clause can be sped up if you follow these suggestion:

    Keep the number of rows returned by the query as small as possible.


    Keep the number of groupings as few as possible.

    reason for my previous post
     snippet from

    Don't group redundant columns.

    If there is a JOIN in the same SELECT statement that has a GROUP BY, try to rewrite the query to use a subquery instead of using a JOIN. If this is possible, performance will be faster. If you have to use a JOIN, try to make the GROUP BY column from the same table as the column or columns on which the set function is used.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
    JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
    Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
    Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now