在一个 table 内用另一行更新一行
Update a row by another row within one table
我使用的是 Access 2016,我有一个 table 缺少一些数据。我需要根据相同 table 的其他行更新丢失的数据。 table 有两个 Identifier 和两个 Criteria 字段,Null 字段表示缺失的数据。幸运的是,数据仅在 Column Criteria2 和 Criteria1 不为 NULL 时丢失:
Identifyer1 Identifyer2 Criteria1 Criteria2
10 a A3
10 a X NULL
20 b B3
30 c C3
40 d D3
40 d Y NULL
要填充缺失的数据,复制相同标识符的 Criteria2 值就足够了。所以结果应该是这样的:
Identifyer1 Identifyer2 Criteria1 Criteria2
10 a A3
10 a X A3
20 b B3
30 c C3
40 d D3
40 d Y D3
我试过类似的方法,但我无法完成代码:
Update table1 Set Criteria2 = (
如何在此处复制值?) Where Criteria1 is not NULL AND Identifyer1 = Identifyer1 AND Identifyer2 = Identifyer2
试试这个
Update table1
Set Criteria2 = (
select max(criteria2) from table1 t1
where t1.identifier1 = table1.identifier1
and t1.identifier2 = table1.identifier2
and t1.criteria2 is not null
)
Where Criteria1 is not NULL
由于在 MS Access 中更新查询必须 updateable,请考虑域聚合 DMax
,它对应于相关子查询:
UPDATE table1 t1
SET Criteria2 = DMax("Criteria2", "table1",
"Criteria1 IS NOT NULL AND Identifyer1 ='" & t1.Identifyer1 & "'
AND Identifyer2 = '" & t1.Identifyer2 & "'")
我使用的是 Access 2016,我有一个 table 缺少一些数据。我需要根据相同 table 的其他行更新丢失的数据。 table 有两个 Identifier 和两个 Criteria 字段,Null 字段表示缺失的数据。幸运的是,数据仅在 Column Criteria2 和 Criteria1 不为 NULL 时丢失:
Identifyer1 Identifyer2 Criteria1 Criteria2
10 a A3
10 a X NULL
20 b B3
30 c C3
40 d D3
40 d Y NULL
要填充缺失的数据,复制相同标识符的 Criteria2 值就足够了。所以结果应该是这样的:
Identifyer1 Identifyer2 Criteria1 Criteria2
10 a A3
10 a X A3
20 b B3
30 c C3
40 d D3
40 d Y D3
我试过类似的方法,但我无法完成代码:
Update table1 Set Criteria2 = (
如何在此处复制值?) Where Criteria1 is not NULL AND Identifyer1 = Identifyer1 AND Identifyer2 = Identifyer2
试试这个
Update table1
Set Criteria2 = (
select max(criteria2) from table1 t1
where t1.identifier1 = table1.identifier1
and t1.identifier2 = table1.identifier2
and t1.criteria2 is not null
)
Where Criteria1 is not NULL
由于在 MS Access 中更新查询必须 updateable,请考虑域聚合 DMax
,它对应于相关子查询:
UPDATE table1 t1
SET Criteria2 = DMax("Criteria2", "table1",
"Criteria1 IS NOT NULL AND Identifyer1 ='" & t1.Identifyer1 & "'
AND Identifyer2 = '" & t1.Identifyer2 & "'")