gpt4 book ai didi

mysql - 根据 SQL 中的不同元素,必需的 Sum 导致单独的列

转载 作者:行者123 更新时间:2023-11-29 07:37:32 26 4
gpt4 key购买 nike

我是 SQL 的新手,基本上我需要根据不同的元素在单独的列中求和结果。

请在下图中找到我的表格和所需的结果。

enter image description here

请指导我如何使用 SQL 查询来获得所需的结果。

最佳答案

您可以使用条件聚合:

select payment,
sum(case when product = 'A' then amount else 0 end) as a,
sum(case when product = 'B' then amount else 0 end) as b,
sum(case when product = 'C' then amount else 0 end) as c,
sum(case when product = 'D' then amount else 0 end) as d,
sum(amount) as total
from t
group by payment;

关于mysql - 根据 SQL 中的不同元素,必需的 Sum 导致单独的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48007803/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com