次のようなテーブルがあるstates
とします。
create table states (id int4 unique not null, state int4 not null);
状態が 1 のテーブルの行数と、状態が 2 のテーブルの行数を取得したいと考えています。これを 2 つの別々のクエリで行うのは簡単です。
select count(*) from states where state = 1;
select count(*) from states where state = 2;
しかし、テーブル全体を 2 回調べるのはばかげているようです。誰かが、1 つのステートメントで同じ情報を取得し、テーブルを 1 回だけ通過できる巧妙なトリックを思いつくことができますか?