Eliminating Duplicates Help please

Please support our MS SQL advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2004
Posts: 217
Reputation: marceta is an unknown quantity at this point 
Solved Threads: 0
marceta marceta is offline Offline
Posting Whiz in Training

Eliminating Duplicates Help please

 
0
  #1
Aug 29th, 2007
Hi guys,

The question I need to answer is
This query is about patients who got the same illness on the same day. For each pair of such patients, display one row showing the illness code, the date of onset, and the patient number of each person involved, e.g.
FLU 2007-08-21 1021 1024

Do not show the same pair of patients more than once, i.e. with the patient numbers swapped around, e.g. don’t show:
FLU 2007-08-21 1021 1024
FLU 2007-08-21 1024 1021
I have done so successfully...but I can not for the life of me work out how to not show the pair more than once!!! I have tried to add distinct patient numbers as that is all that my SQL skills can do, but they will still show duplicates! Here is my code

With Joins
  1. SELECT I1.IllnessCode, I1.DateOfOnset, P1.PatientNum, P2.PatientNum
  2. FROM Patients P1, Patients P2, Illhealth I1, IllHealth I2
  3. WHERE P1.PatientNum = I1.PatientNum
  4. AND P2.PatientNum = I2.PatientNum
  5. AND I1.DateofOnSet = I2.DateofOnset
  6. AND I1.IllnessCode = I2.IllnessCode
  7. AND P1.PatientNum NOT LIKE P2.PatientNum

With Subquery and Joins!
  1. SELECT I1.IllnessCode, I1.DateOfOnset, P1.PatientNum, P2.PatientNum
  2. FROM Patients P1, Patients P2, Illhealth I1
  3. WHERE I1.IllnessCode IN (
  4. SELECT I1.IllnessCode
  5. FROM IllHealth I2
  6. WHERE P1.PatientNum = I1.PatientNum
  7. AND P2.PatientNum = I2.PatientNum
  8. AND I1.DateofOnSet = I2.DateofOnset
  9. AND I1.IllnessCode = I2.IllnessCode
  10. AND P1.PatientNum NOT LIKE P2.PatientNum)

Here is the output. Same for each query (just differnt orders). I know its right as I have manually checked, but cant get those darn duplicates out!!

http://www.imagehosting.com/out.php/i1079014_output.jpg ([ img] tags not working?)

Can someone please help me out

Thanks in advance.

Pete


PS.Here is the db layout in case you need it.

  1. Patients (PatientNum, GivenName, FamilyName, Gender, Address, DateOfBirth, DateAdmitted, WardId)
  2. Illhealth (PatientNum, IllnessCode, DateOfOnset, DiagnosedByStaffNum)
  3. Illnesses (IllnessCode, IllnessDescription, TypicalDuration)
  4. Staff (StaffNum, GivenName, FamilyName, MedicalRole, WorkPhoneNum, HomePhoneNum)
  5. Wards (WardId, WardName)
  6. Roster (StaffNum, WeekNum, DayNum, ShiftNum, WardId, InCharge)
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 217
Reputation: marceta is an unknown quantity at this point 
Solved Threads: 0
marceta marceta is offline Offline
Posting Whiz in Training

Re: Eliminating Duplicates Help please

 
0
  #2
Aug 30th, 2007
  1. P1.PatientNum < P2.PatientNum

*slaps head*!

geez cant belive i couldn't think of that!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC