Brian
Brian

Reputation: 14836

SQL, return select results with different where clauses

I have table whose column is just the length of a session and I would like to return the number of session that have zero length and the number of sessions that have length greater than zero.

I can do that with two separate commands

select count(session_length) from my_table where session_length=0

select count(session_length) from my_table where session_length>0

But I would like to see the results combined in one table

Upvotes: 0

Views: 29

Answers (3)

RDoogan
RDoogan

Reputation: 3

This may be too simple so apologies if I have misread your query but Can you use

select count(session_length) from my_table where session_length >= 0

Again, Apologies if this is not what you're looking for.

Upvotes: 0

JohnHC
JohnHC

Reputation: 11195

select 1 as QryNo, count(session_length) as SessLen
from my_table 
where session_length=0

union 

select 2 as QryNo, count(session_length) as SessLen
from my_table
where session_length>0

or

select 
    case
      when session_length = 0 then 1
      else 2
    end  as QryNo, 
    count(session_length) as SessLen
from my_table

Upvotes: 1

Vamsi Prabhala
Vamsi Prabhala

Reputation: 49260

You can do it with one query using conditional aggregation.

select 
count(case when session_length = 0 then 1 end),
count(case when session_length > 0 then 1 end) 
from my_table

Upvotes: 1

Related Questions