Search code examples
sqlms-access

SQL query calculate percent true from 10 boolean columns


I have a table called "Grades" with 10 boolean columns called PassA, PassB, PassC, PassD, etc. I am trying to make a query that will give me the percent of "true" in each column?

ID   PassA  PassB   PassC
1      x      x       
2             x       x
3      x
4             x

Expected query result (or equivalent):

PassA  PassB   PassC
50%    75%     25%

It is an Access SQL statement.

I seem to be able to do it with one column (see below) but wan't to do it will all the columns in 1 query. All ideas are very welcome? Thank you!

SELECT PassA, (count(PassA(*)*100 / (Select count(*)FROM Grades} AS Total
FROM Grades
Group by PassA; 

Solution

  • As this is Access SQL:

    SELECT 
        Avg(Abs([PassA])) AS AvgA, 
        Avg(Abs([PassB])) AS AvgB, 
        Avg(Abs([PassC])) AS AvgC
    FROM 
        Grades;
    

    Then set the Format property of the fields to Percent and count of decimals to 0:

    enter image description here