I am working with Netezza SQL.
I have a table that looks like this:
CREATE TABLE MY_TABLE
(
    name VARCHAR(50),
    year INTEGER
);
    
INSERT INTO sample_table (name, year)
VALUES ('aaa', 2010);
INSERT INTO sample_table (name, year)
VALUES ('aaa', 2011);
INSERT INTO sample_table (name, year)
VALUES ('aaa', NULL);
INSERT INTO sample_table (name, year)
VALUES ('aaa', 2013);
INSERT INTO sample_table (name, year)
VALUES ('aaa', NULL);
INSERT INTO sample_table (name, year)
VALUES ('bbb', 2000);
INSERT INTO sample_table (name, year)
VALUES ('bbb', 2001);
INSERT INTO sample_table (name, year)
VALUES ('bbb', NULL);
INSERT INTO sample_table (name, year)
VALUES ('bbb', 2003);
INSERT INTO sample_table (name, year)
VALUES ('bbb', NULL);
   name year
1   aaa 2010
2   aaa 2011
3   aaa NULL
4   aaa 2013
5   aaa NULL
6   bbb 2000
7   bbb 2001
8   bbb NULL
9   bbb 2003
10  bbb NULL
My Question: For each set of NAMES, for rows where the YEAR is NULL - I want to replace those rows with the value of YEAR from the row directly above.
The final answer would look something like this:
   name year
1   aaa 2010
2   aaa 2011
3   aaa 2012
4   aaa 2013
5   aaa 2014
6   bbb 2000
7   bbb 2001
8   bbb 2002
9   bbb 2003
10  bbb 2004
I tried the following SQL code:
UPDATE my_table
SET Year = LAST_VALUE(Year IGNORE NULLS) OVER (PARTITION BY NAME ORDER BY Year ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) + 1
WHERE Year IS NULL;
But I got the following error: Cannot use window aggregates in UPDATE
Can someone please show me how to fix this? Is there another way to do this using Netezza SQL functions?
Thanks!
References:
 
     
    