Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL 2008 R2 Delete Statement

Posted on 2014-01-30
3
Medium Priority
?
299 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 25

Accepted Solution

by:
chaau earned 1000 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 1000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

596 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