Skip to content
Advertisement

How to use Lead() and Lag() to get the first non null value

I have the following query

and returns this table:

now, what I’m trying to do is to calculate the lead of the first non null value on the field. for example, for event_id = 138466815 the columnCount is 85. I want the “lead1” field to show 82.53, which is the next non null value below. and that then later, for event_id = **138386464 ** “lead 1 to show 57.45 (the next not null value below).

I’m not sure if I should pass a parameter to the offset parameter of the lead function, but I don’t know which one should be, maybe something with Coalesce? I wasn’t able to do it.

just to put it a bit more clear, this is what I expect the lead1 column to show:

Also, I’m not sure if there is a way to count the amount of rows between the first not null value in “columnCount” and the second not null value. for example to have 7 as the first count, 3 as the second and 1 as the third.

Advertisement

Answer

You seem to want the next value where columnCount = 1, so partition by that column:

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