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)
*/
[其他解释]
提供思路.如何做自己整.
简单的说把每段NULL的隔开分组.然后直接查组中不为1的以及组中为NULL的 ,同时限定组中分组最大值有>1的
[其他解释]
把整个表结构贴出来,是否可有不打乱A的排序字段
[其他解释]
是否可有不打乱A的排序字段
什么意思?
[其他解释]
就是问你的test表是否有固定的顺序
[其他解释]
这个问题 好像只能一个个取出来判断是否该显示了 ,按集合操作比较费劲 坐等大神.
[其他解释]
帮你顶顶!!
[其他解释]
好难啊 我弄不出来
[其他解释]
这是这么弄的。后来在程序里做了。
[其他解释]
帮忙顶~!没 弄出来
[其他解释]
才学疏浅,看了半天才看懂需求。。帮顶。。