title: Postgres custom function with CASE
body: <p>I have table <code>watchers</code>:</p>
  
  <pre><code>------------------------------------------
  time_type | time_count | timestamp_created
  ------------------------------------------
    'hour'  |   1        |   2016-12-08 15:56:26.169614
    'hour'  |   13       |   ... 
    'day'   |   5        |   ...
  </code></pre>
  
  <p>I try to get integer based on <code>time_type</code> and <code>time_count</code> values:</p>
  
  <pre><code>CREATE OR REPLACE FUNCTION af_calculate_range(tt text,tc  integer) RETURNS integer AS $$ 
              SELECT tt, CASE tt WHEN 'day' THEN tc * 60 * 60 
                                 WHEN 'hour' THEN tc * 60
                         END
          FROM watchers;
  
            $$
      LANGUAGE SQL;
  
  SELECT af_calculate_range(time_type, time_count) FROM watchers
  </code></pre>
  
  <p>but I get an error:</p>
  
  <pre><code>ERROR:  return type mismatch in function declared to return integer  
  DETAIL:  Final statement must return exactly one column. 
  CONTEXT:  SQL function "af_calculate_range"
  ********** Error **********
  </code></pre>
  
  <p>Usage:</p>
  
  <pre><code>SELECT * FROM watchers
  WHERE  EXTRACT(EPOCH FROM now()) &gt; (EXTRACT(EPOCH FROM timestamp_created) +
   af_calculate_range(time_type, time_count) )
  </code></pre>
  
  <p>if <code>time_type</code> = <code>'hour'</code> and <code>time_count</code> = <code>1</code> the output should be 3600 seconds.</p>
  
  <p>What's wrong with my example:</p>
  
  <p>I used <a href="https://www.postgresql.org/docs/7.4/static/functions-conditional.html" rel="nofollow noreferrer">https://www.postgresql.org/docs/7.4/static/functions-conditional.html</a></p>
  
  <p>and <a href="https://www.postgresql.org/docs/9.1/static/sql-createfunction.html" rel="nofollow noreferrer">https://www.postgresql.org/docs/9.1/static/sql-createfunction.html</a></p>