sql - Obtaining counts for two different conditions -


suppose have table states follows:

create table states (id int4 unique not null, state int4 not null); 

i want number of rows in table state 1, , number of rows in table state 2. doing in 2 separate queries straightforward:

select count(*) states state = 1; select count(*) states state = 2; 

but seems silly go through whole table twice. can think of clever trick allow me same information in single statement , going through table once?

select     count((state = 1)::integer or null) state_1,     count((state = 2)::integer or null) state_2 states 

or

select     sum((state = 1)::integer) state_1,     sum((state = 2)::integer) state_2 states 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -