Count of non-null columns in each row

后端 未结 3 1445
一向
一向 2020-12-05 07:22

I have a table that contains 4 columns and in the 5th column I want to store the count of how many non-null columns there are out of the previous 4. For example:

Whe

3条回答
  •  孤街浪徒
    2020-12-05 07:50

    SELECT   Column1,
             Column2,
             Column3,
             Column4,
             CASE WHEN Column1 IS NOT NULL THEN 1 ELSE 0 END + 
             CASE WHEN Column2 IS NOT NULL THEN 1 ELSE 0 END + 
             CASE WHEN Column3 IS NOT NULL THEN 1 ELSE 0 END + 
             CASE WHEN Column4 IS NOT NULL THEN 1 ELSE 0 END AS Column5
    FROM     Table
    

提交回复
热议问题