How can I import unique records based on email

How can I make this insert statement ignore a repeated email?  The [Individuals] table may have multiple entries with the same email address - I only want a single record to be inserted for each email.

INSERT INTO [dbo].[PeopleDetails](Email, FirstName,LastName,Address1,Address2,City,Zip,PhoneNumber,StateID,CountryID,CommPrefTypeID, GalacticID, AccountApproved)
SELECT i.[Email], i.[First_Name], i.[Last_Name], i.[Address_1], i.[Address_2], i.[City], i.[Zip_Code], i.[Phone], s.StateID, c.CountryID, @CommPrefTypeID, @GalacticID, @AccountApproved
FROM [dbo].[Individuals] i
JOIN [dbo].[State] s ON i.[state] = s.[StateCode]
JOIN [dbo].[Country] c ON i.[Country] = c.[CountryName]

Open in new window

CAMPzxzxDeathzxzxAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David ToddSenior Database AdministratorCommented:
Hi,

How will you break the tie between emails? That is, is there another record you'd use or an ID on the individual?

Assuming that there is an IndividualID in the Individuals table, something like this where clause might do the trick.

where
      i.IndividualID in (
            select
                  max( ii.IndividualID ) as latestIndividualID
            from dbo.Individuals ii
            group by
                  ii.Email
            )
;

Alternatively, a CreatedDateTime or a LastmodifiedDateTime on the Individuals table could be used to make sure the last record for this email was selected.

HTH
  David
Scott PletcherSenior DBACommented:
INSERT INTO [dbo].[PeopleDetails](Email, FirstName,LastName,Address1,Address2,City,Zip,PhoneNumber,StateID,CountryID,CommPrefTypeID, GalacticID, AccountApproved)
SELECT i.[Email], i.[First_Name], i.[Last_Name], i.[Address_1], i.[Address_2], i.[City], i.[Zip_Code], i.[Phone], s.StateID, c.CountryID, @CommPrefTypeID, @GalacticID, @AccountApproved
FROM (
      SELECT *, ROW_NUMBER() OVER(PARTITION BY Email ORDER BY GalacticID) AS row_num
      FROM [dbo].[Individuals]

) AS i
JOIN [dbo].[State] s ON i.[state] = s.[StateCode]
JOIN [dbo].[Country] c ON i.[Country] = c.[CountryName]
WHERE i.row_num = 1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CAMPzxzxDeathzxzxAuthor Commented:
Thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.