Home > Software engineering >  Counting rows distinctly on multiple values from in clause in oracle sql
Counting rows distinctly on multiple values from in clause in oracle sql

Time:11-17

I am looking at counting rows distinctly on multiple values from in clause in oracle sql, so that it retrieves output in just 1 query

I tried running below query which leads me one column output on # of A22 ID matching rows count.

select count(*) as A22 from TABLE_NAME where ID = 'A22' ;

If I run below query on multiple ID's using various ID values with in clause it gets me total ( summation) count, but don't see direct option in query to retrieve 4 column value of count per ID time

select count(*) from TABLE_NAME where ID in ('A22','B22', 'C22', 'D22')

Apart from running same count* query 4 times or in loop, is there a better way to get 4 ID count values from the table?

CodePudding user response:

Looks like

select id, count(*) 
from TABLE_NAME 
where ID in ('A22','B22', 'C22', 'D22')
group by id
  • Related