SQL语句实现批量数据的提取,难度高,分数好
需要一条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语句来实现,在线急等。
[最优解释]
--> 测试数据: @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)
*/