select id
,coalesce(max(case when code = 'code1' then cast(amount as int) end), 0) -
coalesce(max(case when code = 'code2' then cast(amount as int) end), 0) +
coalesce(max(case when code = 'code3' then cast(amount as int) end), 0) -
coalesce(max(case when code = 'code4' then cast(amount as int) end), 0)
from tbl
group by id
1条答案
按热度按时间zz2j4svz1#
这可以通过条件聚合来完成(假设每个代码的每个id有一行)。