Add my sales together based on Id and Name using SUM function(使用 SUM 函数根据 Id 和 Name 将我的销售额加在一起)
问题描述
我使用 SSMS 创建了一个请求:
I created a request with SSMS:
这是我的输出:
正如您在代表销售列的 [ventes]
列中所见,销售额不会根据 ID 号和名称相加,但我确实使用了 SUM()
功能在我的选择中.关于我的代码有什么问题的任何想法?
As you can see in the [ventes]
column which represent the sales column, the sales don't add up depending on the Id number and Name but I do use the SUM()
function in my select. Any ideas as to what is wrong with my code?
推荐答案
问题在于您的group by
:
- 在对该列求和时,您不希望
group by
SubTotal
. - 并且当您尝试对一年内的总数求和时,您不希望
group by
OrderDate
.相反,您希望group by
您正在选择的相同计算,例如DATEPART(YYYY, [OrderDate])
.
- You don't want to
group by
SubTotal
when you are summing that column. - And you don't want to
group by
OrderDate
when you are trying to sum the totals over a year. Rather you want togroup by
the same computation you are selecting e.g.DATEPART(YYYY, [OrderDate])
.
所以你更正的 group by
是:
仅供参考:如果您以类似于以下的格式发布问题,即最小可重现示例你让人们更容易、更有可能提供帮助.
FYI: If you post your questions in a format similar to the following, which is a Minimal Reproducible Example you make it much easier and much more likely for people to assist.
返回:
这篇关于使用 SUM 函数根据 Id 和 Name 将我的销售额加在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!