答案 0 :(得分:0)
这可以通过条件聚合来完成(假设每个代码的每个ID都有一行)。
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