1
0
mirror of https://github.com/jbranchaud/til synced 2026-03-04 06:58:45 +00:00

Add Count How Many Records There Are Of Each Type as a postgres til

This commit is contained in:
jbranchaud
2021-03-09 19:25:57 -06:00
parent 7b512d0c43
commit 0ec8b98d00
2 changed files with 49 additions and 1 deletions

View File

@@ -0,0 +1,47 @@
# Count How Many Records There Are Of Each Type
Let's say I have a `books` table full of data. One of the columns on this table
is `status` which represents whether the book is published, in review, or still
a draft.
We can find out how many records (books) there are for each `status` using a
`group by` clause and the `count` aggregate function.
```sql
> select status, count(*)
from books
group by status;
status | count
-----------+-------
ø | 123
published | 611
draft | 364
review | 239
(4 rows)
```
Because we don't have a `not null` constraint on the `status` column, there are
also some records that have a null value.
We can take this a step further by ordering the output in a consistent
way—descending order of the count column.
```sql
> select status, count(*)
from books
group by status
order by 2 desc;
status | count
-----------+-------
published | 611
draft | 364
review | 239
ø | 123
(4 rows)
```
This `order by` clauses uses [a positional index from the select
arguments](use-argument-indexes.md), so the `2` references the `count(*)`
argument.