[英]SQL database, Need max date for pivot table
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.
我有一个具有几个联接的子查询(其中一个表没有person_id),我有一个根据数据创建的数据透视表,并接收了6行测试数据的结果。 The problem is I need 2 rows.. the two unique person_id's with the most recent lab data.
问题是我需要2行..具有最新实验室数据的两个唯一person_id。 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? 我只希望它返回第4行和第6行吗? 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中,以便您按名称应用
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;
Or you could select the max(coll_date_time0
in your subquery, then the most recent value is returned: 或者,您可以在子查询中选择
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.