I am not quite sure where I am going wrong. I am trying to disply if a person is on more than one drug it displays on one line with a YES if they are and a blank if they are not. Each drug is a separate column. My problem is when a person is on more than one drug both columns are blank rather than a YES in each column. I think my problem lies with the second query rather than the final query.
#cohort is previous queried and stored in a temp table for now.
When I run the first SELECT independently the data looks like this:
[tt]
PatientSID DrugNameWithoutDoseSID RowId
16051414 308716 1
16051414 308716 2
16051414 308716 3
16051414 321133 1
16066127 308716 1
16066127 308716 2
16067974 321133 1
16157440 321133 1
25397863 308716 1
[/tt]
In this example personID 16051414 is the only person on 2 drug.
Here is my code:
The above gives this output. As you can see person 16051414 has a blank under both Glipizide and Metformin.
[tt]
PatientSID Glipizide Metformin Glip/Met Combo
16051414
16066127 YES
16067974 YES
16157440 YES
25397863 YES
[/tt]
Thanks for any suggestions.
You don't know what you don't know...
#cohort is previous queried and stored in a temp table for now.
When I run the first SELECT independently the data looks like this:
[tt]
PatientSID DrugNameWithoutDoseSID RowId
16051414 308716 1
16051414 308716 2
16051414 308716 3
16051414 321133 1
16066127 308716 1
16066127 308716 2
16067974 321133 1
16157440 321133 1
25397863 308716 1
[/tt]
In this example personID 16051414 is the only person on 2 drug.
Here is my code:
SQL:
; With Drug AS
(
SELECT
rx.PatientSID
,drugid.DrugNameWithoutDoseSID
,ROW_NUMBER() OVER (PARTITION BY rx.PatientSID,
drugid.DrugNameWithoutDoseSID
ORDER BY rx.PatientSID
) As RowId
FROM
RxOut.RxOutpat AS rx
INNER JOIN #cohort AS cohort ON rx.PatientSID = cohort.PatientSID
INNER JOIN Dim.NationalDrug AS drugid ON rx.NationalDrugSID = drugid.NationalDrugSID
WHERE
drugid.DrugNameWithoutDoseSID IN (321133, 308716, 309474)
AND rx.IssueDate Between '2008-01-01' AND '2014-03-12'
AND rx.RxStatus IN ('ACTIVE', 'SUSPENDED','EXPIRED', 'HOLD')
)
,
MinDrug AS
(
SELECT
Drug.PatientSID
,MIN(Case When drug.DrugNameWithoutDoseSID = 321133
Then 'YES'
ELSE ''
End) As [GLIPIZIDE]
,MIN(Case When drug.DrugNameWithoutDoseSID = 308716
Then 'YES'
ELSE ''
End) As [METFORMIN]
,MIN(Case When drug.DrugNameWithoutDoseSID = 309474
Then 'YES'
ELSE ''
End) As [GLIP/MET COMBO]
FROM Drug
WHERE RowID = 1
GROUP BY
Drug.PatientSID
)
SELECT
cohort.PatientSID
,mindrug.[Glipizide]
,mindrug.[Metformin]
,mindrug.[Glip/Met Combo]
FROM #cohort AS cohort
INNER JOIN MinDrug ON cohort.PatientSID = mindrug.PatientSID
ORDER BY
cohort.PatientSID
The above gives this output. As you can see person 16051414 has a blank under both Glipizide and Metformin.
[tt]
PatientSID Glipizide Metformin Glip/Met Combo
16051414
16066127 YES
16067974 YES
16157440 YES
25397863 YES
[/tt]
Thanks for any suggestions.
You don't know what you don't know...