开发者

Oracle/SQL Couting multiple colums grouped by a common column

I'm back with yet another Oracle query. What I want to do is do counting on multiple columns grouped by a common field. I have half of this done so far. So given the following table

THING ACTION
--------------
T1  _A_
T1  _A_
T1  _B_
T2  _A_
T2  _B_
开发者_Go百科

I have this query

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

Which results in

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

What I would like to see though is this

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

But I'm not certain how to do that. Any ideas?

Thanks!


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

or sum(case action when '_A_' then 1 else 0 end) if you prefer

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜