【问题标题】:Counting multiple columns with the same criteria计算具有相同条件的多列
【发布时间】:2015-01-27 17:17:55
【问题描述】:

我想根据内容计算多列。我要计算的每一列(alpha、bravo 和 charlie)都有“T”或“F”,我只想计算那些有“T”的列。

到目前为止我有:

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 种不同的配置下运行才能得到我想要的结果,因此它不是编写多个版本的实用解决方案。

【问题讨论】:

  • 在这种情况下,例如在您的 Alpha 值中 - 您还计算了不等于 'T' 但具有 (bravo='T' 或 charlie='T) 的 alpha 值,因为where 子句中的“或”条件
  • 感谢 SoulTrain,由于我得到的结果存在差异,我认为这是正在发生的事情。

标签: sql oracle10g oracle-apex


【解决方案1】:

删除where 子句并改用Conditional Aggregate,这将有助于您仅在数据为T 时才count 行。

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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-11
    • 1970-01-01
    • 2021-02-24
    • 1970-01-01
    • 1970-01-01
    • 2014-01-19
    • 2018-11-24
    • 2013-02-26
    相关资源
    最近更新 更多