2011-01-26 28 views
1

我又回到了另一个Oracle查询。我想要做的是计算多个列按照公共字段分组。到目前为止,我完成了一半。所以下面的表中给出Oracle/SQL Couting由共同列分组的多个列表

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 

但我不确定如何去做在。有任何想法吗?

谢谢!

回答

6
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)如果你喜欢

+0

非常好,谢谢! – dscl 2011-01-26 01:52:31