我有一个transactions表,如下所示(简化):
TranID|TranType|TranVal|TranCode
12|Add|10|NJ
135|Add|50|NJ
142|Substract|20|NJ
501|Add|415|PA
421|Substract|15|PA我想添加‘加法’,然后减去‘减法’,按TranCode分组。
因此,NJ的最终和是: 40,PA的最终和是: 400。
考虑到没有公共密钥,这是可能的吗?
发布于 2017-12-12 08:09:51
只需使用聚合:
select trancode,
sum(case when trantype = 'add' then tranval
when trantype = 'subtract' then - tranval
end)
from t
group by trancode;https://stackoverflow.com/questions/47763445
复制相似问题