Solved

SQL 2008 R2 Delete Statement

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

839 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