This will pull complete rows.
select *
from consults a
where name = (select min(name) from consults where email = a.email)
and not exists (select 1 from users where email = a.email)
Main Topics
Browse All TopicsSorry about the Question title, didn't know exactly how to call it...
I have a table called consults:
Name, Last Name, email
And a table called Users:
Name, Last Name, email
I need to copy all the data from the table consults that is not in the table Users (using email). There are repeated emails in the consults table (but not necessary have the same name and last name, although some of them are the same. For example: John - Black - johnblack@mail.com / John - BlackS - johnblack@mail.com. These 2 records are the same, but there was a mistake when the lastName was entered for second time, note the "S").
With the next query I have what I want in part, but I also need the name and last name, and I can't use distinct with all the rows because distinct for some reason returns less records that the ones I know it has to return. I write it clearer.
SELECT DISTINCT Email FROM Consults
WHERE (Email NOT IN (SELECT email FROM users)) <---- with this query I get the records I want (but I also need Name and lastName!). I also know with this how many records I'll have to copy to the other table (it returns 450 records).
If I use SELECT DISTINCT Name, lastName, Email FROM Consults
WHERE (Email NOT IN (SELECT email FROM users))
It returns less records than 450, I think Distinct "groups" using lastname and name too and some rows disappear.
Just to learn more, I would be happy with the query that returns what I want (I was going to paste results in Excel, and then export it to the Users table through SQL Server Manager) and also with a direct Insert, that automatically inserts (adds) the results in the Users table.
I hope you can help me, thanks a lot! :)
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
I need to copy all the data from the table consults that is not in the table Users (using email). There are repeated emails in the consults table (but not necessary have the same name and last name, although some of them are the same.
Select MAX(c.Name), MAX(c.LastName), c.email
From Consults c
LEFT Join Users u On c.email = u.email
Where u.email Is Null
Group By c.email
Hi,
This is one of those problems that need to be broken down into pieces:
1: Delete all rows from consults which already exist in users (matched by email only)
2: Count the number of rows in consults for each email address.
3: Insert the single entries from consults into users, based on the count from section 2
4: Process the multiple entries, keeping the top one from each (use order by to determine which one).
Note: You might need to change the declare for the @email variable to match your size.
-- Section 1
-- Drop rows already exist
delete consults
where email in (select email from users)
-- Section 2
-- Count number by email
select email
, count(*) as NumRows
into #temp
from consults
group by
email
-- Section 3
-- load single entries
insert into users
select * from consults
where email in (select email from #temp where NumRows = 1)
-- drop rows proceseed
delete #temp
where NumRows = 1
-- Section 4
declare @Check int
declare @email varchar(255)
select @check = count(*) from #temp
print @check
while @check > 0
begin
-- Process first duplicate
select top 1 @email = email from #temp
insert into users select top 1 * from consults where email = @email
delete #temp where email = @email
-- Recheck outstanding entries
select @check = count(*) from #temp
end
Business Accounts
Answer for Membership
by: slkingPosted on 2005-10-04 at 20:28:25ID: 15019272
Hi,
try this
select top 1 * from consults
where email NOT IN (SELECT email FROM users)
You cannot however decide which of the Name,Last Name combination gets chosen by the query for a given email.
Hope this serves your purpose