我在 MySQL 中使用 SUM() 时遇到问题

发布于 2024-09-11 19:32:17 字数 406 浏览 10 评论 0原文

我想获取由特定参加者参加的特定客户的BalanceAmt。标准是顾客可以购买不止一件商品。每个购买的物品都被单独记录。在某些情况下,“参加者”值将为 NULL。

这是我的查询:

SELECT Customer, AttendedBy, SUM(BalanceAmt) FROM billing GROUP BY Customer 
     HAVING AttendedBy='attender' AND Customer='CustomerName'

我的问题是我得到了“出席者”未参与的值的总和(即,包括 NULL 值)。 帮我做一下..

提前谢谢..

I want to fetch the BalanceAmt of a particular Customer attended by a particular attender. The criteria is that a customer may buy more than one items. Each bought items has been recorded individually. At some cases 'attender' value would be NULL.

Here's my query:

SELECT Customer, AttendedBy, SUM(BalanceAmt) FROM billing GROUP BY Customer 
     HAVING AttendedBy='attender' AND Customer='CustomerName'

My problem is I'm getting the value summed up not attended by the 'attender' (i.e., including NULL value).
Help me to do it..

Thanx in advance..

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

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

发布评论

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

评论(3

清晨说晚安 2024-09-18 19:32:17

我感觉问题没有得到正确的答案,但我会像这样编写 SELECT 语句,但

SELECT Customer, SUM(BalanceAmt) 
FROM billing 
WHERE AttendedBy='attender' AND Customer='CustomerName' 
GROUP BY Customer  

我不确定AssignedTo 是什么。如果您想要 Customer、AssignedTo 的总和,那么它将是:

SELECT Customer, AssignedTo, SUM(BalanceAmt) 
FROM billing 
WHERE AttendedBy='attender' AND Customer='CustomerName' 
GROUP BY Customer, AssignedTo 

I have the feeling haven't gotten the question right, but I would write the SELECT Statement like this

SELECT Customer, SUM(BalanceAmt) 
FROM billing 
WHERE AttendedBy='attender' AND Customer='CustomerName' 
GROUP BY Customer  

I am not sure what the AssignedTo is. If you want the sum on Customer, AssignedTo then it would be:

SELECT Customer, AssignedTo, SUM(BalanceAmt) 
FROM billing 
WHERE AttendedBy='attender' AND Customer='CustomerName' 
GROUP BY Customer, AssignedTo 
酷到爆炸 2024-09-18 19:32:17

我认为您需要包含一个 where 子句来排除您的空值...类似以下内容应该可以解决问题:

SELECT Customer, AssignedTo, SUM(BalanceAmt) FROM billing 
WHERE not(BalanceAmt is null) AND AttendedBy='attender' AND Customer='CustomerName'
GROUP BY Customer, AssignedTo

HTH。

I think you need to include a where clause to exclude your null values... Something like the following should do the trick:

SELECT Customer, AssignedTo, SUM(BalanceAmt) FROM billing 
WHERE not(BalanceAmt is null) AND AttendedBy='attender' AND Customer='CustomerName'
GROUP BY Customer, AssignedTo

HTH.

迎风吟唱 2024-09-18 19:32:17
SELECT Customer, SUM(BalanceAmt) 
FROM billing 
WHERE AttendedBy='attender' AND Customer='CustomerName' 
GROUP BY Customer

在上面的查询中,不需要 group by ,因为您已将该列放在 where 子句中。

您可以从 select 中删除它,也可以删除 group by

您可以编写类似这样的内容,以获得按客户计算的总和。

SELECT Customer, SUM(BalanceAmt) 
FROM billing 
WHERE AttendedBy='attender'
GROUP BY Customer;
SELECT Customer, SUM(BalanceAmt) 
FROM billing 
WHERE AttendedBy='attender' AND Customer='CustomerName' 
GROUP BY Customer

In your above query group by is not required as you have put the column in the where clause.

You can remove it from select and you remove group by as well.

You can write something like this, to get the Customer-wise sum.

SELECT Customer, SUM(BalanceAmt) 
FROM billing 
WHERE AttendedBy='attender'
GROUP BY Customer;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文