title: How to use crosstab() with varying amount of column names? [PostgreSQL]
body: <p>I want to get this output:</p>
  <p><img src="https://i.stack.imgur.com/cScKc.png" alt="crosstab in psql" /></p>
  <p>Which I get when I run the following through psql:</p>
  <pre><code>    select month, staff_name, total FROM (
  SELECT 
      to_char(payment.payment_date, 'Month') AS month
      , staff.first_name || ' ' || staff.last_name AS staff_name
      , sum(payment.amount) AS total
  FROM 
      payment
          LEFT JOIN staff
              ON payment.staff_id = staff.staff_id
  GROUP BY
      staff_name
      , month 
      ,  date_part('month', payment.payment_date)
  ORDER BY 
      staff_name
      , date_part('month', payment.payment_date)) as x 
  \crosstabview
  </code></pre>
  <p>But instead of using the psql shell I want to use pgAdmin 4.</p>
  <p>The problem happens when I try to recreate the same output with:</p>
  <pre><code>SELECT * FROM crosstab (
              '
              select month, staff_name, total FROM (
          SELECT 
              to_char(payment.payment_date, ''Month'') AS month
              , staff.first_name || '' '' || staff.last_name AS staff_name
              , sum(payment.amount) AS total
          FROM 
              payment
                  LEFT JOIN staff
                      ON payment.staff_id = staff.staff_id
          GROUP BY
              staff_name
              , month 
              ,  date_part(''month'', payment.payment_date)
          ORDER BY 
      staff_name
      , date_part(''month'', payment.payment_date)) as x order by 1,2 ') 
      AS final_result(&quot;Month&quot; text, &quot;Name&quot; text, &quot;Total&quot; numeric); 
  </code></pre>
  <p>-&gt; I get:</p>
  <pre><code>ERROR:  return and sql tuple descriptions are incompatible
  </code></pre>
  <p>And if I change the last line to:</p>
  <pre><code>AS final_result(&quot;Month&quot; text, &quot;Name&quot; numeric, &quot;Total&quot; numeric);
  </code></pre>
  <p>I get a wrong output:</p>
  <p><a href="https://i.stack.imgur.com/GKaG7.png" rel="nofollow noreferrer">Wrong output in pgAdmin</a></p>