Solved

SQL 2008 R2 Delete Statement

Posted on 2014-01-30
3
279 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hyperlink data type in SQL 3 27
CPU high usage when update statistics 2 29
Tsql query 6 21
Replace the integer portion in CAST with a column 4 14
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

770 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