Question

I have a table that has checkbox columns (about 10-15) as shown below:

Race  |  Disability  |  Sexual Harrassment  |  ......  

Yes         No               Yes               
No          Yes              Yes

I wanted a query result with the totals like:

Basis   |           Total
Race                  1
Disability            1
Sexual Harrassment    2

I don't know how I can get different columns into one column, I'm guessing it is transpose, I'm not sure how to use it though.

Was it helpful?

Solution

You need a UNION query to unpivot your data, something like this

    SELECT 
        'Race' AS Basis,
        [Race] AS Response
    FROM YourTable
UNION ALL
    SELECT 
        'Disability' AS Basis,
        [Disability] AS Response
    FROM YourTable
UNION ALL
    SELECT 
        'Sexual Harrassment' AS Basis,
        [Sexual Harrassment] AS Response
    FROM YourTable

Save that query in Access as [MyDataUnpivoted] and then produce the sums like so

SELECT
    Basis,
    SUM(IIf(Response=True,1,0)) AS Total
FROM MyDataUnpivoted
GROUP BY Basis

OTHER TIPS

To the best of my knowledge, there's no easy way to go from columns to rows, usually people are trying to go from rows to columns. The only way I can think to do this would be to loop through the Table collection and place the field names into an array, and then create a new table and write the contents of the array into it as records.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top