INSERT INTO Family
(ID,
Name,
Age,
Affected,
Relationship)
SELECoT ExcelPatients.id, ExcelPatients.sibling1_name AS Name,
ExcelPatients.sibling1_age AS Age,
ExcelPatients.sibling1_affected AS Affected, "Sibling"
FROM ExcelPatients
WHERE (((ExcelPatients.Sibling1_name) Is Not Null))
AND NOT EXISTS (SELECT DISTINCT ID FROM Family where family.id = ExcelPatients.id and Family.name = ExcelPatients.sibling1_name)
UNION
SELECT ExcelPatients.id, ExcelPatients.sibling2_name AS Name,
ExcelPatients.sibling2_age AS Age,
ExcelPatients.sibling2_affected AS Affected, "Sibling"
FROM ExcelPatients
WHERE (((ExcelPatients.Sibling2_name) Is Not Null))
AND NOT EXISTS (SELECT DISTINCT ID FROM Family where family.id = ExcelPatients.id and Family.name = ExcelPatients.sibling2_name)
UNION
SELECT ExcelPatients.id, ExcelPatients.sibling3_name AS Name,
ExcelPatients.sibling3_age AS Age,
ExcelPatients.sibling3_affected AS Affected, "Sibling"
FROM ExcelPatients
WHERE (((ExcelPatients.Sibling3_name) Is Not Null))
AND NOT EXISTS (SELECT DISTINCT ID FROM Family where family.id = ExcelPatients.id and Family.name = ExcelPatients.sibling3_name)
UNION
SELECT ExcelPatients.id, ExcelPatients.sibling4_name AS Name,
ExcelPatients.sibling4_age AS Age,
ExcelPatients.sibling4_affected AS Affected, "Sibling"
FROM ExcelPatients
WHERE (((ExcelPatients.Sibling4_name) Is Not Null))
AND NOT EXISTS (SELECT DISTINCT ID FROM Family where family.id = ExcelPatients.id and Family.name = ExcelPatients.sibling4_name)
没有看到数据,我不知道 UNION ALL 或 UNION 是否是正确的选择。如果名称只能在 4 个兄弟列之一中,则使用 UNION ALL,如果可以重复,则使用 UNION。由于您正在清理来自另一个来源的数据,因此 UNION 可能是更安全但更慢的选择。NOT EXISTS 往往是 SQL Server 中最快的比较,这就是我选择它的原因。