我有以下架构,并为此问题添加了代表性示例数据:
Oracle 11g R2 模式设置:
create table foo(id integer, att1 varchar(9), att2 varchar(9), from_at date);
insert into foo(id, att1, att2, from_at) values(1, 'J', null, sysdate-5);
insert into foo(id, att1, att2, from_at) values(1, null, 'L', sysdate-4);
insert into foo(id, att1, att2, from_at) values(1, 'B', null, sysdate-3);
insert into foo(id, att1, att2, from_at) values(1, 'C', null, sysdate-2);
insert into foo(id, att1, att2, from_at) values(1, null, 'H', sysdate-1);
insert into foo(id, att1, att2, from_at) values(1, 'A', 'H', sysdate);
insert into foo(id, att1, att2, from_at) values(2, null, 'H', sysdate-1);
insert into foo(id, att1, att2, from_at) values(2, 'A', null, sysdate);
| ID | ATT1 | ATT2 | FROM_AT |
----------------------------------------------------------
| 1 | J | (null) | December, 01 2012 15:13:42+0000 |
| 1 | (null) | L | December, 02 2012 15:13:42+0000 |
| 1 | B | (null) | December, 03 2012 15:13:42+0000 |
| 1 | C | (null) | December, 04 2012 15:13:42+0000 |
| 1 | (null) | H | December, 05 2012 15:13:42+0000 |
| 1 | A | H | December, 06 2012 15:13:42+0000 |
| 2 | (null) | H | December, 05 2012 15:13:42+0000 |
| 2 | A | (null) | December, 06 2012 15:13:42+0000 |
我希望能够显示每一行,但用att1
和替换为 thatatt2
的最新值id
。null
属性表示与上次插入行时没有变化的值,非属性null
表示新值。输出应如下所示:
| ID | ATT1 | ATT2 |
------------------------
| 1 | J | (null) |
| 1 | J | L |
| 1 | B | L |
| 1 | C | L |
| 1 | C | H |
| 1 | A | H |
| 2 | (null) | H |
| 2 | A | H |