dscl
dscl

Reputation: 1626

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_

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!

Upvotes: 1

Views: 2021

Answers (1)

araqnid
araqnid

Reputation: 133712

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

Upvotes: 6

Related Questions