MS Access 2010 Running Total in Query

帅比萌擦擦* 提交于 2019-11-28 13:06:54

You can get what you want with a correlated subquery.

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;

You could also do it with DSum, but then you need to use delimiters with agentID and incurrredDate in the DSum WhereCondition option. It seems like more effort, and I found it more error-prone, than the subquery approach.

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;

Both queries return your requested results using your sample data in Access 2007.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!