Need help in sql

Database
Enthusiast

Need help in sql

I have a table where the column values are like below::

 

Col1
1
1
-1
0
2
1
2
-2
3
2
-3

 

I want to have the output like whenever it has any positive value it shuld display as it is but if it is negetive value then the value should be replaced with the previous row's value like below:

 

Col1
1
1
1
0
2
1
2
2
3
2

2

 

Thanks,

Taniya

1 REPLY 1
Ambassador

Re: Need help in sql

Assuming there's a way order the data:

last_value(case when col >= 0 then col end ignore nulls)
over (-- partition by ??
      order by column_to_order_your_data
      rows unbounded preceding)