简体   繁体   English

在同一张表上创建更新总计查询困难

[英]Difficulty in creating update totals query on same table

Consider the following table: 请考虑下表:

ID nonUniqueID value total
--------------------------
1  12345        5     x
2  12345        10    x
3  789          20    x
4  789          5     x

I need to make a query something like this (psuedo SQL), which will work within Access 2007: 我需要进行如下查询(伪SQL),该查询将在Access 2007中运行:

UPDATE table 
SET total = SUM(value) 
WHERE nonUniqueID IS SAME;

The result should be as follows: 结果应如下所示:

ID nonUniqueID value total
--------------------------
1  12345        5     15
2  12345        10    15
3  789          20    25
4  789          5     25

I've tried group bys, but I got odd results that quite frankly, I could not interpret. 我已经尝试了分组方式,但是坦率地说,我得到的结果很奇怪,我无法解释。 Does anybody know how I could achieve something like this? 有人知道我能实现这样的目标吗?

Not sure if this works in Access or not, but give it a try: 不知道这是否可以在Access中工作,请尝试一下:

update table t1
inner join (
    select nonUniqueID, sum(value) as SumValue
    from table
    group by nonUniqueID 
) t2 on t1.nonUniqueID = t2.nonUniqueID
set t1.total = t2.SumValue

Update: Based on this question , it looks like it is not going to work. 更新:基于此问题 ,看来它无法正常工作。 But give it a shot! 但是试一试! If it doesn't, you can use the approach suggested in that question. 如果不是,则可以使用该问题中建议的方法。

Another possible option: 另一个可能的选择:

update t 
set total = (select SUM(value) from table where nonUniqueID = t.nonUniqueID)
from table t

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

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