我想要做的是合并几行数据以显示为一行。
从此:
对此:
非常欢迎任何建议。
谢谢。
最佳答案
您可以使用条件聚合:
select contractid,
max(case when contractgroupid = 189 then amount else 0 end) as group_189,
max(case when contractgroupid = 190 then amount else 0 end) as group_190,
max(case when contractgroupid = 191 then amount else 0 end) as group_191,
max(case when contractgroupid = 192 then amount else 0 end) as group_192,
sum(amount) as total_amount
from mytable
group by contractid
如果同一组对于给定合约可能出现多次,您可能需要使用 sum()
而不是 max()
。
关于mysql - SQL 根据匹配值将多行合并为单行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65327538/