Given:
table 'thing': age --- 3.4 3.4 10.1 40 45 49
I want to count the number of things for each 10 year range, for example,
age_range | count ----------+------- 0 | 2 10| 1 20| 0 30| 0 40| 3
This request is close:
SELECT FLOOR(age / 10) as age_range, COUNT(*) FROM thing GROUP BY FLOOR(age / 10) ORDER BY FLOOR(age / 10);
Output:
age_range | count -----------+------- 0 | 1 1 | 2 4 | 3
However, it does not show ranges that have 0 samples. How can I modify the query so that it also shows ranges between 0 values?
I found similar stacking questions for counting ranges, some for counting 0, but they are related to the need to specify each range (either hard code the ranges in the query, or put ranges in a table). I would prefer to use a general query like the one above, where I do not need to explicitly specify each range (e.g. 0-10, 10-20, 20-30, ...). I am using PostgreSQL 9.1.3.
Is there a way to change the simple query above to include 0 score?
Similar:
Oracle: how is "group by" in a range?
Get frequency distribution of decimal range in MySQL
sql aggregate-functions group-by postgresql
Rob bednark
source share