Skip to content
Advertisement

Conditional ROW_NUMBER() to skip nulls still counting them

I am trying to add a ROW_NUMBER column to a data set that follows the order of [Withdrawal then Fee; Withdrawal then Fee] based upon whether the column NETWORKCD is null. However, my row_number formula is still counting Nulls even though it’s not displaying the numbers, like so:

I am using the following formula for PeriodCount

I was expecting PeriodCount to count [1,2,3] instead of [1,3,5], like so:

What am I missing?

Advertisement

Answer

It is still showing a value the 1st, 3rd and 5th rows – so the row count as it encounters each of those rows is correct. It’s the row number within the entire result set, not within the not-null values. Your case expression is deciding whether to show the value, not affecting how it is found.

You could get the result you want using dense_rank() instead;

… as that does suppress gaps in the generated values. From the docs:

DENSE_RANK computes the rank of a row in an ordered group of rows and returns the rank as a NUMBER. The ranks are consecutive integers beginning with 1. …

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement