Link to home
Start Free TrialLog in
Avatar of olivejooce
olivejooce

asked on

How do I remove duplicate SQL records, leaving only one by most recent date?

I need to create a SQL View that removes duplicate records. Right now when I run the query, it is pulling in all records and I want it to only pull up the most recently updated record.

The SQL View name is:  FM_Report and it is pulling info from two Tables: FM_Project and Project_Status_History

-- I want to remove all duplicate records sharing the same: SBBC_Project_Number (found in both tables)
-- I want to leave just the most recent record by the most recent update: Project_Status_Date (found in the Project_Status_History table)

Can you suggest a script for this, please?
Avatar of Sharath S
Sharath S
Flag of United States of America image

What is your SQL  version?
ASKER CERTIFIED SOLUTION
Avatar of reb73
reb73
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

>> I need to create a SQL View that removes duplicate records.
View can not delete your records. you can create an SP for that. 
Do you want to permanently delete the duplicate records from your both tables or you just want to display the distinct records?
 
For hard deltes from your tables, you can use this.
 
 DELETE T FROM (
 SELECT row_number() OVER(ORDER BY SBBC_Project_Number,YourDateColumn DESC) as row_number, * FROM FM_Project ) T
 WHERE row_number not in (
 SELECT min(row_number) FROM (SELECT row_number() OVER(ORDER BY SBBC_Project_Number,YourDateColumn DESC) as row_number, * FROM FM_Project) T 
 GROUP BY SBBC_Project_Number )
 -- check whether the records are deleted.
 SELECT * FROM FM_Project
 
 
-- similarly for Project_Status_History table also you can write the code.
  DELETE T FROM (
 SELECT row_number() OVER(ORDER BY SBBC_Project_Number,YourDateColumn DESC) as row_number, * FROM Project_Status_History ) T
 WHERE row_number not in (
 SELECT min(row_number) FROM (SELECT row_number() OVER(ORDER BY SBBC_Project_Number,YourDateColumn DESC) as row_number, * FROM Project_Status_History) T 
 GROUP BY SBBC_Project_Number )
 
 

Open in new window

Avatar of olivejooce
olivejooce

ASKER

I tried it and it didn't work.  I'm using SQL Server 2000 (ancient, I know).  I'm new to all this so it's probably me.  I'm attaching a snapshot of the SQL View, perhaps it'll help.  Thank you!
sh-01.gif

Question - Do you want to hard delete the records from your tables? Or do you want your view to display only those records which are recently updated?
If you just want to display through View, you need not to delete the records in your tables?
Hi Sharath,
No, I don't want to delete the records.  I just want the view to display only one project record(SBBC_Project_Number) that was most recently updated (Project_Status_Date).

Thanks for your help!!!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial