?
Solved

SQL 2008 R2 Delete Statement

Posted on 2014-01-30
3
Medium Priority
?
294 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
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…
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

771 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