请教一下,求一组数据中相同数据的间隔,如何获取?
如 id number
1 10
2 15
3 8
4 10
5 14
6 9
7 10
8 54
如上图,获取10之间的间隔是多少?
number 间隔数10 015 020 0
10 25 0855 0
824 0
866 0
855 2
897 0
758 0
824 4
855 3
321 0
258 0
952 0
855 3
801 0
824 6
921 0
824 1
555 0
855 5
100 0
后一排就是间数
如 id number
1 10
2 15
3 8
4 10
5 14
6 9
7 10
8 54
如上图,获取10之间的间隔是多少?
number 间隔数10 015 020 0
10 25 0855 0
824 0
866 0
855 2
897 0
758 0
824 4
855 3
321 0
258 0
952 0
855 3
801 0
824 6
921 0
824 1
555 0
855 5
100 0
后一排就是间数
在吗?
矢志不渝 09:27:03
恩
让缘随风 09:27:16
请教一下,求一组数据中相同数据的间隔,如何获取?
如 id number
1 10
2 15
3 8
4 10
5 14
6 9
7 10
8 54
如上图,获取10之间的间隔是多少?
矢志不渝 09:27:55
==
让缘随风 09:28:03
TKS!!!!!!!!!!
矢志不渝 09:32:27
不懂
让缘随风 09:32:28
您好,我现在有事不在,一会再和您联系。
让缘随风 09:44:29
不会吧
矢志不渝 09:44:53
不很清楚您的意思
让缘随风 09:46:03
就是一个字段里的数据,我想查找两个相同的数据之间间隔了几条数据
让缘随风 09:46:15
隔了几行
矢志不渝 09:46:57
3 3 啊
让缘随风 09:47:30
number 间隔数10 015 020 0
10 25 0
让缘随风 09:47:47
如上图,我就想获取间隔数是多少?
矢志不渝 09:48:03
写多一点
矢志不渝 09:48:13
这么几个看不出来
让缘随风 09:48:15
如果是任一数字,如何获取,因为数据很多
让缘随风 09:48:16
好
矢志不渝 09:48:24
写25个
让缘随风 09:48:44
好
让缘随风 09:51:50
855 0
824 0
866 0
855 2
897 0
758 0
824 4
855 3
321 0
258 0
952 0
855 3
801 0
824 6
921 0
824 1
555 0
855 5
100 0
让缘随风 09:52:04
后一排就是间数
让缘随风 09:52:10
间隔数
让缘随风 09:52:24
数据很多,我想自动获取
矢志不渝 09:53:16
有难度
declare @tb table (id int,number int)
insert into @tb
select 1,10 union all
select 2,15 union all
select 3,8 union all
select 4,10 union all
select 5,14 union all
select 6,9 union all
select 7,10 union all
select 8,54--select * from @tbselect a.number,
num= isnull(a.id-(select max( id) from @tb where number=a.number and id< a.id)-1 ,0)
from @tb a
order by a.id
number num
----------- -----------
10 0
15 0
8 0
10 2
14 0
9 0
10 2
54 0(8 行受影响)
--> 测试数据: @tb
declare @tb table (number int)
insert into @tb
select 855 union all
select 824 union all
select 866 union all
select 855 union all
select 897 union all
select 758 union all
select 824 union all
select 855 union all
select 321 union all
select 258 union all
select 952 union all
select 855 union all
select 801 union all
select 824 union all
select 921 union all
select 824 union all
select 555 union all
select 855 union all
select 100select *,id=identity(int,1,1) into #t from @tbselect a.number,
num= isnull(a.id-(select max( id) from #t where number=a.number and id< a.id)-1 ,0)
from #t a
order by a.id
number num
----------- -----------
855 0
824 0
866 0
855 2
897 0
758 0
824 4
855 3
321 0
258 0
952 0
855 3
801 0
824 6
921 0
824 1
555 0
855 5
100 0(19 行受影响)
insert into # select
1 , 10 union all select
2 ,15 union all select
3 ,8 union all select
4 ,10 union all select
5 ,14 union all select
6 ,9 union all select
7 ,10 union all select
8 ,54
go
with cte as
(
select *,
case when number=10 then 1 else 0 end as flag
from #
)
select
number ,间隔数=case when flag=1 then isnull((select min(ID) from cte where flag=1 and ID<k.id)+1,0) else 0 END
from cte knumber 间隔数
----------- -----------
10 0
15 0
8 0
10 2
14 0
9 0
10 2
54 0
2000 臨時表10#
(
select *,
rn=ROW_NUMBER() over(order by id ),
case when number=10 then 1 else 0 end as flag
from #
)
select
number ,间隔数=case when flag=1 then isnull((select min(rn) from cte where flag=1 and rn<k.rn)+1,0) else 0 END
from cte knumber 间隔数
----------- -----------
10 0
15 0
8 0
10 2
14 0
9 0
10 2
54 0
2000加个自增列 2005用row_number排序