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

前端 未结 7 1730
臣服心动
臣服心动 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:26

    Ugly solution:

    select Col1, Col2,
           case when Col1 is null then 1 else 0 end
         + case when Col2 is null then 1 else 0 end
         as Col3
    from (
    
    select 'A' as Col1, 'B' as Col2
    union select 'A', NULL
    union select NULL, NULL
    
    ) z
    

    This returns

    Col1 Col2 Col3
    NULL NULL 2
    A    NULL 1
    A    B    0
    
    0 讨论(0)
提交回复
热议问题