Distinct or group by on some columns but not others

Posted by Nazadus on Stack Overflow See other posts from Stack Overflow or by Nazadus
Published on 2010-04-01T14:13:10Z Indexed on 2010/04/01 14:33 UTC
Read the original article Hit count: 212

Filed under:
|

I have a view that I'm trying to filter with something similar to DISTINCT on some columns but not others.

I have a view like this:

Name
LastName
Zip
Street1
HouseholdID (may not be unique because it may have multiple addresses -- think of it in the logical sense as grouping persons but not physical locations; If you lookup HouseholdID 4130, you may get two rows.. or more, because the person may have mutiple mailing locations)
City
State

I need to pull all those columns but filter on LastName,Zip, and Street1. Here's the fun part: The filter is arbitrary -- meaning I don't care which one of the duplicates goes away. This is for a mail out type thing and the other information is not used for any other reason than than to look up a specific person if needed (I have no idea why). So.. given one of the records, you can easily figure out the removed ones.

As it stands now, my Sql-Fu fails me and I'm filtering in C# which is incredibly slow and is pretty much a foreach that starts with an empty list and adds the row in if the combined last name, zip, and street aren't are not in the list.

I feel like I'm missing a simple / basic part of SQL that I should be understanding.

© Stack Overflow or respective owner

Related posts about tsql

Related posts about sql-server-2005