How do I get the count of null value columns per row in a return set?

前端 未结 7 1739
臣服心动
臣服心动 2021-01-21 17:46

I\'m looking for a query which will return me an extra column at the end of my current query which is the count of all columns within the return set which contain a null column.

7条回答
  •  孤街浪徒
    2021-01-21 18:01

    As in a similar post, SQL is not very suited to work across different columns within a row, but muach better on working across rows.

    I'd suggest to turn the table into 'individual' facts about a row, e.g.

    select , col1 as value From aTable
    UNION
    select , col2 as value From aTable
    UNION
    ... and so on for the other columns to be summed.
    

    This can be turned into a view i.e.

    create view aView as (select as above).
    

    Then the correct answer is just

    select key, count(*)
    from aView
    where value is null
    Group By key
    

提交回复
热议问题