Solved

SQL 2008 R2 Delete Statement

Posted on 2014-01-30
3
278 Views
Last Modified: 2014-02-11
I have a table called Inspection.

I have multiple duplications.

Is there a query I can use to delete all duplications but leave the original??

I have attached a spread sheet with a sample of the data I have (Page called "IS") and a sheet with what it should be (Page called "Should Be")

Im not sure if this all makes sence but heres hoping!!

Thanks!!
Experts.xlsx
0
Comment
Question by:HDM
3 Comments
 
LVL 24

Accepted Solution

by:
chaau earned 250 total points
ID: 39822946
You need to use ROW_NUMBER:
;with cte as
(SELECT *,
ROW_NUMBER() OVER(PARTITION BY [DATE], [APPLICANT], [REMARKS], [CONTAINER] ORDER BY [DATE], [APPLICANT], [REMARKS], [CONTAINER]) AS rn 
from Inspection)
delete from cte where rn > 1;

Open in new window

Disclaimer: Please backup your table prior to doing any DELETEs on the live data
0
 
LVL 12

Expert Comment

by:Tony303
ID: 39822956
In these situations since there is no ID field I take the following approach.

Backup.
SELECT the DISTINCT records into a temp table.
DELETE all rows from the Original table.
INSERT FROM the temp table back into the Original.

All records are now unique.

Check data.
Delete temp table when happy.



Then you need to change what ever caused the duplications in the first place.

T
0
 
LVL 35

Assisted Solution

by:David Todd
David Todd earned 250 total points
ID: 39822981
Hi,

-- SQL 2012
use ExpertsExchange
go

if exists(
	select *
	from sys.objects
	where
		object_id = object_id( N'dbo.Inspection' )
		and type in( N'U' )
	)
	drop table dbo.Inspection
;
go

if not exists(
	select *
	from sys.objects
	where
		object_id = object_id( N'dbo.Inspection' )
		and type in( N'U' )
	)
	create table dbo.Inspection (
		InspectionDate date
		, Application varchar( 20 )
		, Remarks varchar( 20 )
		, CarrierType varchar( 20 )
		, Quantity int
		, QuantityCount int
		, Container varchar( 20 )
		, Grade varchar( 10 )
		, CertificateNumber int
		, QuantityThousandWeight float
		, QuantityHundredWeight float
		, UID2 int
	) on [PRIMARY]
go

insert dbo.Inspection( 
	InspectionDate 
	, Application 
	, Remarks 
	--, CarrierType 
	, Quantity 
	, QuantityCount 
	, Container 
	, Grade 
	, CertificateNumber 
	, QuantityThousandWeight 
	, QuantityHundredWeight 
	, UID2 
	)
	values
( '3/01/2014',	'Customer A',	'Burbank',		2212,	50,	'Cartons',	'1 US # 1',	774723,	110600,	1106,	1),
( '3/01/2014',	'Customer A',	'Burbank',		4000,	10,	'Poly Sax',	'1 US # 1',	774723,	40000,	400,	2),
( '3/01/2014',	'Customer A',	'Burbank',		420,	10,	'Film Sax',	'1 US # 1',	774723,	4200,	42,	3),
( '3/01/2014',	'Customer A',	'Burbank',		420,	5,	'Film Sax',	'1 US # 1',	774723,	2100,	21,	4),
( '3/01/2014',	'Customer A',	'Burbank',		18,	2000,	'Bulk Bins',	'1 US # 1',	774723,	36000,	360,	5),
( '3/01/2014',	'Customer A',	'Burbank',		850,	50,	'Mesh Sax',	'2 US # 2',	774723,	42500,	425,	6),
( '3/01/2014',	'Customer A',	'Burbank',		84,	50,	'Paper Sax',	'2 US # 2',	774723,	4200,	42,	7),
( '3/01/2014',	'Customer A',	'Burbank',		105,	50,	'Cartons',	'2 US # 2',	774723,	5250,	52.5,	8),
( '3/01/2014',	'Customer A',	'Burbank',		2212,	50,	'Cartons',	'1 US # 1',	774723,	110600,	1106,	1),
( '3/01/2014',	'Customer A',	'Burbank',		4000,	10,	'Poly Sax',	'1 US # 1',	774723,	40000,	400,	2),
( '3/01/2014',	'Customer A',	'Burbank',		420,	10,	'Film Sax',	'1 US # 1',	774723,	4200,	42,	3),
( '3/01/2014',	'Customer A',	'Burbank',		420,	5,	'Film Sax',	'1 US # 1',	774723,	2100,	21,	4),
( '3/01/2014',	'Customer A',	'Burbank',		18,	2000,	'Bulk Bins',	'1 US # 1',	774723,	36000,	360,	5),
( '3/01/2014',	'Customer A',	'Burbank',		850,	50,	'Mesh Sax',	'2 US # 2',	774723,	42500,	425,	6),
( '3/01/2014',	'Customer A',	'Burbank',		84,	50,	'Paper Sax',	'2 US # 2',	774723,	4200,	42,	7),
( '3/01/2014',	'Customer A',	'Burbank',		105,	50,	'Cartons',	'2 US # 2',	774723,	5250,	52.5,	8),
( '3/01/2014',	'Customer A',	'Burbank',		2212,	50,	'Cartons',	'1 US # 1',	774723,	110600,	1106,	1),
( '3/01/2014',	'Customer A',	'Burbank',		4000,	10,	'Poly Sax',	'1 US # 1',	774723,	40000,	400,	2),
( '3/01/2014',	'Customer A',	'Burbank',		420,	10,	'Film Sax',	'1 US # 1',	774723,	4200,	42,	3),
( '3/01/2014',	'Customer A',	'Burbank',		420,	5,	'Film Sax',	'1 US # 1',	774723,	2100,	21,	4)
go

-- Sample Date
select *, row_number( ) over( partition by InspectionDate, Application, Remarks, Container, Grade, CertificateNumber order by InspectionDate, Application, Remarks, Container, Grade, CertificateNumber )
from dbo.Inspection
;

-- Delete
delete i
from (
	select *, row_number( ) over( partition by InspectionDate, Application, Remarks, Container, Grade, CertificateNumber order by InspectionDate, Application, Remarks, Container, Grade, CertificateNumber ) as RowNumber
	from dbo.Inspection
	) i
where
	i.RowNumber > 1
;

-- Results showing now no duplicates
select *, row_number( ) over( partition by InspectionDate, Application, Remarks, Container, Grade, CertificateNumber order by InspectionDate, Application, Remarks, Container, Grade, CertificateNumber )
from dbo.Inspection
;

Open in new window


I may have been a bit heavy handed, and delete a few too many rows, but you'll get the idea.

HTH
  David
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

747 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

9 Experts available now in Live!

Get 1:1 Help Now