我处理患者数据。当加入表格以显示哪个clician看到患者时,如果患者看到多个临床医生,我会得到多行。我使用了行号功能来尝试突出显示第二个临床医生。但是,我需要每行代表一名患者和其他专栏,以显示临床医生的姓名(如果有多名)。
我的查询中有以下数据输出
patient Clinician seen Clinician number
1 joe 1
2 dan 1
2 peter 2
3 sandra 1
4 andrea 1
4 steve 2
5 helen 1
我需要输出:
patient Clinician seen 1 person seen 2
1 joe NULL
2 dan peter
3 sandra NULL
4 andrea steve
5 helen NULL
这是我的问题:
SELECT
patient,
Clinician_seen,
,ROW_NUMBER ( ) OVER(PARTITION BY patient ORDER BY Clinician_seen asc )
FROM
patients
clinicians on patients.patietn_id = clinicians.patietn_id
答案 0 :(得分:0)
您可以使用条件聚合:
SELECT patient,
MAX(case when cnum = 1 then Clinician_seen end) as Clinician1,
MAX(case when cnum = 2 then Clinician_seen end) as Clinician2
FROM (SELECT p.patient, c.Clinician_seen,
ROW_NUMBER ( ) OVER (PARTITION BY p.patient ORDER BY c.Clinician_seen asc ) as cnum
FROM patients p JOIN
clinicians c
on p.patient_id = c.patient_id
) t
GROUP BY patient;