Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SELECT DISTINCT syntax question 5

Status
Not open for further replies.

tahoskier

MIS
Sep 27, 2001
24
US
I have a address table with multiple family members in it. I am using the SELECT DISTINCT command to remove the extra family members for a mailer. The DISTINCT FIELDS are address, city, state, zip and lname. How can I export into a new table a first name also? I am not picky about which name.

Here is the exact statement that I am using.

SELECT DISTINCT address, city, state, zip, lname INTO TableNoDups from Table1
 
Does your query succeed?all you need is how to insert the result into a new table?

(1)create a table(say table2) with
address, city, state, zip, lname fileds.

(2)execute the following query

insert into table2 select DISTINCT address, city, state, zip, lname from table1
 
But How do insert a fname field which is a non DISTINCT field?

Thanks for your quick reply.
 
I am confused now,you need all the other column's value to be distinct but not for fname.Then whatever you did for distinct will be destroyed by fname.(basically if you mix non distinct and distinct = non distinct!)

Then how the new table will look like.Can you give the result of table how you want it to be?

 
Hi,

Try this query.. Only one first name is returned for a lastname....

SELECT DISTINCT address, city, state, zip, lname ,
(SELECT Top 1 FName from Table1 T Where T.Lname = T1.Lname) FNAme
INTO TableNoDups from Table1 T1

But if 2 families have same lastname this query will fail
probably u can use address,city,state,zip also reduce chances of duplication in result. Do u have some kind of ID field in the DB for each family? If so, u can use that instead of Lname.

Hope it helps

Sunil
 
Thanks for everyones advice. I think I will modify the tables.
 
If you just want to take any one firstname for each family then this will do it:

Code:
SELECT address, city, state, zip, lname, MIN(fname) AS fname
  INTO tablenodups
FROM table1
GROUP BY address, city, state, zip, lname

--James
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top