我需要在我的Access查詢中添加「Running Total」列。這樣做的目的是確定在特定時間有多少個開放位置。在Access查詢中運行總計
有一個名爲「開放日期」的字段,我希望按升序排列。
在某些情況下,多個位置打開的同一天,如下圖所示:
╔══════════╦═══════════╦═══════════════╦
║ Location ║ Open Date ║ Running Total ║
╠══════════╬═══════════╬═══════════════╬
║ 1 ║ 1/1/1990 ║ 1 ║
║ 2 ║ 1/3/1990 ║ 2 ║
║ 5 ║ 1/3/1990 ║ 3 ║
║ 3 ║ 2/18/1991 ║ 4 ║
║ 6 ║ 3/17/1992 ║ 5 ║
║ 4 ║ 4/1/1995 ║ 6 ║
╚══════════╩═══════════╩═══════════════╩
所以在這種情況下,我可以說,在1995年4月1日,有6個開放的位置。
我試過使用DSum和DCount,但這些都沒有給出我想要的結果。
UPDATE:
這是我目前使用的代碼:
SELECT t1.[store sort], t1.[soft opening],
(SELECT COUNT(t2.[store sort]) FROM [storelist query] as t2
WHERE Format(t2.[soft opening], "yyyy-mm-dd") & "-" & t2.[store sort] <=
Format(t1.[soft opening], "yyyy-mm-dd") & "-" & t1.[soft opening]) AS Running_Total
FROM [storelist query] as t1
ORDER BY Format(t1.[soft opening], "yyyy-mm-dd") & "-" & t1.[store sort];
然而,這裏是輸出:
+------------+--------------+---------------+
| store sort | soft opening | Running_Total |
+------------+--------------+---------------+
| 1 | 8/1/1980 | 1 |
| 10 | 4/1/1985 | 2 |
| 2 | 10/1/1986 | 2 |
| 3 | 4/1/1987 | 4 |
| 4 | 10/1/1987 | 4 |
| 5 | 3/1/1988 | 5 |
+------------+--------------+---------------+
注:這是使用數據。前面的例子只是用於演示目的的示例數據。
正如你所看到的,這不是所期望的效果。
在這種情況下,應用商店2的總共運行時間爲3,因爲它是第3家商店。商店4應該有一個總共5個商店。而商店5應該有一個運行總共6個商店,等等。
常見問題。查看https://support.microsoft.com/zh-CN/help/290136/how-to-create-a-running-totals-query-in-microsoft-access。搜索論壇,可能會找到例子。這裏是一個https://stackoverflow.com/questions/44443253/loop-through-records-and-increment-value-in-vba-access/44444821#44444821 – June7
搜索'[ms-access]運行total'返回甚至更多的結果,甚至幾個題目幾乎完全像你的問題:[在訪問中運行總查詢](https://stackoverflow.com/questions/38936340/running-total-query-in-access) –
Typo再次罷工!請參閱關於我的答案的評論以瞭解更新的解決方案。 – SandPiper