MS-Access select 小于当前值匹配条件的最大值
MS-Access select the greatest value less than current value matching criteria
我的情况是我有一个 table 简化后看起来像这样:
Sort| IsHeader
1 | 0
2 | 0
3 | 1
4 | 0
5 | 0
6 | 1
7 | 0
8 | 0
9 | 0
我正在尝试进行查询,该查询将为每个详细信息行更新一个新的 HeaderSort 行以显示该行属于哪个 header,最终结果为:
Sort| IsHeader| HeaderSort
1 | 0 | 0
2 | 0 | 0
3 | 1 | 3
4 | 0 | 3
5 | 0 | 3
6 | 1 | 6
7 | 0 | 6
8 | 0 | 6
9 | 0 | 6
在T-SQL我可以很容易地做到:
update MT set HeaderSort =
(
select isnull(max(Sort),0) from MyTable where Sort <= MT.Sort and IsHeader = 1
) as HeaderSort
from MyTable MT
我尝试的访问版本如下:
Update MyTable set HeaderSort =
(
select nz(max(Sort),0) from MyTable where Sort <= MT.Sort and IsHeader = 1
)
然而,这给出了 "Operation must use an updatable query." 错误。
是我获取值的逻辑错误,还是只是一些 ms-access 语法?
操作必须使用可更新查询。 是更新语句中引用的并非所有表都可更新时遇到的一般错误。不幸的是,子查询很少。
您可以使用 DLookup
而不是子查询来解决错误:
Update MyTable set HeaderSort = DLookUp("nz(max(Sort),0)", "MyTable", "Sort <= " & MyTable.Sort & " and IsHeader = 1")
我的情况是我有一个 table 简化后看起来像这样:
Sort| IsHeader
1 | 0
2 | 0
3 | 1
4 | 0
5 | 0
6 | 1
7 | 0
8 | 0
9 | 0
我正在尝试进行查询,该查询将为每个详细信息行更新一个新的 HeaderSort 行以显示该行属于哪个 header,最终结果为:
Sort| IsHeader| HeaderSort
1 | 0 | 0
2 | 0 | 0
3 | 1 | 3
4 | 0 | 3
5 | 0 | 3
6 | 1 | 6
7 | 0 | 6
8 | 0 | 6
9 | 0 | 6
在T-SQL我可以很容易地做到:
update MT set HeaderSort =
(
select isnull(max(Sort),0) from MyTable where Sort <= MT.Sort and IsHeader = 1
) as HeaderSort
from MyTable MT
我尝试的访问版本如下:
Update MyTable set HeaderSort =
(
select nz(max(Sort),0) from MyTable where Sort <= MT.Sort and IsHeader = 1
)
然而,这给出了 "Operation must use an updatable query." 错误。 是我获取值的逻辑错误,还是只是一些 ms-access 语法?
操作必须使用可更新查询。 是更新语句中引用的并非所有表都可更新时遇到的一般错误。不幸的是,子查询很少。
您可以使用 DLookup
而不是子查询来解决错误:
Update MyTable set HeaderSort = DLookUp("nz(max(Sort),0)", "MyTable", "Sort <= " & MyTable.Sort & " and IsHeader = 1")