title: Select a count of rows, order by dynamic ranges of time intervals in PostgreSQL
body: <p>Here's the test table for my question : </p>
  
  <pre><code>CREATE TABLE document (
      id integer NOT NULL,
      name character varying(120) NOT NULL,
      owner_id bigint DEFAULT 0 NOT NULL,
      doc_type_id smallint DEFAULT 1 NOT NULL,
      archived boolean DEFAULT false NOT NULL,
      insert_date timestamp without time zone DEFAULT now() NOT NULL,
      modify_date timestamp without time zone DEFAULT now() NOT NULL,
      last_writer_id bigint
  );
  </code></pre>
  
  <p>Modify_date determines the last time someone edited a document.</p>
  
  <p>In order to make some statistics, I need to get the time between the creation(insert_date) and modify_date. And then to display a bar chart, I need to get a count of document where this time interval is, for example, between 0 and 5 days, 6 and 10 days, etc. So ranges must be calculated in the query I guess. 
  The result expected (or kind of...) is :</p>
  
  <pre><code>Age                Count
  0-5                2
  6-10               5
  11-15              9
  ...                ...
  </code></pre>
  
  <p>Of course the age could be on a scale where 0-5 == 0, 6-10 == 1. I'll prepare the data to display them. </p>
  
  <p>I found a post quite similar but I couldn't apply it to my case. (<a href="https://stackoverflow.com/questions/17516000/select-data-for-15-minute-windows-postgresql?rq=1">Select data for 15 minute windows - PostgreSQL</a>)</p>
  
  <p>Thanks for any answer you could bring to me.</p>
  
  <p>EDIT 1:</p>
  
  <p>The ranges needs to be dynamically generated from the minimum and maximum age I can get from the table.</p>