Solved

delete duplicate data

Posted on 2016-09-23
4
61 Views
Last Modified: 2016-09-26
I need to delete duplicate data
i.e. if i have 2 rows exactly the same, delete one of the rows

I have a table called Tbl_Data
It has these columns

ID
GPSDateTime (datetime)
ReportID (int)
DeviceID (int)

If all 3 columns match data (except ID) I want to delete one of the rows so i'm left with unique data rows instead duplicates

how might i Do this?
0
Comment
Question by:websss
  • 2
  • 2
4 Comments
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 41812808
For best performance, if the table has an index with all three of those columns in it, and is keyed by one or more of them, start with column first in the PARTITION BY.
For example, say there was an index on ( ReportID, GPSDateTime ) that included ( DeviceID ), then you would ORDER BY ReportID, GPSDateTime, DeviceID.  The idea is to use any existing "pre-sorting" as much as possible.


;WITH cte_dups AS (
    SELECT *, ROW_NUMBER() OVER(PARTITION BY GPSDateTime, ReportID, DeviceID) AS row_num
    FROM Tbl_Data
)
DELETE FROM cte_dups
WHERE row_num > 1
0
 

Author Comment

by:websss
ID: 41813395
Thanks Scott

I'm getting the errror:

Msg 4112, Level 15, State 1, Line 2
The function 'ROW_NUMBER' must have an OVER clause with ORDER BY.
0
 

Author Closing Comment

by:websss
ID: 41813418
Got it thanks
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 41816242
Yeah, sorry, I left out the ORDER BY, which must appear, even if it's meaningless:

;WITH cte_dups AS (
    SELECT *, ROW_NUMBER() OVER(PARTITION BY GPSDateTime, ReportID, DeviceID ORDER BY GPSDateTime) AS row_num
    FROM Tbl_Data
)
DELETE FROM cte_dups
WHERE row_num > 1
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Query Syntax 17 36
TSQL query to generate xml 4 35
Better way to make a query with date filter. 5 27
replace \ by - in select 4 21
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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…
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.

777 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