943,909 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Unsolved
  • Views: 2621
  • MS SQL RSS
Aug 29th, 2007
0

Eliminating Duplicates Help please

Expand Post »
Hi guys,

The question I need to answer is
Quote ...
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
sql Syntax (Toggle Plain Text)
  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!
sql Syntax (Toggle Plain Text)
  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.

MS SQL Syntax (Toggle Plain Text)
  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)
Similar Threads
Reputation Points: 13
Solved Threads: 0
Posting Whiz in Training
marceta is offline Offline
217 posts
since May 2004
Aug 30th, 2007
0

Re: Eliminating Duplicates Help please

SQL Syntax (Toggle Plain Text)
  1. P1.PatientNum < P2.PatientNum

*slaps head*!

geez cant belive i couldn't think of that!
Reputation Points: 13
Solved Threads: 0
Posting Whiz in Training
marceta is offline Offline
217 posts
since May 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS SQL Forum Timeline: sql server middleware
Next Thread in MS SQL Forum Timeline: Decimal Value





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC