Brian Brian - 1 month ago 10
SQL Question

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

vkp vkp
Answer

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