Results 1 to 5 of 5

Thread: SELECT DISTINCT with primary key?

  1. #1

    Thread Starter
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690

    SELECT DISTINCT with primary key?

    MS SQL Server 2000:

    I'm trying to get a list of distinct columns, but I also need the primary key. I thought something like this might work:
    Code:
    SELECT a.CmnPersonPK, b.LastName, b.FirstName, b.MiddleName, b.DOB FROM vCmnPerson a
    INNER JOIN (SELECT CmnPersonPK, LastName, FirstName, MiddleName, DOB FROM vCmnPerson GROUP BY LastName, FirstName, MiddleName, DOB) AS b
    ON a.CmnPersonPK = b.CmnPersonPK
    but I get an error "Column 'vCmnPerson.CmnPersonPK' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause."

    Been trying and searching, can't figure it out. How do I get a list where those four columns are distinct, but also retrieve the PK?

    Thanks,
    Mike

  2. #2
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: SELECT DISTINCT with primary key?

    You'd have to add it to the group by, but this would return each one as they are unique.

    You could use a sub query to get the grouping then link back to the main table to retrieve the primary key... Depending on the sub query and function you'd need (probably) is First... or Last...

    Or ask Szlamany as he is great with Sql server.

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  3. #3

    Thread Starter
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690

    Re: SELECT DISTINCT with primary key?

    Quote Originally Posted by Ecniv
    You could use a sub query to get the grouping then link back to the main table to retrieve the primary key... Depending on the sub query and function you'd need (probably) is First... or Last...
    Ok, but I'm still lost. Can you give me an example?

    Thanks,
    Mike

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: SELECT DISTINCT with primary key?

    How can you get a DISTINCT list and also get the primary keys?

    Are you looking for DUPLICATES - situations where the LastName, FirstName, MiddleName and DOB are on file more then once?

    Please explain your goal a bit further.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5

    Thread Starter
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690

    Re: SELECT DISTINCT with primary key?

    Yeah, you're right. I think what I was trying to do didn't make sense. I guess what I was wanting was a list of unique column values, and an PK, just so I could attach to it.

    I'm going to go about this a different way. Thanks.

    Mike

Posting Permissions

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



Click Here to Expand Forum to Full Width