title: Stored procedure to return count
body: <p>I am trying to make a stored procedure for the query I have:</p>
  
  <pre><code>SELECT count(DISTINCT account_number)
  from account
  NATURAL JOIN branch
  WHERE branch.branch_city='Albany';
  </code></pre>
  
  <p>or </p>
  
  <pre><code>SELECT count(*)
  from (
     select distinct account_number
     from account
     NATURAL JOIN branch
     WHERE branch.branch_city='Albany'
     ) as x;
  </code></pre>
  
  <p>I have written this stored procedure but it returns count of all the records in column not the result of query plus I need to write stored procedure in plpgsql not in SQL.</p>
  
  <pre><code>CREATE FUNCTION account_count_in(branch_city varchar) RETURNS int AS $$
     PERFORM DISTINCT count(account_number) from (account NATURAL JOIN branch)
     WHERE (branch.branch_city=branch_city); $$ LANGUAGE SQL;
  </code></pre>
  
  <p>Help me write this type of stored procedure in plpgsql which returns returns the number of accounts managed by branches located in the specified city.</p>