Skip to content
Advertisement

How to repeat previous row values until another field changes

I am trying to repeat a value from one row if there is no change between a column value in the row before. Thanks in advance!

And the expected result I am looking for is

here

in the “WantedValue” column. So if Floor is the same as the row before it, use the CheckInTime value from the row before. But this needs to carry down through multiple rows potentially.

Advertisement

Answer

I am interpreting the question as you want the minimum date/time every time the floor changes.

Use lag() and a cumulative sum to define the groups. Then use a window function to “spread” the minimum value:

Here is a db<>fiddle.

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