I am using Server 2012 and new to SQL. I have a request from a physician for a list of his patients that meet a criteria. This is stored in a temp table I named #cohort. It only contains PatientSID (85 records).
I want one row per patient with a list of about 10+ labs, vitals, etc as columns. Three items are the most recent labs with value and date. All labs are stored in the same table. I could query each lab individually and place it into a temp table and then join all temp tables at the end, but I am trying to move past that and have all labs in one temp table (for now). All temp tables are joined with PatientSID to build the final table for export.
I tried to do something for just 2 labs, but it is not working. There could be nulls values for labs when joined with the #cohort table.
Individually the SELECT statements pull in the most recent lab value and date, but I cannot get them into a temp table with one row of PatientSID and then the lab value and date if they exist.
Any help would be appreciated. Thanks. Since I have other queries that are very similar, I am open to how to do this more efficiently.
Here is a convoluted attempt at 2 labs. The question: For each patient in #cohort list the most recent A1c% and LDL values with their respective dates, if they exist.
You don't know what you don't know...
I want one row per patient with a list of about 10+ labs, vitals, etc as columns. Three items are the most recent labs with value and date. All labs are stored in the same table. I could query each lab individually and place it into a temp table and then join all temp tables at the end, but I am trying to move past that and have all labs in one temp table (for now). All temp tables are joined with PatientSID to build the final table for export.
I tried to do something for just 2 labs, but it is not working. There could be nulls values for labs when joined with the #cohort table.
Individually the SELECT statements pull in the most recent lab value and date, but I cannot get them into a temp table with one row of PatientSID and then the lab value and date if they exist.
Any help would be appreciated. Thanks. Since I have other queries that are very similar, I am open to how to do this more efficiently.
Here is a convoluted attempt at 2 labs. The question: For each patient in #cohort list the most recent A1c% and LDL values with their respective dates, if they exist.
SQL:
IF OBJECT_ID ('TEMPDB..#lab') IS NOT NULL DROP TABLE #lab
SELECT
cohort.PatientSID
,SubQuery1.LabChemResultNumericValue AS 'A1c%'
,SubQuery1.LabChemCompleteDateTime AS 'A1c% Date'
,SubQuery2.LabChemResultNumericValue AS 'LDL'
,SubQuery2.LabChemCompleteDateTime AS 'LDL Date'
INTO #lab
FROM
#cohort AS cohort
INNER JOIN SubQuery1 ON cohort.PatientSID = SubQuery1.PatientSID
INNER JOIN SubQuery2 ON cohort.PatientSID = SubQuery2.PatientSID
(SELECT
S1.PatientSID
,s1.LabChemResultNumericValue AS 'A1c%'
,s1.LabChemCompleteDateTime AS 'A1c% Date'
FROM
(SELECT
lab1.PatientSID
,lab1.LabChemResultNumericValue
,lab1.LabChemCompleteDateTime
,RANK() OVER (PARTITION BY lab1.PatientSID ORDER BY lab1.LabChemCompleteDateTime DESC) AS Lab1Rank
FROM
#cohort AS cohort3
INNER JOIN Chem.PatientLabChem AS lab1 ON cohort3.PatientSID = lab1.PatientSID
WHERE
lab1.LabChemCompleteDateTime BETWEEN (GETDATE() - 365) AND GETDATE()
AND lab1.LabChemTestSID = 1000093199) AS S1 -- Lab test = A1c%
WHERE
Lab1Rank = 1) AS SubQuery1
,
(SELECT
S2.PatientSID
,S2.LabChemResultNumericValue AS 'LDL'
,S2.LabChemCompleteDateTime AS 'LDL Date'
FROM
(SELECT
lab2.PatientSID
,lab2.LabChemTestSID
,lab2.LabChemResultNumericValue
,lab2.LabChemCompleteDateTime
,RANK() OVER (PARTITION BY lab2.PatientSID ORDER BY lab2.LabChemCompleteDateTime DESC) AS Lab2Rank
FROM
#cohort AS cohort4
INNER JOIN Chem.PatientLabChem AS lab2 ON cohort4.PatientSID = lab2.PatientSID
WHERE
lab2.LabChemCompleteDateTime BETWEEN (GETDATE() - 365) AND GETDATE()
AND lab2.LabChemTestSID = 1000087471) AS S2 --Lab test = LDL MEASURED
WHERE
Lab2Rank = 1) AS SubQuery2
You don't know what you don't know...