Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to delete duplicate records based on specific columns in SQL Serevr 2005 / 2008?

Posted on 2009-04-10
7
Medium Priority
?
294 Views
Last Modified: 2012-05-06
I have the table A and it doesn't have any PK or unique key . I want to remove duplicate rows based on DeptID and Date. And I need to keep min(sessionid) for the duplicate records in the table and remove other one.
SessionID  DeptID   Date          accessdate  status
500             100     20090410   20010101    2
561             100      20090410  20010105    5
590              100     20090410  20021212    5
600              101     20090409  20050507    3
610              101    20090409   20050510    4
700              103     20090410  20071012   3
701               105   20090410   20081011   3

ForDeptID 100, i need to keep  500             100     20090410   20010101    2 row and delete remaining Deptid 100 rows.  Please advise me how to do this efficiently.  Tahnks in advance
0
Comment
Question by:PKTG
7 Comments
 
LVL 5

Assisted Solution

by:mfhorizon
mfhorizon earned 600 total points
ID: 24116637
User this query

select  DeptID, min(SessionID)SessionID into filteredTable from A
group by DeptID
go

Delete from A where convert(DeptID,varchar(10))+convert(SessionID,varchar(10)) not in (select convert(DeptID,varchar(10))+convert(SessionID,varchar(10)) from filteredTable)
go

select * from A

0
 
LVL 60

Accepted Solution

by:
chapmandew earned 400 total points
ID: 24116640
0
 
LVL 5

Assisted Solution

by:mfhorizon
mfhorizon earned 600 total points
ID: 24116666
I have assumed that DeptID and SessionID are integer in the table...

Let me read out above queries:

1. select all deprtment id's and corresponding minimum session id's in the table A and store them to another table filteredTable

2. delete from table A all those where combined string DeptID&SessionID is not found in the list of DeptID&SessionID fromt the filteredTable

Please take backup of your table(s) before proceeding with this logic. You may need to restore the backup and do some tweaking according to the case.
0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 11

Assisted Solution

by:bmatumbura
bmatumbura earned 200 total points
ID: 24116693
0
 
LVL 13

Assisted Solution

by:sm394
sm394 earned 400 total points
ID: 24116723
--verify
select *
from(
      select row_number() over(partition by DeptID  , Date order by SessionID  ) as RowNo,*  
      from YOURTABLE
      ) t1
where RowNo>1

--Delete
delete t1
from(
      select row_number() over(partition by DeptID  , Date order by SessionID  ) as RowNo,*  
      from YOURTABLE
      ) t1
where RowNo>1
0
 
LVL 5

Assisted Solution

by:mfhorizon
mfhorizon earned 600 total points
ID: 24116725
In SQL SERVER 2005 and SQL SERVER 2008 there is CAST functionwhich is more reliable so I will repeat my query

select  DeptID, min(SessionID)SessionID into filteredTable from A
group by DeptID
go

Delete from A where Cast(DeptID as varchar(10))+cast(SessionID as varchar(10)) not in (select Cast(DeptID as varchar(10))+cast(SessionID as varchar(10)) from filteredTable)
go

select * from A

ENJOY!!!
0
 
LVL 31

Assisted Solution

by:RiteshShah
RiteshShah earned 400 total points
ID: 24117026
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

877 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