title: What is a SQL frequency-distribution query to count ranges with group-by, and include 0 counts?
body: <p>Given:</p>
  
  <pre><code>table 'thing':
  
  age
  ---
  3.4
  3.4
  10.1
  40
  45
  49
  </code></pre>
  
  <p>I want to count the number of things for each 10-year range, e.g.,</p>
  
  <pre><code>age_range | count
  ----------+-------
          0 |     2
          10|     1
          20|     0
          30|     0
          40|     3
  </code></pre>
  
  <p>This query comes close:</p>
  
  <pre><code>SELECT FLOOR(age / 10) as age_range, COUNT(*)
  FROM thing
  GROUP BY FLOOR(age / 10) ORDER BY FLOOR(age / 10);
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code> age_range | count 
  -----------+-------
           0 |     1
           1 |     2
           4 |     3
  </code></pre>
  
  <p>However, it doesn't show me the ranges which have 0 counts.  How can I modify the query so that it also shows the ranges in between with 0 counts?</p>
  
  <p>I found similar stackoverflow questions for counting ranges, some for 0 counts, but they involve having to specify each range (either hard-coding the ranges into the query, or putting the ranges in a table). I would prefer to use a generic query like that above where I do not have to explicitly specify each range (e.g., 0-10, 10-20, 20-30, ...). I'm using PostgreSQL 9.1.3.</p>
  
  <p>Is there a way to modify the simple query above to include 0 counts?</p>
  
  <p>Similar: <br>
  <a href="https://stackoverflow.com/questions/2483140/oracle-how-to-group-by-over-a-range">Oracle: how to &quot;group by&quot; over a range?</a> <br>
  <a href="https://stackoverflow.com/questions/8636918/get-frequency-distribution-of-a-decimal-range-in-mysql">Get frequency distribution of a decimal range in MySQL</a></p>