这是我的问题。我有一个子查询,有几个连接(其中一个表没有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行?有任何想法吗?
答案 0 :(得分:0)
我可以通过几种方式看到您返回最近日期的行。
首先,您可以将整个查询包装在一个select中,以便按名称应用row_number()
和分区,按日期排序:
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;
或者您可以在子查询中选择max(coll_date_time0
,然后返回最新值:
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