Mysql查询总和和分组不显示具有空值的行

发布于 2025-01-20 18:32:16 字数 589 浏览 0 评论 0原文

我有以下查询:

SELECT Customer, SUM(SoldUnits) AS SoldUnits
FROM Uploads
WHERE Year = 2021
AND Week = 11
GROUP BY Customer;

并返回:

CustomerSoldUnits
CUSTOMER A55
CUSTOMER B32
CUSTOMER D17

CUSTOMER C 存在,但没有 week 11 的数据并且我想显示带有 0 个 SoldUnitsCUSTOMER C。我怎样才能做到这一点?

I have this query:

SELECT Customer, SUM(SoldUnits) AS SoldUnits
FROM Uploads
WHERE Year = 2021
AND Week = 11
GROUP BY Customer;

And Returns me:

CustomerSoldUnits
CUSTOMER A55
CUSTOMER B32
CUSTOMER D17

CUSTOMER C exist, but it doesn't have data for the week 11 and I want to show CUSTOMER C with 0 SoldUnits. How Can I do that?

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

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

发布评论

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

评论(1

黒涩兲箜 2025-01-27 18:32:16

假设您的表确实包含您希望在报告中显示的所有客户,您可以进行条件求和并删除 WHERE 子句:

SELECT Customer,
       SUM(CASE WHEN Year = 2021 AND Week = 11
                THEN SoldUnits ELSE 0 END) AS SoldUnits
FROM Uploads
GROUP BY Customer;

Assuming your table does have all customers you want to appear in the report, you could do a conditional summation and remove the WHERE clause:

SELECT Customer,
       SUM(CASE WHEN Year = 2021 AND Week = 11
                THEN SoldUnits ELSE 0 END) AS SoldUnits
FROM Uploads
GROUP BY Customer;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文