有一个table:
ID   日期      内容
1    201208    ddd2
2    201202    
3    201203    
4    201204    
5    201205    ddd2
6    201207    ddd2数据中有以上内容
现在我想查询“日期字段”是连续的,并且连续3条“内容“字段 是空的数据;如下
2    201202    
3    201203    
4    201204    该查询语句该怎么写?

解决方案 »

  1.   


    select   日期 from  
      (select   count(*)   over   (PARTITION   by   DIFFDATE)   diffcount,id,日期
       from  
      (  
            SELECT   id,日期  ,add_months(日期   ,-ROW_NUMBER()   OVER(ORDER   BY   id,month)   )   DIFFDATE  
            FROM   tabname1  where  内容=null
      )   )where   diffcount   =   3
      

  2.   


    create table t1 (id number(10),month date,c varchar2(10));insert into t1 values (1,date'2012-08-01','ddd2');
    insert into t1 values (2,date'2012-02-01',null);
    insert into t1 values (3,date'2012-03-01',null);
    insert into t1 values (4,date'2012-04-01',null);
    insert into t1 values (5,date'2012-05-01','ddd2');
    insert into t1 values (6,date'2012-07-01','ddd2');
    insert into t1 values (7,date'2012-06-01',null);
    insert into t1 values (8,date'2012-09-01',null);
    insert into t1 values (9,date'2012-10-01',null);
    insert into t1 values (10,date'2012-12-01',null); 
    insert into t1 values (11,date'2012-11-01','222'); 
    insert into t1 values (12,date'2013-01-01',null); 
    insert into t1 values (13,date'2013-02-01',null); select distinct id,to_char(month,'yyyy-mm') month  from  
    (
             select count(*) over(partition by dif) diffcount,id,month
             from  
             (  
                      SELECT id,month ,
                             add_months(month,-row_number() over(order by month))  dif  
                      FROM  t1   
                      where c is null
              )   
    )where diffcount   =   3 
    order by month
        id    month
    --------------------------
    1 2 2012-02
    2 3 2012-03
    3 4 2012-04
    4 10 2012-12
    5 12 2013-01
    6 13 2013-02
      

  3.   

    with tb1 as (select ID ,日期 ,内容 from tb where 内容 is null order by ID) ,
    select ID ,日期 ,内容 
    from tb1
    where add_months(日期,rownum)in
    (select add_months(日期,rownum)
    from tb1 
    group by add_months(日期,rownum)
    where count(1) =3)
      

  4.   

    借用一下2楼数据create table t1 (id number(10),month date,c varchar2(10));insert into t1 values (1,date'2012-08-01','ddd2');
    insert into t1 values (2,date'2012-02-01',null);
    insert into t1 values (3,date'2012-03-01',null);
    insert into t1 values (4,date'2012-04-01',null);
    insert into t1 values (5,date'2012-05-01','ddd2');
    insert into t1 values (6,date'2012-07-01','ddd2');
    insert into t1 values (7,date'2012-06-01',null);
    insert into t1 values (8,date'2012-09-01',null);
    insert into t1 values (9,date'2012-10-01',null);
    insert into t1 values (10,date'2012-12-01',null); 
    insert into t1 values (11,date'2012-11-01','222'); 
    insert into t1 values (12,date'2013-01-01',null); 
    insert into t1 values (13,date'2013-02-01',null); 
     select id, month, c
       from (select id,
                    month,
                    c,
                    count(1) over(partition by add_months(month, -rownum)) cn
               from t1
              where c is null)
      where cn = 3
             ID MONTH       C
    ----------- ----------- ----------
              2 2012-2-1    
              3 2012-3-1    
              4 2012-4-1    
             10 2012-12-1   
             12 2013-1-1    
             13 2013-2-1  
      

  5.   

    select ID,日期
    from (
    select ID, add_months(日期, level)
    from table
    where 内容 is null
    connec by level <=3
    )