请问各位,
   我现在有一些数据是用分析函数动态产生、按时间段(小时)和产品分组的,但它们现在是不连续的(因为有些时段没有数据)。
   我现在想实现这样的功能:如果当前时段没有数据,则把最相邻的上一个时段数据延续到当前时段,有什么好办法呢?
   比如说10点钟,11点钟,14点钟有数据,但12、13点钟不连续了;现在要把11点钟的数据延续到12、13点钟。
   急用,请各位多多指教。

解决方案 »

  1.   

    一个例子,假设表t1中存放的是你要查询的时间序列(12.1到12.31),t2是这些时间内的统计值,现在要查询出这个时间段内的记录,如果这天没有记录就去之前最近一天的记录
    SQL> create table t1(d date);Table createdSQL> create table t2(d date,v number);Table createdSQL> insert into t1 select trunc(sysdate,'MM')+rownum-1 from all_objects where rownum<=31;31 rows insertedSQL> insert into t2 values(to_date('2006-12-3','yyyy-mm-dd'),4231);1 row insertedSQL> insert into t2 values(to_date('2006-12-13','yyyy-mm-dd'),2432);1 row insertedSQL> insert into t2 values(to_date('2006-12-8','yyyy-mm-dd'),6013);1 row insertedSQL> insert into t2 values(to_date('2006-12-17','yyyy-mm-dd'),1044);1 row insertedSQL> insert into t2 values(to_date('2006-12-19','yyyy-mm-dd'),7695);1 row insertedSQL> insert into t2 values(to_date('2006-12-20','yyyy-mm-dd'),4586);1 row insertedSQL> insert into t2 values(to_date('2006-12-25','yyyy-mm-dd'),5907);1 row insertedSQL> insert into t2 values(to_date('2006-12-27','yyyy-mm-dd'),6828);1 row insertedSQL> commit;Commit completeSQL> select d,d1,d2,v
      2      from t1, (select d d1, lead(d-1,1,to_date('2999-12-31','yyyy-mm-dd')) over(order by d) d2, v from t2)
      3      where d1(+)<=d and d<=d2(+);D           D1          D2                   V
    ----------- ----------- ----------- ----------
    2006-12-1                           
    2006-12-2                           
    2006-12-3   2006-12-3   2006-12-7         4231
    2006-12-4   2006-12-3   2006-12-7         4231
    2006-12-5   2006-12-3   2006-12-7         4231
    2006-12-6   2006-12-3   2006-12-7         4231
    2006-12-7   2006-12-3   2006-12-7         4231
    2006-12-8   2006-12-8   2006-12-12        6013
    2006-12-9   2006-12-8   2006-12-12        6013
    2006-12-10  2006-12-8   2006-12-12        6013
    2006-12-11  2006-12-8   2006-12-12        6013
    2006-12-12  2006-12-8   2006-12-12        6013
    2006-12-13  2006-12-13  2006-12-16        2432
    2006-12-14  2006-12-13  2006-12-16        2432
    2006-12-15  2006-12-13  2006-12-16        2432
    2006-12-16  2006-12-13  2006-12-16        2432
    2006-12-17  2006-12-17  2006-12-18        1044
    2006-12-18  2006-12-17  2006-12-18        1044
    2006-12-19  2006-12-19  2006-12-19        7695
    2006-12-20  2006-12-20  2006-12-24        4586D           D1          D2                   V
    ----------- ----------- ----------- ----------
    2006-12-21  2006-12-20  2006-12-24        4586
    2006-12-22  2006-12-20  2006-12-24        4586
    2006-12-23  2006-12-20  2006-12-24        4586
    2006-12-24  2006-12-20  2006-12-24        4586
    2006-12-25  2006-12-25  2006-12-26        5907
    2006-12-26  2006-12-25  2006-12-26        5907
    2006-12-27  2006-12-27  2999-12-31        6828
    2006-12-28  2006-12-27  2999-12-31        6828
    2006-12-29  2006-12-27  2999-12-31        6828
    2006-12-30  2006-12-27  2999-12-31        6828
    2006-12-31  2006-12-27  2999-12-31        682831 rows selectedSQL>