需要一条SQL如下,
表名TEST
字段A
A中有如1,2,3,NULL等值,示例记录如下:
A
1
2
NULL
2
2
NULL
1
1
NULL从一条不为NULL的数据开始如1,2,NULL,如果这几条数据中包含1,则1这条数据都不显示
如果为2,2,NULL,都显示
如果为1,1,NULL,这三条数据都不显示只能用SQL语句来实现,在线急等。

解决方案 »

  1.   

    就是问你的test表是否有固定的顺序
      

  2.   

    提供思路.如何做自己整.
    简单的说把每段NULL的隔开分组.然后直接查组中不为1的以及组中为NULL的 ,同时限定组中分组最大值有>1的
      

  3.   


    --> 测试数据: @T
    declare @T table (A int)
    insert into @T
    select 1 union all
    select 2 union all
    select null union all
    select 2 union all
    select 2 union all
    select null union all
    select 1 union all
    select 1 union all
    select null union all
    select 3 union all
    select 1 union all
    select 2 union all
    select null union all
    select 2 union all
    select 1 union all
    select null ;with maco as
    (
    select row_number() over (order by (select 1)) as id,* from @T 
    ),list as (
    select 
    a.id,
    isnull(ltrim(a.A),' ')+isnull(ltrim(b.A),' ')+isnull(ltrim(c.A),' ') as col
    from maco a 
    left join maco b on a.id=b.id-1
    left join maco c on b.id=c.id-1 
    )
    ,m3 as
    (
    select id from list where col='11 '
    )
    select A from maco where id not in 
    (
    select id from m3
    union all
    select id-1 from m3
    union all
    select id+1 from m3
    )
    and isnull(A,'')<>1/*
    A
    -----------
    2
    NULL
    2
    2
    NULL
    3
    2
    NULL
    2
    NULL(10 row(s) affected)
    */