我对开发相当陌生,如果我的问题很愚蠢,很抱歉。我已经将许多列连接在一起,并且它在一对多关系表之间也有连接。无论如何,我现在得到的输出是
Patient_Name episode_id DOB primary_insu sec_insur patient_id
name, 001 03-29-1956 MEDICAID NULL 12
name, 001 03-29-1956 NULL STATEPROB 12
name2 001 02-20-1981 AETNA NULL 13
name2 001 02-20-1981 NULL MEDICAID 13
有一个表,其中有 2 条保险记录,即患者 ID 的主要保险和次要保险。有什么方法可以为每个患者 ID 在一行中显示它。
当前查询...
SELECT LTRIM(RTRIM(isnull(pat.LNAME, ''))) + ', ' + LTRIM(RTRIM(isnull(pat.FNAME, ''))) + ', ' + LTRIM(RTRIM(isnull(pat.MNAME, ''))) AS Patient_Name,
pat.episode_id,
CONVERT (VARCHAR (11), pat.dob, 110) AS DOB,
CASE
WHEN covh.copay_priority = '1' THEN covp.payor_ID ELSE NULL
END AS primary_insu,
CASE
WHEN covh.copay_priority = '2' THEN covp.payor_ID
END AS sec_insur
FROM Patient AS pat WITH (NOLOCK)
INNER JOIN
coverage_history AS covh WITH (NOLOCK)
ON pat.patient_id = covh.patient_id
AND pat.episode_id = covh.episode_id
INNER JOIN
coverage AS cov WITH (NOLOCK)
ON cov.patient_id = covh.patient_id
AND cov.episode_id = covh.episode_id
AND cov.hosp_status_code = covh.hosp_status_code
AND cov.coverage_plan_id = covh.coverage_plan_id
LEFT OUTER JOIN
coverage_plan AS covp WITH (NOLOCK)
ON covp.coverage_plan_id = covh.coverage_plan_id
AND covp.hosp_status_code = covh.hosp_status_code
WHERE covh.hosp_status_code = 'op'
AND (covh.effective_to IS NULL
OR covh.effective_to > GETDATE());