使用相同标准计算多个列

时间:2015-01-27 17:17:56

标签: sql oracle10g oracle-apex

我想根据内容计算多个列。我想要计算的每一列(alpha,bravo和charlie)都有' T'或者' F',我只想数一个带有' F'。

到目前为止,我有:

select t1.name, count(t1.alpha), count(t1.bravo), count(t1.charlie)
from t1
  where alpha='T',
  or bravo='t'
  or charlie='t'
group by name
order by name asc;

但是我得到的数字与我得到的数据不匹配

select count(*)
from t1
where name='test'
and alpha='t';

这个例子需要以16种不同的配置运行才能得到我想要的结果,因此它不是编写多个版本的实用解决方案。

1 个答案:

答案 0 :(得分:2)

删除where子句并改为使用Conditional Aggregate,这样,只有在数据为count时才能帮助您T行。

select t1.name, 
count(case when t1.alpha ='T' then 1 end),
count(case when t1.bravo='T' then 1 end),
count(case when t1.charlie='T' then 1 end)
from t1
group by name
order by name asc;