Stack Exchange
How to calculate the percentage in a PIVOT
I'm trying to calculate the percentage of type of vehicles per departement in a pivot table. I did it with a count :
    SELECT
    *
FROM
    (
    SELECT
        CASE
            WHEN TYPE_V = 'N1' THEN 'CAR'
            WHEN TYPE_INCIDENT IN ('N2',
            'N3') THEN 'BUS'
        END TYPE_VEH,
        DEPARTEMENT
    FROM
        DWH
    WHERE
        CODE = 'YES' ) PIVOT (COUNT(DEPARTEMENT)FOR DEPARTEMENT IN ('D1' AS D1,
    'D2' AS D2,
    ) )

Result: 
===========================
TYPE_VEH        D1      D2
===========================
    CAR        2        8
    BUS        2        2

What I want is this :
===========================
TYPE_VEH        D1      D2
===========================
    CAR        20%      80%
    BUS        50%      50% 


You can use
SELECT TYPE_VEH, D1 / (D1+D2) as D1, D2 / (D1+D2) as D2 FROM ...

instead of
SELECT * FROM


My answer returns the percentages as numbers whereas the other answers return strings. Depending on the use case you might prefere the one or other solution...

You need aggregation to get percentages, I suggest you to use window analytic function to get aggregation within the subquery without need of grouping. 
SELECT TYPE_VEH, ( D1 / TOT ) * 100||'%' AS D1, ( D2 / TOT ) * 100||'%' AS D2               
  FROM (SELECT CASE
                 WHEN TYPE_V = 'N1' THEN
                  'CAR'
                 WHEN TYPE_INCIDENT IN ('N2', 'N3') THEN
                  'BUS'
              END TYPE_VEH,
              DEPARTMENT,
              COUNT(*) OVER (PARTITION BY CASE WHEN TYPE_V = 'N1' THEN 1 ELSE 0 END 
                             ORDER BY 1) TOT
         FROM DWH
        WHERE CODE = 'YES') 
 PIVOT(COUNT(DEPARTMENT) FOR DEPARTMENT IN('D1' AS D1,'D2' AS D2));

Demo

I would just use conditional aggregation:
SELECT TYPE_VEH,
       AVG(CASE WHEN DEPARTMENT = 'D1' THEN 100.0 ELSE 0 END) as D1,
       AVG(CASE WHEN DEPARTMENT = 'D2' THEN 100.0 ELSE 0 END) as D2
FROM (SELECT (CASE WHEN TYPE_V = 'N1' THEN 'CAR'
                   WHEN TYPE_INCIDENT IN ('N2', 'N3') THEN 'BUS'
              END) AS TYPE_VEH,
              DWH.*
      FROM DWH
      WHERE CODE = 'YES'
     ) D
GROUP BY TYPE_VEH;

Normally, you would want this value as a number.  But if you want it formatted as a string, you can appear a percent time by using || '%'.

I assume you don't even need PIVOT for this.
You can directly get this output from table DW_DECSI2.DWH_DSI_F_PLAN_PERFORMANCE as follows:
SELECT
    DEPARTEMENT,
    (D1 * 100) / ( D1 + D2 ) || '%' AS D1,
    (D2 * 100) / ( D1 + D2 ) || '%' AS D2
FROM
    (
        SELECT
            DEPARTEMENT,
            SUM(CASE
                WHEN TYPE_V = 'N1' THEN 1
                ELSE 0
            END) AS D1,
            SUM(CASE
                WHEN TYPE_V <> 'N1'
                     AND TYPE_INCIDENT IN(
                    'N2', 'N3'
                ) THEN 1
                ELSE 0
            END) AS D2
        FROM
            DW_DECSI2.DWH_DSI_F_PLAN_PERFORMANCE
        WHERE
            CODE = 'YES'
        GROUP BY
            DEPARTEMENT
    );

Cheers!!

A quick google search for DW_DECSI2 finds that this answer is the only reference for it on the web. Same thing with DWH_DSI_F_PLAN_PERFORMANCE. While this is an impressive achievement on its own, it means that there's no additional information to help users make sense of this answer. Would you mind explaining what that table is?
