Here is my issue. I have a sub query that has several joins (one of the tables do not have a person_id) I have a pivot table created off this data and receive the results of 6 rows for my test data. The problem is I need 2 rows.. the two unique person_id's with the most recent lab data. Here is my Query so far:
SELECT person_id, first_name,
middle_name, last_name, address_line_1,
address_line_2, city, state, zip,
date_of_birth, sex, enc_id, ClinicalComments,
RecheckMonths, SignedName, [CHOLESTEROL],[HDL CHOLESTEROL],
[LDL CHOLESTEROL,CALCULATED], [TRIGLYCERIDES], coll_date_time
from
SELECT person_id, first_name,
middle_name, last_name, address_line_1,
address_line_2, city, state, zip,
date_of_birth, sex, enc_id, ClinicalComments,
RecheckMonths, SignedName, [CHOLESTEROL],[HDL CHOLESTEROL],
[LDL CHOLESTEROL,CALCULATED], [TRIGLYCERIDES], coll_date_time
from
(SELECT p.person_id as person_id,
p.city as city,
p.first_name as first_name,
p.middle_name as middle_name,
p.last_name as last_name,
p.address_line_1 as address_line_1,
p.address_line_2 as address_line_2,
p.state as state,
p.zip as zip,
p.date_of_birth as date_of_birth,
p.sex as sex,
cl.enc_id as enc_id,
cl.ClinicalComments as ClinicalComments,
cl.RecheckMonths as RecheckMonths,
cl.SignedName as SignedName,
lab.result_desc as result_desc,
lab.observ_value as observ_value,
lab_results_obr_p.coll_date_time as coll_date_time
FROM person p
LEFT OUTER JOIN card_lipid_ cl ON p.person_id = cl.person_id
left outer join CANEA_CARD_LIPIDS_ ccl on cl.enc_id=ccl.enc_id
left outer join lab_results_obx lab on ccl.person_id = lab.person_id
left outer join lab_results_obr_p on lab.unique_obr_num = lab_results_obr_p.unique_obr_num
WHERE ccl.LIPIDS='1'
AND lab.delete_ind='N'
AND lab.result_desc in ('CHOLESTEROL','HDL CHOLESTEROL','LDL CHOLESTEROL,CALCULATED','TRIGLYCERIDES')) SourceTable
PIVOT
(max(observ_value) for result_desc in ([CHOLESTEROL],[HDL CHOLESTEROL],[LDL CHOLESTEROL,CALCULATED],[TRIGLYCERIDES])) AS PivotTable
This gives results of
ClinicalComments RecheckMonths SignedName CHOLESTEROL HDL CHOLESTEROL LDL CHOLESTEROL;CALCULATED TRIGLYCERIDES coll_date_time
None 2 Dr Singer 112 35 52.2 126 10/26/2010 11:08
None 1 Dr Singer 106 41 42 114 1/11/2011 10:41
None 0 Dr Singer 112 3 4/26/2011 12:00
None 1 Dr Singer 96 37 48 58 10/8/2012 9:45
None 1 Dr Singer 103 48 36 109 8/30/2011 12:00
None 1 Dr Singer 102 41 37 120 2/14/2012 11:20
I want it to return lines 4 and 6 only? any ideas?
There are a few ways that I can see that you return the rows with the most recent date.
First way, you can wrap the entire query in a select so you apply a row_number()
and partition by the name, ordering by date:
select person_id, first_name,
middle_name, last_name, address_line_1,
address_line_2, city, state, zip,
date_of_birth, sex, enc_id, ClinicalComments,
RecheckMonths, SignedName, [CHOLESTEROL],[HDL CHOLESTEROL],
[LDL CHOLESTEROL,CALCULATED], [TRIGLYCERIDES], coll_date_time
from
(
SELECT person_id, first_name,
middle_name, last_name, address_line_1,
address_line_2, city, state, zip,
date_of_birth, sex, enc_id, ClinicalComments,
RecheckMonths, SignedName, [CHOLESTEROL],[HDL CHOLESTEROL],
[LDL CHOLESTEROL,CALCULATED], [TRIGLYCERIDES], coll_date_time,
ROW_NUMBER() over(partition by person_id order by coll_date_time desc) rn
from
(
SELECT p.person_id as person_id,
p.city as city,
p.first_name as first_name,
p.middle_name as middle_name,
p.last_name as last_name,
p.address_line_1 as address_line_1,
p.address_line_2 as address_line_2,
p.state as state,
p.zip as zip,
p.date_of_birth as date_of_birth,
p.sex as sex,
cl.enc_id as enc_id,
cl.ClinicalComments as ClinicalComments,
cl.RecheckMonths as RecheckMonths,
cl.SignedName as SignedName,
lab.result_desc as result_desc,
lab.observ_value as observ_value,
lab_results_obr_p.coll_date_time as coll_date_time
FROM person p
LEFT OUTER JOIN card_lipid_ cl
ON p.person_id = cl.person_id
left outer join CANEA_CARD_LIPIDS_ ccl
on cl.enc_id=ccl.enc_id
left outer join lab_results_obx lab
on ccl.person_id = lab.person_id
left outer join lab_results_obr_p
on lab.unique_obr_num = lab_results_obr_p.unique_obr_num
WHERE ccl.LIPIDS='1'
AND lab.delete_ind='N'
AND lab.result_desc in ('CHOLESTEROL','HDL CHOLESTEROL','LDL CHOLESTEROL,CALCULATED','TRIGLYCERIDES')
) SourceTable
PIVOT
(
max(observ_value)
for result_desc in ([CHOLESTEROL],[HDL CHOLESTEROL],[LDL CHOLESTEROL,CALCULATED],[TRIGLYCERIDES])
) AS PivotTable
) src
where rn = 1;
Or you could select the max(coll_date_time0
in your subquery, then the most recent value is returned:
SELECT person_id, first_name,
middle_name, last_name, address_line_1,
address_line_2, city, state, zip,
date_of_birth, sex, enc_id, ClinicalComments,
RecheckMonths, SignedName, [CHOLESTEROL],[HDL CHOLESTEROL],
[LDL CHOLESTEROL,CALCULATED], [TRIGLYCERIDES], coll_date_time
from
(
SELECT p.person_id as person_id,
p.city as city,
p.first_name as first_name,
p.middle_name as middle_name,
p.last_name as last_name,
p.address_line_1 as address_line_1,
p.address_line_2 as address_line_2,
p.state as state,
p.zip as zip,
p.date_of_birth as date_of_birth,
p.sex as sex,
cl.enc_id as enc_id,
cl.ClinicalComments as ClinicalComments,
cl.RecheckMonths as RecheckMonths,
cl.SignedName as SignedName,
lab.result_desc as result_desc,
lab.observ_value as observ_value,
lab_results_obr_p.coll_date_time as coll_date_time
FROM person p
LEFT OUTER JOIN card_lipid_ cl
ON p.person_id = cl.person_id
left outer join CANEA_CARD_LIPIDS_ ccl
on cl.enc_id=ccl.enc_id
left outer join lab_results_obx lab
on ccl.person_id = lab.person_id
left outer join
(
select MAX(coll_date_time) coll_date_time,
unique_obr_num
from lab_results_obr_p
group by unique_obr_num
) lab_results_obr_p
on lab.unique_obr_num = lab_results_obr_p.unique_obr_num
WHERE ccl.LIPIDS='1'
AND lab.delete_ind='N'
AND lab.result_desc in ('CHOLESTEROL','HDL CHOLESTEROL','LDL CHOLESTEROL,CALCULATED','TRIGLYCERIDES')
) SourceTable
PIVOT
(
max(observ_value)
for result_desc in ([CHOLESTEROL],[HDL CHOLESTEROL],[LDL CHOLESTEROL,CALCULATED],[TRIGLYCERIDES])
) AS PivotTable
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.