title: Dynamically execute query using the output of another query
body: <p>I have a function called generate_table, that takes 2 input parameters (<code>rundate::date</code> and <code>branch::varchar</code>)</p>
  
  <p>Now I am trying to work on a second function, using PLPGSQL, that will get a list of all branches and the newest date for each branch and pass this as a parameter to the generate_table function.</p>
  
  <p>The query that I have is this:</p>
  
  <pre><code>select max(rundate) as rundate, branch
  from t_index_of_imported_files
  group by branch
  </code></pre>
  
  <p>and it results on this:</p>
  
  <pre><code>rundate;branch 
  2014-03-13;branch1
  2014-03-12;branch2
  2014-03-10;branch3
  2014-03-13;branch4
  </code></pre>
  
  <p>and what I need is that the function run something like this</p>
  
  <pre><code>select generate_table('2014-03-13';'branch1');
  select generate_table('2014-03-12';'branch2');
  select generate_table('2014-03-10';'branch3');
  select generate_table('2014-03-13';'branch4');
  </code></pre>
  
  <p>I've been reading a lot about PLPGSQL but so far I can only say that I barely know the basics.</p>
  
  <p>I read that one could use a concatenation in order to get all the values together and then use a EXECUTE within the function, but I couldn't make it work properly. </p>
  
  <p>Any suggestions on how to do this? </p>