これが私の問題です。複数の結合を持つサブクエリがあります (テーブルの 1 つに person_id がありません)。このデータから作成されたピボット テーブルがあり、テスト データの 6 行の結果を受け取ります。問題は、2 つの行が必要なことです.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行目だけを返したいですか?何か案は?