如何创建一个查询来提供累计总数?

发布于 2024-12-27 19:26:21 字数 377 浏览 5 评论 0原文

我有一个包含以下列的表:reportDate DATETIME 和损失 CURRENCY,当然还有 ID 列。

如何编写一个查询来返回一个包含损失列的运行总计的表?每个日期都会有多个条目,所以我认为他们需要为每个日期使用 Sum() 。我知道这与 DSum 函数有关,但我仍然迷失在这个函数上。 我认为,它应该看起来像

Month   Losses   Cum
-----   ------   -----
Jan     $3,000   $3,000
Feb     $2,000   $5,000
Mar     $1,500   $6,500

具有非 Access 特定的 sql 语句对我来说是最有帮助的。但所有解决方案均受到赞赏。感谢您的帮助。

I have a table with the following columns: reportDate DATETIME and losses CURRENCY and of course the ID column.

How do I write a query that will return a table with a running total of the losses column? Each date will have multiple entries so i think they will need use Sum() for each date. I know this has to do with the DSum function but im still lost on this one. It should look something like

Month   Losses   Cum
-----   ------   -----
Jan     $3,000   $3,000
Feb     $2,000   $5,000
Mar     $1,500   $6,500

Having a sql statement that's not Access specific would be the most help to me, I think. But all solutions are appreciated. Thanks for the help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

国产ˉ祖宗 2025-01-03 19:26:21

我在您问题的编辑历史记录中找到了表和字段名称,因此在此答案中使用了这些名称。您没有提供 record_matYields 示例数据,因此我创建了自己的数据并希望它合适:

id reportDate gainOrLoss
 1 12/28/2011  $1,500.00
 2 12/29/2011    $500.00
 3 12/30/2011  $1,000.00
 4   1/2/2012     $10.00
 5   1/3/2012  $4,500.00
 6   1/4/2012    $900.00

首先,我创建了 qryMonthlyLosses。以下是 SQL 和输出:

SELECT
    Year(reportDate) AS reportYear,
    Month(reportDate) AS reportMonth,
    Min(y.reportDate) AS MinOfreportDate,
    Sum(y.gainOrLoss) AS SumOfgainOrLoss
FROM record_matYields AS y
GROUP BY
    Year(reportDate),
    Month(reportDate);

reportYear reportMonth MinOfreportDate SumOfgainOrLoss
      2011          12      12/28/2011       $3,000.00
      2012           1        1/2/2012       $5,410.00

我使用第一个查询创建了另一个查询,qryCumulativeLossesByMonth

SELECT
    q.reportYear,
    q.reportMonth,
    q.MinOfreportDate,
    q.SumOfgainOrLoss,
    (
        SELECT
        Sum(z.gainOrLoss)
        FROM record_matYields AS z
        WHERE z.reportDate < q.MinOfreportDate
    ) AS PreviousGainOrLoss
FROM qryMonthlyLosses AS q;

reportYear reportMonth MinOfreportDate SumOfgainOrLoss PreviousGainOrLoss
      2011          12      12/28/2011       $3,000.00 
      2012           1        1/2/2012       $5,410.00          $3,000.00

最后,我使用 qryCumulativeLossesByMonth 作为查询中的数据源,该查询将输出转换为匹配您请求的格式。

SELECT
    q.reportYear,
    MonthName(q.reportMonth) AS [Month],
    q.SumOfgainOrLoss AS Losses,
    q.SumOfgainOrLoss +
        IIf(q.PreviousGainOrLoss Is Null,0,q.PreviousGainOrLoss)
        AS Cum
FROM qryCumulativeLossesByMonth AS q;

reportYear Month    Losses    Cum
      2011 December $3,000.00 $3,000.00
      2012 January  $5,410.00 $8,410.00

您可能可以使用子查询而不是单独的命名查询将其修改为单个查询。我使用这种逐步的方法是因为我希望它更容易理解。

编辑:我使用 MonthName() 函数返回了全名。如果您需要缩写的月份名称,请将 True 作为第二个参数传递给该函数。其中任何一个都应该有效:

MonthName(q.reportMonth, True) AS [Month]
MonthName(q.reportMonth, -1) AS [Month]

I found table and field names in the edit history of your question, so used those names in this answer. You didn't provide record_matYields sample data, so I created my own and hope it is suitable:

id reportDate gainOrLoss
 1 12/28/2011  $1,500.00
 2 12/29/2011    $500.00
 3 12/30/2011  $1,000.00
 4   1/2/2012     $10.00
 5   1/3/2012  $4,500.00
 6   1/4/2012    $900.00

First I created qryMonthlyLosses. Here is the SQL and the output:

SELECT
    Year(reportDate) AS reportYear,
    Month(reportDate) AS reportMonth,
    Min(y.reportDate) AS MinOfreportDate,
    Sum(y.gainOrLoss) AS SumOfgainOrLoss
FROM record_matYields AS y
GROUP BY
    Year(reportDate),
    Month(reportDate);

reportYear reportMonth MinOfreportDate SumOfgainOrLoss
      2011          12      12/28/2011       $3,000.00
      2012           1        1/2/2012       $5,410.00

I used that first query to create another, qryCumulativeLossesByMonth:

SELECT
    q.reportYear,
    q.reportMonth,
    q.MinOfreportDate,
    q.SumOfgainOrLoss,
    (
        SELECT
        Sum(z.gainOrLoss)
        FROM record_matYields AS z
        WHERE z.reportDate < q.MinOfreportDate
    ) AS PreviousGainOrLoss
FROM qryMonthlyLosses AS q;

reportYear reportMonth MinOfreportDate SumOfgainOrLoss PreviousGainOrLoss
      2011          12      12/28/2011       $3,000.00 
      2012           1        1/2/2012       $5,410.00          $3,000.00

Finally I used qryCumulativeLossesByMonth as the data source in a query which transforms the output to match your requested format.

SELECT
    q.reportYear,
    MonthName(q.reportMonth) AS [Month],
    q.SumOfgainOrLoss AS Losses,
    q.SumOfgainOrLoss +
        IIf(q.PreviousGainOrLoss Is Null,0,q.PreviousGainOrLoss)
        AS Cum
FROM qryCumulativeLossesByMonth AS q;

reportYear Month    Losses    Cum
      2011 December $3,000.00 $3,000.00
      2012 January  $5,410.00 $8,410.00

You could probably revise this into a single query using subqueries instead of the separate named queries. I used this step-wise approach because I hoped it would be easier to understand.

Edit: I returned the full name with the MonthName() function. If you want the abbreviated month name, pass True as a second parameter to that function. Either of these should work:

MonthName(q.reportMonth, True) AS [Month]
MonthName(q.reportMonth, -1) AS [Month]
橘寄 2025-01-03 19:26:21

此页面看起来很适合您:

http://support.microsoft.com/kb/290136

仅供参考,我之前针对SQL Server写了如下T-SQL:

create table #a (key_col int, val int)

insert into #a values (1, 10)
insert into #a values (2, 10)
insert into #a values (3, 30)
insert into #a values (4, 10)

select x.key_col,x.val,sum(y.val) as cumulated
from #a x 
inner join #a y on
x.key_col >= y.key_col
group by x.key_col,x.val
order by x.key_col,x.val

drop table #a

结果:

 key_col     val         cumulated   
 ----------- ----------- ----------- 
           1          10          10 
           2          10          20 
           3          30          50 
           4          10          60 

This page looks good for you:

http://support.microsoft.com/kb/290136

FYI, I wrote the following T-SQL against SQL Server before:

create table #a (key_col int, val int)

insert into #a values (1, 10)
insert into #a values (2, 10)
insert into #a values (3, 30)
insert into #a values (4, 10)

select x.key_col,x.val,sum(y.val) as cumulated
from #a x 
inner join #a y on
x.key_col >= y.key_col
group by x.key_col,x.val
order by x.key_col,x.val

drop table #a

The result:

 key_col     val         cumulated   
 ----------- ----------- ----------- 
           1          10          10 
           2          10          20 
           3          30          50 
           4          10          60 
傲娇萝莉攻 2025-01-03 19:26:21

这将在一个 SQL 中为您完成,而无需使用临时表

SELECT Format$([TranDate],"yyyy mm") AS mthYear, First(DSum("[GainOrLoss]","[Trans]","Format$([TranDate],'yyyy mm')='" & [mthYear] & "'")) AS ThisMonth, First(DSum("[GainOrLoss]","[Trans]","Format$([TranDate],'yyyy mm')<='" & [mthYear] & "'")) AS RunningTotal FROM trans GROUP BY Format$([TranDate],"yyyy mm");

This will do it for you in one SQL without using temporary tables

SELECT Format$([TranDate],"yyyy mm") AS mthYear, First(DSum("[GainOrLoss]","[Trans]","Format$([TranDate],'yyyy mm')='" & [mthYear] & "'")) AS ThisMonth, First(DSum("[GainOrLoss]","[Trans]","Format$([TranDate],'yyyy mm')<='" & [mthYear] & "'")) AS RunningTotal FROM trans GROUP BY Format$([TranDate],"yyyy mm");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文