【发布时间】:2021-02-22 09:28:29
【问题描述】:
【问题讨论】:
标签: sql tsql aggregate-functions
【问题讨论】:
标签: sql tsql aggregate-functions
一种方法是条件聚合:
select sum(case when code = 'fst' then value else - value end)
from t
where code in ('fst', 'trd');
假设每个代码只有一行,您也可以使用join:
select tf.value - tt.value
from t tf join
t tt
on tf.code = 'fst' and tt.code = 'trd'
【讨论】:
sum(case when code = 'fst' then value end) / sum(case when value = 'trd' then value end).