Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

REMOVING DUPLICATE VALUES FROM MY DATA FOR AN INSERT

Posted on 2008-06-26
3
Medium Priority
?
204 Views
Last Modified: 2010-03-20
TicketingCode  Name      CarrierCode      ClassCode      CabinDescription
121      ADA Air      ZY      A      First
121      ADA Air      ZY      B      Economy
121      ADA Air      ZY      C      Business


I have the above data in a temporary table.

The first thing I need to do is insert the distinct ticketing code, name and carrier code into a table called airline.

Using:
INSERT INTO AIRLINE(AIRLINENAME,TICKETINGCODE,REFCODE)
select distinct [Name], TicketingCode, CarrierCode from AirlineCabinClass$

However the airlinename field is a unique field and im getting the error:
Cannot insert duplicate key row in object 'dbo.AIRLINE' with unique index 'UNC_Airline_AirlineName'.

I need some debug sql to try and show me the duplicate name. Please be aware that the data above is a sample and there are a lot more records for that airline and also many other airlines with a similiar amount of classes.

I was trying something like:
select DISTINCT  [Name], CarrierCode, COUNT([NAME]) from AirlineCabinClass$
GROUP BY [NAME], TicketingCode, CarrierCode

but count(name) is given me 26 for the example above as there are 26 records for that airline name.

Im assuming that there will be an airline name that has two carriercodes or ticketing codes hence leaving 2 names the same in my distinct list.

Can anyone help?
0
Comment
Question by:scm0sml
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 19

Expert Comment

by:frankytee
ID: 21873579
you are checking the data from your temp table which may have unique airline names. however since you are inserting into table AIRLINE , you need to check if there are any existing records (by airline name) that appear in both tables.
below will return records from your temp table whose name already appears in your airline table:

select t.*
from AirlineCabinClass$ t join AIRLINE a
on t.name = a.airlinename
0
 

Author Comment

by:scm0sml
ID: 21874304
no there are not it is an empty table

0
 
LVL 19

Accepted Solution

by:
frankytee earned 2000 total points
ID: 21879922
if you want to find dupes by name field in your table then
select [Name], COUNT([CarrierCode]) as x
from AirlineCabinClass$
GROUP BY [NAME]
having COUNT([CarrierCode]) > 1
0

Featured Post

Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question