我如何在访问中编写查询以根据其他 2 个列的值更新列?
how do i write a query in access to update a column based on the values of 2 other columns?
我正在使用访问权限来更新 table(不是自愿的)。 table 看起来像这样:
我想用
填充最后一列 ("regulation 102 or Regulation 103")
- "Reg 102 only" 如果每个检验 ID 的 "regulation for non-compliance" 列中仅出现 Reg 102 不合规情况,
- "Reg 103 only" 如果每个检查 ID
的 "regulation for non-compliance" 列中仅出现 Reg 103 不合规情况
- "Reg 102 and 103" 如果两者都出现。
所以在上图中,有 3 个检查——第一个检查 (1-JHHK9) 将仅是 Reg 102,第二个/第三个 (1-JJVAN, 1-JJVFR) 将是 Reg 102 和 103 .
您可以使用条件聚合和 join
:
select t.*,
switch(cnt_102 > 0 and cnt_103 > 0, "Reg 102 and 103",
cnt_102 > 0, "Reg 102 only",
cnt_103 > 0, "Reg 103 only",
1=1, "Neither"
) as reg_102_or_103
from t left join
(select t.[inspection id],
sum(iif([regulation for non-compliance] like "*Reg102*", 1, 0)) as cnt_102,
sum(iif([regulation for non-compliance] like "*Reg102*", 1, 0)) as cnt_103
from t
group by [inspection id]
) as tt
on t.[inspection id] = tt.[inspection id]
我正在使用访问权限来更新 table(不是自愿的)。 table 看起来像这样:
我想用
填充最后一列 ("regulation 102 or Regulation 103")- "Reg 102 only" 如果每个检验 ID 的 "regulation for non-compliance" 列中仅出现 Reg 102 不合规情况,
- "Reg 103 only" 如果每个检查 ID 的 "regulation for non-compliance" 列中仅出现 Reg 103 不合规情况
- "Reg 102 and 103" 如果两者都出现。
所以在上图中,有 3 个检查——第一个检查 (1-JHHK9) 将仅是 Reg 102,第二个/第三个 (1-JJVAN, 1-JJVFR) 将是 Reg 102 和 103 .
您可以使用条件聚合和 join
:
select t.*,
switch(cnt_102 > 0 and cnt_103 > 0, "Reg 102 and 103",
cnt_102 > 0, "Reg 102 only",
cnt_103 > 0, "Reg 103 only",
1=1, "Neither"
) as reg_102_or_103
from t left join
(select t.[inspection id],
sum(iif([regulation for non-compliance] like "*Reg102*", 1, 0)) as cnt_102,
sum(iif([regulation for non-compliance] like "*Reg102*", 1, 0)) as cnt_103
from t
group by [inspection id]
) as tt
on t.[inspection id] = tt.[inspection id]