How to get number of false in every column of a table?

后端 未结 3 909
离开以前
离开以前 2021-01-21 00:52

I have a table say T_myTable it has 5 columns and all have some values either true or false.

--------------------------------
col1 | col2 | col3 | col4 | col5
--         


        
3条回答
  •  说谎
    说谎 (楼主)
    2021-01-21 01:34

    One of the most interesting way is to:

    1. unpivot data to be able to filter data
    2. pivot data again to get count of 'false'

    Check this:

    SELECT [col1],[col2],[col3],[col4],[col5]
    FROM (
        SELECT MyVal, ColName
        FROM (
            SELECT *
            FROM T_myTable
            ) AS pvt
        UNPIVOT(MyVal FOR ColName IN ([col1],[col2],[col3],[col4],[col5])) AS unpvt
        WHERE MyVAl = 'false'
        ) As DT
    PIVOT (COUNT(MyVal) FOR ColName IN ([col1],[col2],[col3],[col4],[col5])) AS PT
    

    Result:

    col1    col2    col3    col4    col5
    2       3       4       1       4
    

提交回复
热议问题