繁体   English   中英

使用Count使用qry中的值并更新到Access 2007中的表

[英]Use value from qry using Count and update to a table in Access 2007

我有一个查询,它计算每个子集的记录数:

SELECT tbl_Interpolation.Unit, tbl_Interpolation.Car, tbl_Interpolation.Pad, 
Nz(Count(tbl_Interpolation.CalenderDate),0) AS NumRecords
FROM tbl_Interpolation
GROUP BY tbl_Interpolation.Unit, tbl_Interpolation.Car, tbl_Interpolation.Pad;

我在tbl_CalcReg中还有一个名为NumRecords的字段,该字段与上面的查询具有相同的单位,汽车和脚垫,因此在这些变量相同的地方,例如tbl_CalcReg.Pad = qry_NumRecords.pad我想更新NumRecords字段。

同样,我在聚合函数方面遇到问题,我看过DCount(),但是我似乎找不到不使用标准的方法来实现它,因为我只想计算每个Unit / Car /有多少个压延日期。垫组合。 我到了DCOUNT(“ CalenderDate”,“ tbl_Interpolation”,“ *”),但我真的不知道如何将其转换为更新查询?

请帮忙。

最好的问候唐娜

您确实可以DCount()与这样的查询一起使用

UPDATE tbl_CalcReg SET NumRecords = 
DCount("CalenderDate", "tbl_Interpolation", "Unit=" & Unit & " AND Car=" & Car & " AND Pad=" & Pad)

假设[Unit],[Car]和[Pad]是数字字段。 如果它们是文本字段,则需要在值周围加上引号( ' ),并在值本身中转义任何引号。

如果我理解正确,则可以使用相关子查询来执行此操作。 下面对所有记录进行更新:

update tbl_CalcReg
    set NumRecords = (select count(*)
                      from tbl_Interpolation as i
                      where i.unit = tbl_CalcReg.unit and i.car = tbl_CalcReg.car and
                            i.pad = tbl_CalcReg.pad
                     )

如果只想更改记录where则可以添加以下where子句:

where NumRecords <> (select count(*)
                     from tbl_Interpolation as i
                     where i.unit = tbl_CalcReg.unit and i.car = tbl_CalcReg.car and
                           i.pad = tbl_CalcReg.pad
                    )

编辑:

MS Access支持UPDATE / JOIN 在这种情况下:

update tbl_CalcReg JOIN
       (select unit, car, pad, count(*) as cnt
        from tbl_Interpolation
        group by unit, car, pad
       ) as i
       on i.unit = tbl_CalcReg.unit and i.car = tbl_CalcReg.car and i.pad = tbl_CalcReg.pad
    set NumRecords = i.cnt

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM