Solved

REMOVING DUPLICATE VALUES FROM MY DATA FOR AN INSERT

Posted on 2008-06-26
3
199 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
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 500 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Suggested Solutions

Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

860 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