SQL Sum 多行合二为一 [英] SQL Sum Multiple rows into one

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

问题描述

我需要一些有关 SUM 功能的帮助.我正在尝试将同一帐户的账单金额合计为一个总计,但我得到的结果显示我的 SUM 列只是将我的第一列乘以 3.

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

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

如果可能,我希望同时获得这两种结果.

I would prefer to have both results if possible.

这是我得到的:

我的 SUM 列只是乘以三,实际上并不是根据帐号对数据求和.

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 Sum 多行合二为一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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