Use previous row value when a value is 0

Database
N/A

Use previous row value when a value is 0

Hi everyone I have a doubt with an excersice in Teradata. So im asking for help.

I have a tabla like this one

ID, calendar_date, value
1. 01/01/2000 100
1. 01/01/2000 0
1. 01/01/2000 200
1. 01/01/2000 0
1. 01/01/2000 300


What I neee is when a value is 0 then take the previous value, obviously different to 0.

Output needed:

ID, calendar_date, value
1. 01/01/2000 100
1. 01/01/2000 100
1. 01/01/2000 200
1. 01/01/2000 200
1. 01/01/2000 300

Thanks.
2 REPLIES
Teradata Employee

Re: Use previous row value when a value is 0

It looks like there must be other attributes here that you are not showing.  You use the term "previous," but what defines the order?

N/A

Re: Use previous row value when a value is 0

You can change 0 to NULL and then apply LAST_VALUE:

last_value(nullif(value,0) ignore nulls)
over (partition by id
      order by whatever_determines_order)