在一行中显示来自相同联接 table 的不同值

Show different values from the same joined table on one row

每位患者都有两名医生(DOC_A 和 DOC_B)。有 3 个 table。患者 table 有 Patient_ID 和 DOC_A 的 DOC_ID。 CONSULT table 有 Patient_ID 和 DOC_B 的 DOC_ID。最后,医生 table 有 DOC_A 和 DOC_B 的 DOC_ID 以及他们的名字和姓氏。

我想在每一行上显示不同的 Patient_ID,其中 DOC_A 和 DOC_B 的名字和姓氏(串联)而不是 DOC_ID .

示例输出(手动创建):

Patient_ID DOC_A FirstLast DOC_B FirstLast
123 John Smith Jane Smith
456 Nathaniel Hawkeye Cora Munroe

如果您将 PATIENT 和 CONSULT table 视为 1 table(因为它们共享一个 Patient_ID),那么想象与 DOCTOR 的连接会更容易一些table...您必须加入两次,只要您使用别名即可:

select p.patient_id
     , concat(doca.firstname,' ',doca.lastname) as DOC_A_FirstLast
     , concat(docb.firstname,' ',docb.lastname) as DOC_B_FirstLast
  from dbo.PATIENT p
  join dbo.CONSULT c
    on c.patient_id = p.patient_id
  join dbo.DOCTOR doca
    on doca.doc_id = p.doc_id
  join dbo.DOCTOR docb
    on docb.doc_id = c.doc_id;