根据另一列的Previous值填充null Column的Value

时间:2013-03-05 03:47:48

标签: sql oracle

--------------------------------------
code      Start DT   End date  Status
--------------------------------------
Y68 25/OCT/11   25/OCT/11   ADM_CHECKD
Y68 25/OCT/11   25/OCT/11   SATISFIED
Y68 25/OCT/11   25/OCT/11   SATISFIED
Y68 25/OCT/11   31/OCT/11   SATISFIED
Y68         SATISFIED

我的要求是,如果最后一行的开始日期为空,则其值应为31 / oct / 11。这是前一行的结束日期值。

提前致谢。

1 个答案:

答案 0 :(得分:1)

如果前一行的意图是按照来源代码的结束日期排序最后一行,则可以使用:

SQL> select code,
  2         nvl(start_dt, lag(end_dt, 1) over (partition by code order by end_dt)) start_dt,
  3         end_dt, status
  4    from data
  5   order by start_dt;

COD START_DT  END_DT    STATUS
--- --------- --------- ----------
Y68 28-FEB-13 01-MAR-13 ADM_CHECKD
Y68 01-MAR-13 02-MAR-13 SATISFIED
Y68 02-MAR-13 03-MAR-13 SATISFIED
Y68 03-MAR-13 04-MAR-13 SATISFIED
Y68 04-MAR-13           SATISFIED

例如: http://sqlfiddle.com/#!4/abe8f/1