How to group time intervals by order of magnitude in Postgres?
A CASE expression can help:

SELECT CASE WHEN gap &lt;= INTERVAL '10 minutes'
            THEN '0-10 minutes'
            WHEN gap &lt;= INTERVAL '59 minutes'
            THEN '10-59 minutes'
            ...
            WHEN gap &lt;= INTERVAL '10 days'
            THEN '2-10 days'
            ELSE 'more than 10 days'
       END AS gap_range
...
GROUP BY gap_range;