答案 0 :(得分:1)
只需使用条件聚合:
select id,
sum(case when condition = 'true' then value else 0 end) as num_true,
sum(case when condition = 'false' then value else 0 end) as num_false
from t
group by id;
您已使用Oracle和SQL Server标记了该问题。它们都不直接在SQL中支持布尔类型,所以我猜这个条件是一个字符串。
答案 1 :(得分:1)
是的可能
select id "id", sum(decode(condition,'TRUE',value,0)) "sum_of_condition_true",
sum(decode(condition,'FALSE',value,0)) "sum_of_condition_false"
from mytable
group by id
order by id;