Subject | DISTINCT on only one field? |
---|---|
Author | Joe Martinez |
Post date | 2003-04-28T16:26:55Z |
Here's my situation:
I have a table with addresses:
Name
Address
City
State
Zip
I want to do a select that only picks up distinct values of the Address and
Zip fields, but I want the select to include the rest of the fields. If
the same value pair of Address and Zip appears more than once, I only want
one of the records, even if the rest of the fields differ. I don't care
which one of the records I get. Example:
Joe Smith
123 Main St.
Anytown
CA
12345
Joseph Smith
123 Main St.
Mytown
CA
12345
Cheryl Smith
123 Main St.
Anytown
CA
12345
In this case, I want only one of those records (I don't care which
one). But, I want the query to give me all the fields.
I was trying to think of a way to do it with DISTINCT. The problem is that
if I select all the fields, they ALL are included in the DISTINCT, and I'll
get all 3 records. If I only put Address and Zip in the select, I ony get
one record, but I don't have the rest of the fields.
Is there some other way that I can achieve this?
Thanks,
Joe
I have a table with addresses:
Name
Address
City
State
Zip
I want to do a select that only picks up distinct values of the Address and
Zip fields, but I want the select to include the rest of the fields. If
the same value pair of Address and Zip appears more than once, I only want
one of the records, even if the rest of the fields differ. I don't care
which one of the records I get. Example:
Joe Smith
123 Main St.
Anytown
CA
12345
Joseph Smith
123 Main St.
Mytown
CA
12345
Cheryl Smith
123 Main St.
Anytown
CA
12345
In this case, I want only one of those records (I don't care which
one). But, I want the query to give me all the fields.
I was trying to think of a way to do it with DISTINCT. The problem is that
if I select all the fields, they ALL are included in the DISTINCT, and I'll
get all 3 records. If I only put Address and Zip in the select, I ony get
one record, but I don't have the rest of the fields.
Is there some other way that I can achieve this?
Thanks,
Joe