I have a single table (Details) with fields “system” and “color_code”. I want to calculate the percent of items by color_code, for each system with a single SQL statement. I have this:
SELECT system, color_code, Count(color_code) AS ColorCount,
Count(color_code)*100 /(select count(*) from Detail) AS ColorPercent
FROM Detail
GROUP BY system, color_code;
The result in “ColorPercent” is of all records. I want the “ColorPercent” to be based on the number of records of each “system”.
I tried this (adding the “GROUP BY system” to the expression), but it doesn’t work,
SELECT system, color_code, Count(color_code) AS ColorCount,
Count(color_code)*100/(select count(*) from Detail
GROUP BY system) AS ColorPercent
FROM Detail
GROUP BY system, color_code;
You need to restrict the records obtained in your sub-query.
Try:
I’m not sure if you even need to alias your table or not