SQL总和多行合成一个 [英] SQL Sum Multiple rows into one

查看:126
本文介绍了SQL总和多行合成一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些关于SUM功能的帮助。我试图将同一个账户的账单数额合计为一个总计,但是我得到的结果显示我的SUM列仅增加了我的第一列三倍。

I need some help with the SUM feature. I am trying to SUM the bill amounts for the same account into one grand total, but the results I am getting show my SUM column just multiples my first column by 3.

这是我想要的模拟数据结果:

Here is what I want as results for my mock data:

AccountNumber    Bill      BillDate   
1                100.00    1/1/2013     
1                150.00    2/1/2013   
1                200.00    3/1/2013 
2                75.00     1/1/2013  
2                100.00    2/1/2013   

查询:

SELECT AccountNumber, Bill, BillDate, SUM(Bill)
FROM Table1
GROUP BY AccountNumber, Bill, BillDate


AccountNumber    Bill      BillDate    SUM(Bill)
1                100.00    1/1/2013    450.00    
1                150.00    2/1/2013    450.00
1                200.00    3/1/2013    450.00
2                75.00     1/1/2013    175.00
2                100.00    2/1/2013    175.00

OR

AccountNumber    Bill      SUM(Bill)
1                100.00    450.00    
2                75.00     175.00

如果可能,我宁愿同时拥有这两个结果。

I would prefer to have both results if possible.

这是我得到的结果:

我的SUM列只是乘以3,实际上并不是根据帐户号码汇总数据。

My SUM column is just multiplying by three, it's not actually summing the data based on account Number.

AccountNumber    Bill      BillDate    SUM(Bill)
1                100.00    1/1/2013    300.00    
1                150.00    2/1/2013    450.00
1                200.00    3/1/2013    600.00
2                75.00     1/1/2013    225.00
2                100.00    2/1/2013    300.00


推荐答案

感谢您的回应。原来我的问题是数据库问题有重复的条目,而不是我的逻辑。快速表同步修复了这一点,并且SUM功能按预期工作。这对于SUM功能来说仍然是非常有用的知识,如果您在使用时遇到问题,值得一读。

Thank you for your responses. Turns out my problem was a database issue with duplicate entries, not with my logic. A quick table sync fixed that and the SUM feature worked as expected. This is all still useful knowledge for the SUM feature and is worth reading if you are having trouble using it.

这篇关于SQL总和多行合成一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆