不同时期的 SQL sum()?

发布于 2024-10-01 00:21:03 字数 1510 浏览 2 评论 0原文

我正在研究一个存储过程。我有一个名为 #CashFlow 的表,其中包含一段时间的数据。

FundID   TradeDate Amount 
 1       1/1/2004  123.00 
 1       6/30/2006 100.00 
 2       1/1/2004  100.00 
 2       3/15/2010 150.00
 3       1/1/2010  100.00

我还有一个名为 #Funds 的表,其中包含我感兴趣的基金 ID 列表。(有一堆处理过程可以为我生成此基金列表,我不会让您感到厌烦)例如,假设我的 #Funds 表中只有 ID 1 和 2(不包括 3)。

我有三个时间段(均以“2010/8/31”结尾),分别从 1/1/2004、1/1/2006 和 1/1/2010 开始,我想汇总这三个时间段的总和。

我尝试过这样的事情:

select sum(c1.amount), sum(c2.amount), sum(c3.amount)
from 
    #fundtable f
inner join
    #cashflow c1 on f.fundid = c1.fundid and c1.tradedate between '1/1/2004' and '8/31/2010'
inner join
    #cashflow c2 on f.fundid = c2.fundid and c2.tradedate between '1/1/2006' and '8/31/2010'
inner join 
    #cashflow c3 on f.fundid = c3.fundid and c3.tradedate between '1/1/2010' and '8/31/2010'

但它没有做我想做的事(事实上我无法弄清楚它到底在做什么)。如果我只选择一个时期,我确实会得到该时期的正确值,但是当我添加其中一个连接时,我的数字就会被全部破坏。

这类似于我试图替换的原始查询:

select 
   (select sum(Amount) from #Cashflow c inner join #fundtable f on c.fundid = f.fundid where tradedate between '1/1/2004' and '8/31/2010') as 'Period1',
   (select sum(Amount) from #Cashflow c inner join #fundtable f on c.fundid = f.fundid where tradedate between '1/1/2006' and '8/31/2010') as 'Period2',
   (select sum(Amount) from #Cashflow c inner join #fundtable f on c.fundid = f.fundid where tradedate between '1/1/2010' and '8/31/2010') as 'Period3'

请注意,我的日期实际上是变量(对此无关紧要)和我的原始查询

I'm working on a stored procedure. I have a table called #CashFlow that contains data for a span of time.

FundID   TradeDate Amount 
 1       1/1/2004  123.00 
 1       6/30/2006 100.00 
 2       1/1/2004  100.00 
 2       3/15/2010 150.00
 3       1/1/2010  100.00

I also have a table called #Funds which contains a list of the fund id's that I'm interested in. (There is a bunch of processing that I won't bore you with that generates this list of fund's for me) For example, let's just say that my #Funds table only has ID's 1 and 2 in it (3 is excluded).

I have three time periods (all ending '8/31/2010') starting at 1/1/2004, 1/1/2006, and 1/1/2010 and I'd like to aggregate the sum over the three periods.

I've tried something like this:

select sum(c1.amount), sum(c2.amount), sum(c3.amount)
from 
    #fundtable f
inner join
    #cashflow c1 on f.fundid = c1.fundid and c1.tradedate between '1/1/2004' and '8/31/2010'
inner join
    #cashflow c2 on f.fundid = c2.fundid and c2.tradedate between '1/1/2006' and '8/31/2010'
inner join 
    #cashflow c3 on f.fundid = c3.fundid and c3.tradedate between '1/1/2010' and '8/31/2010'

But it isn't doing what I want (in fact I can't figure out what exactly it IS doing). If I only select one period, I DO get the right values for the period, but the second I add one of the joins, my numbers get all whacked ou.

This is something like the original query that I'm trying to replace:

select 
   (select sum(Amount) from #Cashflow c inner join #fundtable f on c.fundid = f.fundid where tradedate between '1/1/2004' and '8/31/2010') as 'Period1',
   (select sum(Amount) from #Cashflow c inner join #fundtable f on c.fundid = f.fundid where tradedate between '1/1/2006' and '8/31/2010') as 'Period2',
   (select sum(Amount) from #Cashflow c inner join #fundtable f on c.fundid = f.fundid where tradedate between '1/1/2010' and '8/31/2010') as 'Period3'

Note that my dates are actually variables (which shouldn't matter for this) and my original query

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

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

发布评论

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

评论(2

回忆凄美了谁 2024-10-08 00:21:03
SELECT  SUM(CASE WHEN TradeDate BETWEEN '2004-01-01' AND '2010-08-31' THEN Amount END),
        SUM(CASE WHEN TradeDate BETWEEN '2006-01-01' AND '2010-08-31' THEN Amount END),
        SUM(CASE WHEN TradeDate BETWEEN '2010-01-01' AND '2010-08-31' THEN Amount END)
FROM    Funds
JOIN    Cashflow
ON      Cashflow.id = Funds.id
SELECT  SUM(CASE WHEN TradeDate BETWEEN '2004-01-01' AND '2010-08-31' THEN Amount END),
        SUM(CASE WHEN TradeDate BETWEEN '2006-01-01' AND '2010-08-31' THEN Amount END),
        SUM(CASE WHEN TradeDate BETWEEN '2010-01-01' AND '2010-08-31' THEN Amount END)
FROM    Funds
JOIN    Cashflow
ON      Cashflow.id = Funds.id
深巷少女 2024-10-08 00:21:03

使用分组依据:

select period, sum(money) from table group by period

Use group by:

select period, sum(money) from table group by period
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文