title: Group in ranges values extracted from an 'alphanumeric + symbols' string [PostgreSQL]
body: <ul>
  <li><strong>Nb.</strong> I am using PostgreSQL.</li>
  <li><strong>Nb1.</strong> For simplicity purposes I am only showing the two relevant columns for this post, original table has more rows &amp; columns.</li>
  </ul>
  
  <p>I have a table called 'contents' like this:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;body&gt;
  
  &lt;table border="1" style="width:100%"&gt;
    &lt;tr&gt;
      &lt;td&gt;id&lt;/td&gt;
      &lt;td&gt;data&lt;/td&gt;  
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;4009&lt;/td&gt;
      &lt;td&gt;"duration"=&gt;"101", "preview_version"=&gt;"", "twitter_profile"=&gt;"", "creator_category"=&gt;"association", "facebook_profile"=&gt;"", "linkedin_profile"=&gt;"", "personal_website"=&gt;"", "content_expertise_type"=&gt;"image", "content_expertise_categories"=&gt;"1,2,3"&lt;/td&gt;  
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;4865&lt;/td&gt;
      &lt;td&gt;"duration"=&gt;"108", "preview_version"=&gt;"", "twitter_profile"=&gt;"", "creator_category"=&gt;"association", "facebook_profile"=&gt;"", "linkedin_profile"=&gt;"", "personal_website"=&gt;"", "content_expertise_type"=&gt;"image", "content_expertise_categories"=&gt;"4,6"&lt;/td&gt;  
    &lt;/tr&gt;
  &lt;/table&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>from this table I need to extract the duration value by using this query:</p>
  
  <pre><code>select id,data-&gt;'duration' as data from contents
  </code></pre>
  
  <p>which gives me the below result (again, the original table will return many more entries and some values in "data" column will coincide reason why I need to group them in ranges):</p>
  
  <pre><code>+------+------+
  |  id  | data |
  +------+------+
  | 4009 |  101 |
  | 4865 |  108 |
  +------+------+
  </code></pre>
  
  <p>Now that I have the 'data' values I want to tag them in different ranges</p>
  
  <pre><code>SELECT d.id,
      case when d.data &gt;= 0 and d.data&lt; 10 then '0-9' 
      when d.data &gt;= 10 and d.data&lt; 20 then '10-19' 
      else '20-500' end as range 
  FROM (SELECT id,data-&gt;'duration' as data FROM contents) as d
  </code></pre>
  
  <p>But here the query returns this error:</p>
  
  <blockquote>
    <p>ERROR:  operator does not exist: text >= integer
    LINE 3:          case when d.data >= 0 and d.data&lt; 10 then '0-9'
                                      ^
    HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.</p>
  </blockquote>
  
  <p>After this I was hoping to group the ranges like this:</p>
  
  <pre><code>SELECT t.range as score_range, count(*) as number_of_ocurrences 
  FROM
      (***ABOVE QUERY THAT CURRENTLY RETURNS AN ERROR TO BE PLACED IN HERE***) as t
  GROUP BY t.range
  ORDER BY score_range
  </code></pre>
  
  <p>Any help to achieve this grouping task will be very much appreciated!</p>
  
  <p>Looking forward to getting an answer! :-)</p>
  
  <p>Thanks!</p>