title: PostgreSQL: how to convert "PIVOT" from T-SQL to PLPGSQL procedure
body: <p>I would like to convert a stored procedure from T-SQL to PL/PGSQL.
  Here is the T-SQL code</p>
  <pre><code>SELECT NO_PTF, coalesce(1, 0) as RISK_1, coalesce(2, 0) as RISK_2, coalesce(3, 0) as RISK_3, coalesce(4, 0) as RISK_4
      FROM (
              SELECT D.NO_PTF, C.NB_DEG_RSQ, D.MT_DEM/100 as MT_DEM
                  FROM public.TB_Demande      D
                      INNER JOIN
                      public.TB_Compartiment C ON D.ID_CPA = C.ID_CPA
                                                  AND D.MC_UTL = 'F3000'
                                                  AND C.IN_ACT = true
              ) Q
      PIVOT (SUM(Q.MT_DEM) FOR Q.NB_DEG_RSQ IN (1, 2, 3, 4)) as PVT
      )
  </code></pre>
  <p>I tried to replace PIVOT by crosstab. But i have an error. Here is my PL/PGSQL code</p>
  <pre><code>SELECT NO_PTF, coalesce(1, 0) as RISK_1, coalesce(2, 0) as RISK_2, coalesce(3, 0) as RISK_3, coalesce(4, 0) as RISK_4
      FROM (
              SELECT D.NO_PTF, C.NB_DEG_RSQ, D.MT_DEM/100 as MT_DEM
                  FROM public.TB_Demande      D
                      INNER JOIN
                      public.TB_Compartiment C ON D.ID_CPA = C.ID_CPA
                                                  AND D.MC_UTL = 'F3000'
                                                  AND C.IN_ACT = true
              ) Q
      crosstab (SUM(Q.MT_DEM) FOR Q.NB_DEG_RSQ IN (1, 2, 3, 4)) as PVT
  </code></pre>
  <p>Someone have an idea about the rigth way to use crosstab here?</p>