Microsoft访问查询每5行不使用日期滚动总计

时间:2013-12-28 20:22:59

标签: sql ms-access running-total

我是微软访问的新手。

我需要一个查询,允许我为每5行数据总计滚动总数。所以在第六天我需要一条线来放下总线和新线。

Fields:
ID, Daily_SUM

结果应为

ID  Daily sum   Weekly Sum
1   12  
2   41  
3   46  
4   125 
5   120 344
6   42  374
7   41  374
8   57  385
9   207 467
10  215 562
11  187 707
12  -43 623
13  45  611
14  56  460
15  40  285
16  8   106
17  95  244
18  580 779
19  360 1083
20  337 1380

1 个答案:

答案 0 :(得分:0)

您可以使用相关子查询执行此操作。挑战实际上是在前几行获得NULL值:

select t.id, t.daily,
       (select iif(count(*) = 7, sum(t3.daily), NULL)
        from (select top 7 t2.daily
              from table t2
              where t2.id <= t.id
              order by t2.id desc
             ) t3
       ) as weekly
from table t;

编辑:

如果我们假设id是按顺序分配的,没有间隙,那么你可以使用显式连接:

select t.id, t.daily,
       iif(count(*) = 7, sum(t2.daily), NULL) as weekly
from table t inner join
     table t2
     on t2.id between t.id - 6 and t.id
group by t.id, t.daily;