【问题标题】:Oracle/SQL Couting multiple colums grouped by a common columnOracle/SQL 计算按公共列分组的多列
【发布时间】:2011-01-26 01:34:54
【问题描述】:

我又回来了一个 Oracle 查询。我想要做的是对按公共字段分组的多个列进行计数。到目前为止,我已经完成了一半。所以给出下表

THING ACTION
--------------
T1  _A_
T1  _A_
T1  _B_
T2  _A_
T2  _B_

我有这个问题

select    THING,
    count(ACTION) as "A"
 from  <table>
 where  ACTION = '_A_'
 group by THING

结果

THING A
----------
T1    2
T2    1

我想看到的是这个

THING A B
--------------
T1    2   1
T2    1   1

但我不确定该怎么做。有什么想法吗?

谢谢!

【问题讨论】:

    标签: sql oracle count group-by


    【解决方案1】:
    select thing,
           count(case action when '_A_' then 1 end) as a,
           count(case action when '_B_' then 1 end) as b
    from <table>
    group by thing
    

    或者sum(case action when '_A_' then 1 else 0 end),如果你愿意的话

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多