Click here to Skip to main content
15,901,283 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,
I have a table with columns: LastName, FirstName, DOB, CurrentWeight, CurrentEmpStat, DateOfVIsit, Patient_ID and Rec_ID.
In this table I can have same person with multiple entries. Record will have a unique Rec_ID.
I need to check the table and find the rows with same LastName, FirstName, DOB and Patient_ID.
Then I need to delete all multiple records (LastName, FirstName, DOB, Patient_ID)from that table except the newest record. What's the best way to achieve this?
Thankx in advance.
JL.

Hello again,
Actually, let me describe the problem a little better. What I actually want to achieve is to go through the table row by row and compare data in those rows. While comparing one to another and finding same values in four out of five columns in those rows it should delete the one whose 5th value is higher/lower (Edit: assuming OP knows which to choose) And continue processing until those four columns are unique in the table.

[Edit: Please dont leave CAPS ON :)]

HELLO AGAIN,
ACTUALLY, LET ME DESCRIBE IT LITTLE BETTER.
WHAT I'M LOOKING FOR LITTERALY IS TO GO THROUGH THE DATABASE ROW BY ROW AND COMPARE DATA IN THOSE ROWS. WHEN COMPERING ONE TO ONE AND FINDING SAME VALUES IN FOUR OUT OF FIVE COLUMNS IN THOSE ROWS IT SHOULD DELETE THE ONE WHICH VALUE OF THE FIFTH COLUMN IS HIGHER. OR SMALLER. THEN WHEN DELETE IS DONE IT SHOULD GO AND COMPARE IT TO THE ANOTHER ROW IN THE DATABASE TILL THE END. SO BASICALLY, NO ROWS WITH SAME FOUR VALUES.
HOPE, IT'S NOT THAT COMFUSING.. :)

JL
Posted
Updated 2-Jun-11 4:38am
v4
Comments
Wayne Gaylard 2-Jun-11 9:05am    
Do you want to do this in c# or in SQL Management studio ?
Sunasara Imdadhusen 2-Jun-11 9:27am    
you need to get only distinct row based on (LastName, FirstName, DOB, Patient_ID)?

1 solution

Assuming a table like this
SQL
create table test(
    rec_id uniqueidentifier -- your PK
,   rec_timestamp timestamp -- something to let us find the latest record
,   pid varchar(max) -- patient id, first name, last name, and DOB
,   first varchar(max)
,   last varchar(max)
,   dob date
)
this delete statement should do the trick:
SQL
delete lhs
from test lhs
where exists (
    select *
    from test rhs
    where lhs.first=rhs.first
      and lhs.last=rhs.last
      and lhs.dob=rhs.dob
      and lhs.pid=rhs.pid
      and rhs.rec_timestamp > lhs.rec_timestamp
)
Good luck!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900