• ScottPletcher (10/27/2016)


    Seems more confusing than the natural CASE statement to me:

    SUM(CASE WHEN VAL1 <> 0 THEN VAL1 ELSE VAL2 END)

    VAL1 can return NULL unfortunately so using a comparison operator won't work. We did have the code saying:

    SUM(CASE WHEN (VAL1 = 0 or VAL1 IS NULL).... but this is exactly what ISNULL(NULLIF... is useful for IMO.

    In the end I tracked this down to an error in my understanding of the source data. With multiple VAL1 and VAL2 rows returned, with some VAL1s being 0 or NULL and others not, within the same grouping, my formula was evaluating to both cases being true for the given grouping and providing a SUM of both values, which I didn't want, so my second formula actually does what's required. :crazy:

    Very grateful for the input.

    Cheers