Hi.
I have the following data where the duplicates are the episode numbers. The problem is that there is a date associated with each record. I need to show the date as well as the program title in addition to the episode. I executed a delete process, but the duplicates are not removed.
Here's a sample of the data (there are thousands of records):
I tried the following code, which did not give the right results. How can I revise my process so that the duplicate records are accurately removed?
I have the following data where the duplicates are the episode numbers. The problem is that there is a date associated with each record. I need to show the date as well as the program title in addition to the episode. I executed a delete process, but the duplicates are not removed.
Here's a sample of the data (there are thousands of records):
Code:
Air Date Episode Program
12/31/09 CMTW-09001T MW
12/31/09 CMTW-09001 MW
12/31/09 CMTW-09001T MW
12/31/09 CMTW-09001 MW
12/31/09 CMTW-09001 MW
12/31/09 CRCA1044T RFT
12/31/09 CRCA1044 RFT
12/31/09 CRCA1044 RFT
12/31/09 CRCA1044T RFT
I tried the following code, which did not give the right results. How can I revise my process so that the duplicate records are accurately removed?
Code:
/*Delete rows that have duplicate values for the episodes*/
BEGIN TRAN
SELECT Airdate, Episode, COUNT(Episode) as NumOccurences, Program,
FROM AsrunLogs
GROUP BY Airdate, Episode, Program
HAVING (Count(Episode) >1)
IF @@ERROR <> 0
BEGIN
ROLLBACK TRAN
END
/*As there are various duplicates, select the duplicate values into a holding table*/
Select Airdate, Episode, Program, COUNT(*) as NumOccur
INTO HoldKey
FROM AsrunLogs
GROUP BY Airdate, Episode, Program
HAVING (COUNT(*) > 1)
IF @@ERROR <> 0
BEGIN
ROLLBACK TRAN
END
/*Select the duplicate rows into a holding table, eliminating duplicates in the process*/
Select Distinct AsrunLogs.*
Into holdups
from AsrunLogs, holdkey
WHERE AsrunLogs.Episode= holdkey.episode
IF @@ERROR <> 0
BEGIN
ROLLBACK TRAN
END
/*Delete the duplicate rows from original table*/
Delete AsrunLogs
From AsrunLogs a, HoldKey h
WHERE a.Episode= h.episode
IF @@ERROR <> 0
BEGIN
ROLLBACK TRAN
END
/*Put the Unique rows back in the original table*/
Insert INTO Asrunlogs (Airdate, Episode, Program)
SELECT Airdate, Episode, Program FROM holdups
IF @@ERROR <> 0
BEGIN
ROLLBACK TRAN
END
Commit Tran
GO