正在做彩票统计分析,想得到如下结果:
表1
id haoma
1 2
2 1
3 2
4 2
5 2
6 3
7 2
8 2当前号码2的连出次数2次,最大连出次数3次意思就是,当前开奖了8期,号码2最大连出了3次,即id是3、4、5的时候连出了3次;当前连出了2次,即最新一期id8和上期id7都出了号码2。不想用union来连接,因为太多期就麻烦了。谢谢~
表1
id haoma
1 2
2 1
3 2
4 2
5 2
6 3
7 2
8 2当前号码2的连出次数2次,最大连出次数3次意思就是,当前开奖了8期,号码2最大连出了3次,即id是3、4、5的时候连出了3次;当前连出了2次,即最新一期id8和上期id7都出了号码2。不想用union来连接,因为太多期就麻烦了。谢谢~
数字每次出现后就+1
create table tb(id int, haoma int)
insert tb select 1, 2
insert tb select 2, 1
insert tb select 3, 2
insert tb select 4, 2
insert tb select 5, 2
insert tb select 6, 3
insert tb select 7, 2
insert tb select 8, 2
go--定义要统计的号码
declare @haoma int
set @haoma = 2;with t1 as
(
select rid = row_number() over(order by id),gid = row_number() over(partition by haoma order by id),* from tb
)
,t2 as
(
select gid = rid - gid, id, haoma from t1
)
,t3 as
(
select haoma,maxid = max(id), cn = count(*) from t2 group by gid,haoma-- order by id
)
select name = '当前连出次数',cn from t3 where maxid = (select max(maxid) from t3 where haoma = @haoma)
union all
select name = '最大连出次数',max(cn) from t3 where haoma = @haoma/*
name cn
------------ -----------
当前连出次数 2
最大连出次数 3(2 行受影响)
*/
记得结贴哦,楼主