我在Access数据库中运行SQL查询以累积每条记录后运行的距离。
我使用以下代码计算运行总计:
SELECT a.run, a.year, a.date, a.distance, round((SELECT SUM(b.distance)
FROM vwAllCast b
WHERE b.run <= a.run),1) AS AccDistance
FROM vwAllCast AS a
ORDER BY a.run DESC;
此代码返回总计AccDistance中所有距离的所有记录。现在,我想每年重新开始积累。有谁知道如何实现这一目标?
答案 0 :(得分:0)
SELECT a.run, a.year, a.date, a.distance, round((SELECT SUM(b.distance)
FROM vwAllCast b
WHERE b.run <= a.run and a.year = b.year),1) AS AccDistance
FROM vwAllCast AS a
ORDER BY a.run DESC;
答案 1 :(得分:0)
自联接+分组可能比相关子查询快
SELECT a.run, a.year, a.date, a.distance, SUM(b.distance)
FROM vwAllCast AS a
JOIN vwAllCast b on b.run <= a.run AND b.year = a.year
GROUP BY a.run, a.year, a.date, a.distance
ORDER BY a.run DESC