Solved

REMOVING DUPLICATE VALUES FROM MY DATA FOR AN INSERT

Posted on 2008-06-26
3
196 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
  • 2
3 Comments
 
LVL 19

Expert Comment

by:frankytee
Comment Utility
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
Comment Utility
no there are not it is an empty table

0
 
LVL 19

Accepted Solution

by:
frankytee earned 500 total points
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now