Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count of non-null columns in each row

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:

Where X is any value:

Column1 | Column2 | Column3 | Column4 | Count   X     |    X    |   NULL  |    X    |   3  NULL   |   NULL  |    X    |    X    |   2  NULL   |   NULL  |   NULL  |   NULL  |   0 
like image 819
AToya Avatar asked Aug 12 '13 17:08

AToya


People also ask

How do you find the number of non nulls in a data frame?

Solution: In order to find non-null values of PySpark DataFrame columns, we need to use negate of isNotNull() function for example ~df. name. isNotNull() similarly for non-nan values ~isnan(df.name) .

Does COUNT () ignores NULL?

COUNT does not include NULL values in column counts. Therefore, the number of return values for each column might differ or be less than the total number of rows returned by COUNT(*).


1 Answers

select     T.Column1,     T.Column2,     T.Column3,     T.Column4,     (         select count(*)         from (values (T.Column1), (T.Column2), (T.Column3), (T.Column4)) as v(col)         where v.col is not null     ) as Column5 from Table1 as T 
like image 177
Roman Pekar Avatar answered Oct 08 '22 10:10

Roman Pekar