sqlpostgresqlnullrow-value-expression

Exclude rows with null values


I'm doing a bunch of sum queries:

SELECT col1 + col2 + col3 + ...

Some of the values in some of the columns are null. I'm checking for them by doing:

SELECT CASE WHEN col1 is not null and col2 is not null and ...

I wonder if there is a more concise syntax for accomplishing this task.


Solution

  • Well, since the sum of any number and null is null, you can do something like the following (with the obvious ... filled in):

    select big_honking_sum
    from(
        select col1+col2+col3+...+coln as big_honking_sum
        from some_table
    )sum
    where big_honking_sum is not null;