MS 访问用最近的非零行替换空值或零值
MS access replace null or zero values with nearest non-zero row
我有一个这样的示例数据集
Group Age Value
A 1
A 2 0
A 3 20
B 1 0
B 2 0
B 3 25
B 4 0
B 5 0
B 6 50
对于 Group
中的每个不同值,替换空值或零 Value
。
理想的输出将是
Group Age Value
A 1 20
A 2 20
A 3 20
B 1 25
B 2 25
B 3 25
B 4 25
B 5 50
B 6 50
更新
这似乎可以解决问题
UPDATE test
SET [Value] = nz(DMin(
"[Value]",
"test",
"[Group] = '" & [Group] & "' AND
[Age] < " & [Age] & " AND
[Value] <> 0 AND [Value] IS NOT NULL"
),
DMax(
"[Value]",
"test",
"[Group] = '" & [Group] & "' AND
[Age] > " & [Age] & " AND
[Value] <> 0 AND [Value] IS NOT NULL"
))
WHERE [Value] = 0 OR [Value] IS NULL;
您可以使用它(在 MsAccess SQL 中,可以从返回一个值或没有行的子查询派生一列):
select
tactl.[Group],
tactl.Age,
( select trepl.[Value]
from test trepl
where tactl.[Group]=trepl.[Group]
and nz(trepl.[Value],0)<>0
and abs(tactl.Age-trepl.Age)=
( select MIN(abs(ta.Age-tb.Age)) as MinDistanceFound
from test ta
inner join
test tb
on (ta.[Group]=tb.[Group] and nz(tb.[Value],0) <> 0)
where (ta.[Group]=tactl.[Group] and ta.[Age]=tactl.[Age])
)
) as [Value]
from test tactl
GROUP
和 VALUE
在大多数 SQL 实现中可以是保留字(它们在 Access 中);尝试用其他词来代替它们(我把它们放在括号中以克服问题)
考虑 DMin
等域聚合以获得可更新的查询:
UPDATE test
SET [Value] = DMin(
"[Value]",
"test",
"[Group] = '" & [Group] & "' AND
[Age] > " & [Age] & " AND
[Value] <> 0 AND [Value] IS NOT NULL"
)
WHERE [Value] = 0 OR [Value] IS NULL;
我有一个这样的示例数据集
Group Age Value
A 1
A 2 0
A 3 20
B 1 0
B 2 0
B 3 25
B 4 0
B 5 0
B 6 50
对于 Group
中的每个不同值,替换空值或零 Value
。
理想的输出将是
Group Age Value
A 1 20
A 2 20
A 3 20
B 1 25
B 2 25
B 3 25
B 4 25
B 5 50
B 6 50
更新
这似乎可以解决问题
UPDATE test
SET [Value] = nz(DMin(
"[Value]",
"test",
"[Group] = '" & [Group] & "' AND
[Age] < " & [Age] & " AND
[Value] <> 0 AND [Value] IS NOT NULL"
),
DMax(
"[Value]",
"test",
"[Group] = '" & [Group] & "' AND
[Age] > " & [Age] & " AND
[Value] <> 0 AND [Value] IS NOT NULL"
))
WHERE [Value] = 0 OR [Value] IS NULL;
您可以使用它(在 MsAccess SQL 中,可以从返回一个值或没有行的子查询派生一列):
select
tactl.[Group],
tactl.Age,
( select trepl.[Value]
from test trepl
where tactl.[Group]=trepl.[Group]
and nz(trepl.[Value],0)<>0
and abs(tactl.Age-trepl.Age)=
( select MIN(abs(ta.Age-tb.Age)) as MinDistanceFound
from test ta
inner join
test tb
on (ta.[Group]=tb.[Group] and nz(tb.[Value],0) <> 0)
where (ta.[Group]=tactl.[Group] and ta.[Age]=tactl.[Age])
)
) as [Value]
from test tactl
GROUP
和 VALUE
在大多数 SQL 实现中可以是保留字(它们在 Access 中);尝试用其他词来代替它们(我把它们放在括号中以克服问题)
考虑 DMin
等域聚合以获得可更新的查询:
UPDATE test
SET [Value] = DMin(
"[Value]",
"test",
"[Group] = '" & [Group] & "' AND
[Age] > " & [Age] & " AND
[Value] <> 0 AND [Value] IS NOT NULL"
)
WHERE [Value] = 0 OR [Value] IS NULL;