title: Convert rows to columns dynamically in PostgreSQL
body: <p>I want to convert the SQL statement of picture 1 to have the result in picture 2.</p>
  <p>Some constraints: it has to be dynamic so hardcoding the names in the statement would not be good. One other row might have 5 people assigned to it instead of 3.</p>
  <p>Output of SQL result:</p>
  <p><img src="https://i.stack.imgur.com/qtn5F.png" alt="Output of SQL result" /></p>
  <p>SQL Statement producing the result shown in picture 1:</p>
  <pre><code>select 
      t.*, e.*, 
      p.person_days, m.net_md_rate_eur 
  from 
      tpl_tasks t 
  join 
      person_days p on t.id = p.id 
  join 
      employees e on e.employee_id = p.employee_id 
                  and e.type != 'External' 
  join 
      md_rates m on e.bu_or_it = m.type 
  union
  select 
      t.*, e.*, 
      p.person_days, m.net_md_rate_eur 
  from 
      tpl_tasks t 
  join
      person_days p on t.id = p.id 
  join 
      employees e on e.employee_id = p.employee_id 
                  and e.type = 'External' 
  join 
      md_rates_externals m on e.employee_id = m.employee_id 
  order by 
      tpl_project
  </code></pre>
  <p>Desired result:</p>
  <p><img src="https://i.stack.imgur.com/aXEl7.png" alt="Desired SQL Result" /></p>
  <p>Any ideas are helpful.</p>