SQL SELECT Statement
My table looks like this -
EFF_DATE ID Col_A Col_B Col_C
01/01/1900 1122334 N N N
12/01/2010 1122334 NULL Y Y
12/02/2010 1122334 Y NULL NULL
12/21/2010 1122334 NULL NULL NULL
01/01/1900 44555222 N N N
12/02/2010 44555222 NULL NULL NULL
01/01/1900 897969595 N N N
11/22/2010 897969595 Y NULL NULL
01/01/1900 897969596 N N N
11/22/2010 897969596 Y NULL NULL
Now, I need to populate all the columns for every record such that if for a date Col_A is NULL, it should populate the the value of Col_A for the same Id from the previous eff_date.
This is the solution I want -
EFF_DATE ID Col_A Col_B Col_C
01/01/1900 01122334 N N N
12/01/2010 01122334 N Y Y
12/02/2010 01122334 Y Y Y
12/21/2010 01122334 Y Y Y
01/01/1900 044555222 N N N
12/02/2010 044555222 N N N
01/01/1900 897969595 N N N
11/22/2010开发者_StackOverflow中文版 897969595 Y N N
01/01/1900 897969596 N N N
11/22/2010 897969596 Y N N
Please help! I want to use simple SELECT statement to achieve this result.
in mysql
UPDATE tbL_table
JOIN (SELECT EFF_DATE, COl_A, @x:=@x+1 AS rnum FROM tbl_table) a ON a.EFF_DATE=tbl_table.EFF_DATE
JOIN (SELECT EFF_DATE, COl_A, @y:=@y+1 AS rnum FROM tbl_table) b ON b.rnum+1=a.rnum
JOIN (SELECT @x:=0, @y:=0) as vars on true
SET Col_A=IF(Col_A IS NULL, b.Col_A , Col_A)
You'll need to order by eff_date somehow but thats the basic idea of how I would do it.
精彩评论