我不是Access的忠实拥护者,但是对于这个项目,我被要求创建一个Access数据库。我为其他数据库类型创建了类似的东西,所以并不太困难。我大部分都想通了,但总的运行量让我大吃一惊。
我有下表:表名 attendanceView
attendanceView
询问: SELECT * FROM attendanceView
SELECT * FROM attendanceView
====================================================================================== agentID | incurrredDate | points | OneFallOff | TwoFallOff ====================================================================================== chtall | 10/7/2013 | 2 | 2 | 2 chtall | 10/15/2013 | 2 | 2 | 2 chtall | 11/26/2013 | 2 | 2 | 2 chtall | 12/17/2013 | 2 | 2 | 2 vimunson | 7/22/2013 | 2 | 2 | 2 vimunson | 7/29/2013 | 2 | 1 | 1 vimunson | 12/6/2013 | 1 | 1 | 1
这个查询做的事情需要做,找到值OneFallOff和TwoFallOff。但是我需要找到一种方法来TwoFallOff为每个运行总计agentID。例如,chtall有四个记录,如下所示:
OneFallOff
TwoFallOff
agentID
chtall
================================================================================================== agentID | incurrredDate | points | OneFallOff | TwoFallOff | total ================================================================================================== chtall | 10/7/2013 | 2 | 2 | 2 | 2 chtall | 10/15/2013 | 2 | 2 | 2 | 4 chtall | 11/26/2013 | 2 | 2 | 2 | 6 chtall | 12/17/2013 | 2 | 2 | 2 | 8 vimunson | 7/22/2013 | 2 | 2 | 2 | 2 vimunson | 7/29/2013 | 2 | 1 | 1 | 3 vimunson | 12/6/2013 | 1 | 1 | 1 | 4
我尝试了DSUM()哪些无效的方法,或者我使用的方法有误。如果表格中的内容不明确,则total当代理更改时,该列将重置为0。
DSUM()
total
您可以通过相关子查询获得所需的内容。
SELECT a1.agentID, a1.incurrredDate, a1.points, a1.OneFallOff, a1.TwoFallOff ( SELECT Sum(a2.TwoFallOff) FROM attendanceView AS a2 WHERE a2.agentID = a1.agentID AND a2.incurrredDate <= a1.incurrredDate ) AS total FROM attendanceView AS a1;
您也可以使用DSum,但随后需要在 WhereCondition 选项中使用agentID和分隔符。与子查询方法相比,这似乎需要更多的精力,而且我发现它更容易出错。incurrredDate``DSum __
DSum
incurrredDate``DSum
SELECT a.agentID, a.incurrredDate, a.points, a.OneFallOff, a.TwoFallOff, DSum ( "TwoFallOff", "attendanceView", "agentID = '" & a.agentID & "' " & "AND incurrredDate <= " & Format(a.incurrredDate, "\#yyyy-m-d\#") ) AS total FROM attendanceView AS a;
这两个查询都使用Access 2007中的示例数据返回您请求的结果。