Here is the simplified structure of my data. I have Access experience, but I have been upgraded to the our corporate database and am using SQL Management Server 2012 so I am feeling a little lost.
I am trying to keep all records from PatientSID as long as at least 1 record has an ICDCode of 332.0
In the above data I want to include the 3 records for PatientSID 1 and the 1 record from PatientSID 2, but no records for PatientSID 3.
I have looked at various GROUPing, RANK, ROW_NUMBER statements, but I do not think any of these will work. I am thinking I might need to rethink my JOINS and have a RIGHT JOIN after first creating a subquery of only ICDCode of 332.0, but I am having trouble thinking that through. Here is the SQL that has all the information, but not filtered.
Thank you for any suggestions.
You don't know what you don't know...
Code:
[tt]
PatientSID ICDCode
1 332.0
1 332.0
1 709.8
2 332.0
3 333.9
3 250.6
[/tt]
I am trying to keep all records from PatientSID as long as at least 1 record has an ICDCode of 332.0
In the above data I want to include the 3 records for PatientSID 1 and the 1 record from PatientSID 2, but no records for PatientSID 3.
I have looked at various GROUPing, RANK, ROW_NUMBER statements, but I do not think any of these will work. I am thinking I might need to rethink my JOINS and have a RIGHT JOIN after first creating a subquery of only ICDCode of 332.0, but I am having trouble thinking that through. Here is the SQL that has all the information, but not filtered.
SQL:
USE LSV
SELECT
t.PatientSID
,i.ICDCode
--,p.PatientName
--,p.PatientSSN
--,t.EntryDateTime
--,d.TIUDocumentDefinition
--,i.ICDSID
--,i.ICDDescription
FROM
TIU.TIUDocument AS t
LEFT JOIN dim.TIUDocumentDefinition AS d ON t.TIUDocumentDefinitionSID = d.TIUDocumentDefinitionSID
LEFT JOIN SPatient.SPatient AS p ON t.PatientSID = p.PatientSID
LEFT JOIN Outpat.VDiagnosis AS v ON t.VisitSID = v.VisitSID
LEFT JOIN dim.ICD AS i ON v.ICDSID = i.ICDSID
WHERE
t.EntryDateTime BETWEEN '2002/1/1' AND '2013/12/16'
AND d.TIUDocumentDefinition LIKE 'NEUROLOGY MOVEMENT%'
Thank you for any suggestions.
You don't know what you don't know...