Skip to content
Advertisement

Columns ID, update, project. I want a pie chart where there are percentages of updates of different projects in database for particular employee

I have used this query but it gives me percentage of only single project.

SELECT 
    100.0 * COUNT(CASE WHEN Proj = 'abc' AND ID = '101' THEN Update END) /
       COUNT(CASE WHEN ID = '101' THEN proj END) AS percentage 
FROM Updates;

Advertisement

Answer

It is a little unclear what you want. If you want the proportion within a project:

SELECT Proj,
       AVG(CASE WHEN ID = '101' THEN 100.0 ELSE 0 END) as percentage
FROM Updates
GROUP BY Proj;

If you want the proportion over all projects:

SELECT Proj,
       COUNT(*) * 100.0 / SUM(COUNT(*)) OVER () as percentage
FROM Updates
WHERE ID = 101
GROUP BY Proj;
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement