In any SELECT query, where a DISTINCT ON is used, how can one additionally get the number of duplicates for each row in the result set?
Take e.g.
SELECT
DISTINCT ON (building)
building,
name
FROM ...
WHERE ...
This will only return the first result for each building. I want to add another column, so the results look like this:
name | building | excluded
Fred | Office | 0
Bob | Storage | 3
when there are more people than Bob in Storage. I'm using Postgres 10.