1

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!

1 Answer 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

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.