PostgreSQL – Double Count Query

aggregatepostgresql

I have a table with 3 columns: key, status and rank.
status contains numbers and rank contains t or f.
key is auto numbered and irrelevant here.

my goal is to count how many in each status are t and how many false
for example:

if my data is (key, status, rank)

1 10 f
2 10 t
3 10 t
4 11 t

then result will be : (status, rank, count value)

10 f 1
10 t 2
11 t 1

how do i do that?

Best Answer

This is a simple group by query.

select status, 
       rank,
       count(*) as cnt
from the_table
group by status, rank
order by status, rank;