这是我的问题。我有一个包含多个连接的子查询(其中一个表没有 person_id) 我有一个基于此数据创建的数据透视表并接收我的测试数据的 6 行结果。问题是我需要 2 行.. 两个具有最新实验室数据的唯一 person_id。到目前为止,这是我的查询:
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
这给出了结果
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
我希望它只返回第 4 行和第 6 行吗?有任何想法吗?