문제

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.

도움이 되었습니까?

해결책

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

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top