We help IT Professionals succeed at work.

How can I import unique records based on email

51 Views
Last Modified: 2019-02-13
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

Comment
Watch Question

David ToddSenior Database Administrator
CERTIFIED EXPERT

Commented:
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
Senior DBA
CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Author

Commented:
Thanks

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions