第一篇文章,已经广泛搜索了一个月的答案,并认为我只会问专家。
我填写了一张表,其中包含在医院接受过服务的患者帐户。我正在列出列出执行医师的每个程序的专栏。这些列的值是序列号,它指向第二个表中的一组列。第二个表中的列实际上包含我需要的医生标识符。
示例:
表1
Account: Phys_Proc1 Phys_Proc2 PhysProc3 PhysProc4 PhysProc5`
Patient1 2 5 1 4 5
Patient2 1 3 3 4 0
Patient3 2 0 0 0 0
表2
Account: Physician1 Physician2 Physician3 Physician4 Physician5
Patient1 500123 500456 500789 600123 600456
Patient2 400321 500700 300876 456789 987654
Patient3 300500 800700 0 0 0
我需要使用表2中的值更新表1中的记录,其中表1中的值引用表2中的列名。
示例
Patient1的过程1由'500456'执行(Phys_Proc1的值为“2”,表示表2中的Physician2字段。)
非常感谢任何帮助。即使是这方面的暗示也会给我一个方向来查看。指向一个特定的函数名称来搜索比我现在更好(没有。)我尝试了一个广泛的CASE语句,但它没有提取值每个患者帐户并从表2中提取第一个帐户的值并将其应用于所有患者记录。
答案 0 :(得分:1)
UPDATE Table2
SET
Physc_Proc1 =
CASE WHEN Phys_Proc1 = 1 Then Table2.Physician1
CASE WHEN Phys_Proc1 = 2 Then Table2.Physician2
CASE WHEN Phys_Proc1 = 3 Then Table2.Physician3
CASE WHEN Phys_Proc1 = 4 Then Table2.Physician4
CASE WHEN Phys_Proc1 = 5 Then Table2.Physician5
ELSE NULL
END,
Physc_Proc2 =
CASE WHEN Phys_Proc2 = 1 Then Table2.Physician1
CASE WHEN Phys_Proc2 = 2 Then Table2.Physician2
CASE WHEN Phys_Proc2 = 3 Then Table2.Physician3
CASE WHEN Phys_Proc2 = 4 Then Table2.Physician4
CASE WHEN Phys_Proc2 = 5 Then Table2.Physician5
ELSE NULL
END,
PhyscProc3 =
CASE WHEN PhysProc3 = 1 Then Table2.Physician1
CASE WHEN PhysProc3 = 2 Then Table2.Physician2
CASE WHEN PhysProc3 = 3 Then Table2.Physician3
CASE WHEN PhysProc3 = 4 Then Table2.Physician4
CASE WHEN PhysProc3 = 5 Then Table2.Physician5
ELSE NULL
END,
PhyscProc4 =
CASE WHEN PhysProc4 = 1 Then Table2.Physician1
CASE WHEN PhysProc4 = 2 Then Table2.Physician2
CASE WHEN PhysProc4 = 3 Then Table2.Physician3
CASE WHEN PhysProc4 = 4 Then Table2.Physician4
CASE WHEN PhysProc4 = 5 Then Table2.Physician5
ELSE NULL
END,
PhyscProc5 =
CASE WHEN PhysProc5 = 1 Then Table2.Physician1
CASE WHEN PhysProc5 = 2 Then Table2.Physician2
CASE WHEN PhysProc5 = 3 Then Table2.Physician3
CASE WHEN PhysProc5 = 4 Then Table2.Physician4
CASE WHEN PhysProc5 = 5 Then Table2.Physician5
ELSE NULL
END
FROM Table2
INNER JOIN Table1
ON Table2.Account = Table1.Account
注意:我没有尝试过这种语法。我希望这可以让你了解如何继续前进。