SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


unique patients from duplicate patient row entries


unique patients from duplicate patient row entries

Author
Message
boehnc
boehnc
Valued Member
Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)

Group: General Forum Members
Points: 60 Visits: 102
I have one table "patients"...one row per patient.

Patient.ID, Patient.Name, Patient.address, Patient.gender

1/Jim/123 elm/M

I have another table of patient diagnosis codes. It can have multiple dx codes per patient.

dx.patientid/dx.dxcode/dx.dxcodedesc

1/23/diabetes
1/35/chf
2/33/pnem
2/34/stroke
2/56/headache

I need to bring back in a table unique patients where dxcode = 23 or 34.

one patient can have 23 and 34..I just need to bring back whether they had one or the other. If I do a left outer join, a patient with 23 and 34 will bring back two entries. How do I have it bring back just one unique entry? Thanks
Evil Kraig F
Evil Kraig F
SSCrazy Eights
SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)

Group: General Forum Members
Points: 8567 Visits: 7660
boehnc (9/17/2012)

I need to bring back in a table unique patients where dxcode = 23 or 34.

one patient can have 23 and 34..I just need to bring back whether they had one or the other. If I do a left outer join, a patient with 23 and 34 will bring back two entries. How do I have it bring back just one unique entry? Thanks


If you setup schema and data, we'll be able to provide you actual code, but in the meantime, psuedocode will have to do:

SELECT
mt.ID, MAX( c.dxCode)
FROM
MainTable AS mt
JOIN
dxcodes AS c
ON mt.Id = c.Id
GROUP BY
mt.ID




- Craig Farrell

Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

For better assistance in answering your questions | Forum Netiquette
For index/tuning help, follow these directions. |Tally Tables

Twitter: @AnyWayDBA
rhythmk
rhythmk
SSC Eights!
SSC Eights! (906 reputation)SSC Eights! (906 reputation)SSC Eights! (906 reputation)SSC Eights! (906 reputation)SSC Eights! (906 reputation)SSC Eights! (906 reputation)SSC Eights! (906 reputation)SSC Eights! (906 reputation)

Group: General Forum Members
Points: 906 Visits: 1078
boehnc (9/17/2012)
I have one table "patients"...one row per patient.

Patient.ID, Patient.Name, Patient.address, Patient.gender

1/Jim/123 elm/M

I have another table of patient diagnosis codes. It can have multiple dx codes per patient.

dx.patientid/dx.dxcode/dx.dxcodedesc

1/23/diabetes
1/35/chf
2/33/pnem
2/34/stroke
2/56/headache

I need to bring back in a table unique patients where dxcode = 23 or 34.

one patient can have 23 and 34..I just need to bring back whether they had one or the other. If I do a left outer join, a patient with 23 and 34 will bring back two entries. How do I have it bring back just one unique entry? Thanks


Left outer join !!
Can you please post your query with expected output.

--rhythmk
------------------------------------------------------------------
To post your question use below link

http://www.sqlservercentral.com/articles/Best+Practices/61537/
:-)
boehnc
boehnc
Valued Member
Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)

Group: General Forum Members
Points: 60 Visits: 102
this is basically the code...this join... left join UserView.vwEncounterDiagnosis d on d.VisitId=a.VisitId...will give me multiple rows per patient

i might need help with the diagnosis order/code statements in the where code as well?


select a.EncounterKey,a.DivisionName, a.VisitId,a.MedicalRecordNumber,a.InOutIndicator,a.EDFlag,a.AgeYear,a.PatientGender, a.ChargeTotal,pa.City,pa.State,a.PatientZip

from UserView.vwAccount a
left join UserView.vwPayer p on p.PayerPlanKey = a.PayerPrimaryKey
left join UserView.vwPatient pa on pa.PatientKey=a.PatientKey
left join UserView.vwEncounterDiagnosis d on d.VisitId=a.VisitId

where a.DischargeDate>= '2011-01-01' and a.DischargeDate<='2012-01-01'
and p.PayerClassGroupName in ('blue cross','true blue')
and a.ChargeTotal>0
and ((d.DiagnosisOrder between '1' and '4' and d.DiagnosisCode between '140.0' and '239.99')
or (d.DiagnosisOrder between '1' and '4' and d.DiagnosisCode between 'v10.00' and 'v10.99')
or (d.DiagnosisOrder between '1' and '4' and d.DiagnosisCode between 'v76.0' and 'v76.99')
or (d.DiagnosisOrder between '1' and '4' and d.DiagnosisCode in ('v66.1','v66.2','v67.1','v67.2','v58.42')))
rhythmk
rhythmk
SSC Eights!
SSC Eights! (906 reputation)SSC Eights! (906 reputation)SSC Eights! (906 reputation)SSC Eights! (906 reputation)SSC Eights! (906 reputation)SSC Eights! (906 reputation)SSC Eights! (906 reputation)SSC Eights! (906 reputation)

Group: General Forum Members
Points: 906 Visits: 1078
boehnc (9/17/2012)

this is basically the code...this join... left join UserView.vwEncounterDiagnosis d on d.VisitId=a.VisitId...will give me multiple rows per patient

i might need help with the diagnosis order/code statements in the where code as well?


select a.EncounterKey,a.DivisionName, a.VisitId,a.MedicalRecordNumber,a.InOutIndicator,a.EDFlag,a.AgeYear,a.PatientGender, a.ChargeTotal,pa.City,pa.State,a.PatientZip

from UserView.vwAccount a
left join UserView.vwPayer p on p.PayerPlanKey = a.PayerPrimaryKey
left join UserView.vwPatient pa on pa.PatientKey=a.PatientKey
left join UserView.vwEncounterDiagnosis d on d.VisitId=a.VisitId

where a.DischargeDate>= '2011-01-01' and a.DischargeDate<='2012-01-01'
and p.PayerClassGroupName in ('blue cross','true blue')
and a.ChargeTotal>0
and ((d.DiagnosisOrder between '1' and '4' and d.DiagnosisCode between '140.0' and '239.99')
or (d.DiagnosisOrder between '1' and '4' and d.DiagnosisCode between 'v10.00' and 'v10.99')
or (d.DiagnosisOrder between '1' and '4' and d.DiagnosisCode between 'v76.0' and 'v76.99')
or (d.DiagnosisOrder between '1' and '4' and d.DiagnosisCode in ('v66.1','v66.2','v67.1','v67.2','v58.42')))


Did you try the pseudo code posted by Kraig ? or try with DISTINCT.

--rhythmk
------------------------------------------------------------------
To post your question use below link

http://www.sqlservercentral.com/articles/Best+Practices/61537/
:-)
Evil Kraig F
Evil Kraig F
SSCrazy Eights
SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)

Group: General Forum Members
Points: 8567 Visits: 7660
boehnc (9/17/2012)

this is basically the code...this join... left join UserView.vwEncounterDiagnosis d on d.VisitId=a.VisitId...will give me multiple rows per patient

i might need help with the diagnosis order/code statements in the where code as well?


If you're not going to be able to extrapolate from psuedocode, we're going to need a lot more details from you to provide you a working example.

Please see the first link in my signature for what we'll require to properly setup working code for your example and to show you exactly what you'll need to do. This includes schema for test structures, sample data, and expected results from that data, in a format we can cut and paste into our own servers and after review run scripts against what they create.


- Craig Farrell

Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

For better assistance in answering your questions | Forum Netiquette
For index/tuning help, follow these directions. |Tally Tables

Twitter: @AnyWayDBA
boehnc
boehnc
Valued Member
Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)

Group: General Forum Members
Points: 60 Visits: 102
thank you for your responses and link. I'll remember that for future questions I might have. They pseudo code worked fine..thanks for the advice.
Nils Gustav Stråbø
Nils Gustav Stråbø
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2433 Visits: 3575
You could also solve this using EXISTS.

select p.*
from partients p
where exists(select * from dx where dx.patiendid=p.partientid and dx.dxcode in (23,24))



I'm not sure if this approach will perform better than Evil Craig F's solution so you need to test it. At least the there is no aggregation steps in my approach.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search