怎么用sql查询出2、5、8的结果呢?
要查的结果就是查询这组数据从哪里开始不连续的。
哪位大哥帮忙解决下,谢谢!
13 个解决方案
#2
select
max(t.aid)aid
from(
select aid,cnt=aid-row_number()over(order by getdate()) from tb
)t group by cnt
max(t.aid)aid
from(
select aid,cnt=aid-row_number()over(order by getdate()) from tb
)t group by cnt
#3
create table A表(AID int)
insert into A表(AID)
select 1 union all
select 2 union all
select 4 union all
select 5 union all
select 7 union all
select 8 union all
select 10
with t as
(select AID,row_number() over(order by AID) 'rn'
from A表
)
select a.AID
from t a
left join t b on a.rn=b.rn-1
where b.rn is not null and a.AID<>b.AID-1
/*
AID
-----------
2
5
8
(3 row(s) affected)
*/
#4
能不能不用row_number()over搞定呢?刚才的方法把10也查出来了。
#5
不用row_number()的方法.
create table A表(AID int)
insert into A表(AID)
select 1 union all
select 2 union all
select 4 union all
select 5 union all
select 7 union all
select 8 union all
select 10
select identity(int,1,1) 'rn',AID
into #t
from A表
select a.AID
from #t a
left join #t b on a.rn=b.rn-1
where b.rn is not null and a.AID<>b.AID-1
/*
AID
-----------
2
5
8
(3 row(s) affected)
*/
#6
能不能不用临时表,我的数据库不支持这种复杂的SQL,可以用表连接,不好意思。
#7
啥数据库临时表 row_number都不支持?
数据量少的情况下
用master..spt_values吧 如果权限不足也会有问题
#8
sqlce,很多sqlserver的函数都不能用。
#9
表与自己关联
select id from a t1 where not exists( select * from a t2 where t2.id=t1.id+1 )
再去掉最大值
select id from a t1 where not exists( select * from a t2 where t2.id=t1.id+1 )
再去掉最大值
#10
create table A(AID int)
insert into A(AID)
select 1 union all
select 2 union all
select 4 union all
select 5 union all
select 7 union all
select 8 union all
select 10
select aid
from
(
select a.aid,
(select min(aid) from a aa where aa.aid > a.aid) min_aid
from A
)a
where aid +1 < min_aid
/*
aid
2
5
8
*/
#11
可以。
#12
貌似关联子查询就能实现。例如楼上的语句。
#13
#1
#2
select
max(t.aid)aid
from(
select aid,cnt=aid-row_number()over(order by getdate()) from tb
)t group by cnt
max(t.aid)aid
from(
select aid,cnt=aid-row_number()over(order by getdate()) from tb
)t group by cnt
#3
create table A表(AID int)
insert into A表(AID)
select 1 union all
select 2 union all
select 4 union all
select 5 union all
select 7 union all
select 8 union all
select 10
with t as
(select AID,row_number() over(order by AID) 'rn'
from A表
)
select a.AID
from t a
left join t b on a.rn=b.rn-1
where b.rn is not null and a.AID<>b.AID-1
/*
AID
-----------
2
5
8
(3 row(s) affected)
*/
#4
能不能不用row_number()over搞定呢?刚才的方法把10也查出来了。
#5
不用row_number()的方法.
create table A表(AID int)
insert into A表(AID)
select 1 union all
select 2 union all
select 4 union all
select 5 union all
select 7 union all
select 8 union all
select 10
select identity(int,1,1) 'rn',AID
into #t
from A表
select a.AID
from #t a
left join #t b on a.rn=b.rn-1
where b.rn is not null and a.AID<>b.AID-1
/*
AID
-----------
2
5
8
(3 row(s) affected)
*/
#6
能不能不用临时表,我的数据库不支持这种复杂的SQL,可以用表连接,不好意思。
#7
啥数据库临时表 row_number都不支持?
数据量少的情况下
用master..spt_values吧 如果权限不足也会有问题
#8
sqlce,很多sqlserver的函数都不能用。
#9
表与自己关联
select id from a t1 where not exists( select * from a t2 where t2.id=t1.id+1 )
再去掉最大值
select id from a t1 where not exists( select * from a t2 where t2.id=t1.id+1 )
再去掉最大值
#10
create table A(AID int)
insert into A(AID)
select 1 union all
select 2 union all
select 4 union all
select 5 union all
select 7 union all
select 8 union all
select 10
select aid
from
(
select a.aid,
(select min(aid) from a aa where aa.aid > a.aid) min_aid
from A
)a
where aid +1 < min_aid
/*
aid
2
5
8
*/
#11
可以。
#12
貌似关联子查询就能实现。例如楼上的语句。