简体   繁体   English

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

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

I have a query which counts the number of records for each subset: 我有一个查询,它计算每个子集的记录数:

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;

I also have a field in tbl_CalcReg called NumRecords which have the same unit, car and pad as the query above, so where these variables are the same eg tbl_CalcReg.Pad=qry_NumRecords.pad i would like to update the field NumRecords. 我在tbl_CalcReg中还有一个名为NumRecords的字段,该字段与上面的查询具有相同的单位,汽车和脚垫,因此在这些变量相同的地方,例如tbl_CalcReg.Pad = qry_NumRecords.pad我想更新NumRecords字段。

Again i am having an issue with the aggregate function, i have looked at DCount() but i cannot seem to find a way to implement it without using criteria, as i just want to count how many calenderdates there are for each Unit/Car/Pad combination. 同样,我在聚合函数方面遇到问题,我看过DCount(),但是我似乎找不到不使用标准的方法来实现它,因为我只想计算每个Unit / Car /有多少个压延日期。垫组合。 I got as far as DCOUNT("CalenderDate", "tbl_Interpolation", "*") but i really havent a clue as to how i can make this into an update query? 我到了DCOUNT(“ CalenderDate”,“ tbl_Interpolation”,“ *”),但我真的不知道如何将其转换为更新查询?

Please help. 请帮忙。

Best Regards Donna 最好的问候唐娜

You can indeed use DCount() with a query like this 您确实可以DCount()与这样的查询一起使用

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

assuming that [Unit], [Car], and [Pad] are numeric fields. 假设[Unit],[Car]和[Pad]是数字字段。 If they are text fields then you'll need to put quotes ( ' ) around the values and escape any quotes in the values themselves. 如果它们是文本字段,则需要在值周围加上引号( ' ),并在值本身中转义任何引号。

If I understand correctly, you can do this with a correlated subquery. 如果我理解正确,则可以使用相关子查询来执行此操作。 The following does the update for all records: 下面对所有记录进行更新:

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
                     )

You can add this where clause if you only want changed records: 如果只想更改记录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
                    )

EDIT: 编辑:

MS Access supports UPDATE / JOIN . MS Access支持UPDATE / JOIN In that case: 在这种情况下:

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