有一列存储数字,另一列存储状态。状态可以存储1(一个)或0(零)。
我尝试了每种情况的when子句和子查询。但是这个子查询太长了。我想缩短查询。
select (select sum(number) from account where state = 1) as activesum, (select sum(number) from account where state=0) as passivesum from dual
PassiveSum ActiveSum
1458,6 152,3
我希望这个查询像这样工作 从状态为(0,1)的帐户中选择活动的总和(数字),被动的总和(数字)
答案 0 :(得分:1)
假设state
仅采用这两个值,则可以执行以下操作:
select sum(state) as active_sum,
sum(1 - state) as passivesum
from account;
如果您想要数字的总和,请使用case
:
select sum(case when state = 1 then number end) as active_sum,
sum(case when state = 0 then number end) as passivesum
from account;