2011-01-26 64 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