急求一条sql语句 或者存储过程

chang1216 2009-04-29 09:10:01
我现在有这样一个表
开奖日期 ,中奖号码
20070401-01 58105
20070401-02 48338
20070401-03 30353
20070401-04 24579
20070401-05 13924
20070401-06 52309
20070401-07 48517
20070401-08 38172
20070401-09 20320
20070401-10 14304
我想要的 是对位纵向查询历史数据,以最近一期开奖数据为基准数据(如上20070401-10 14304),查询以任意一位上的数字为查询依据或对位以连续上两期、三期或依次对位连续十期为查询数据组为依据,查询与依据数字或数字组相同的历史数据的下一期的对位数字是什么。
现在最新的开奖号是当天的第10期(20070401-10 14304),我要查个位,以个位4为查询依据,查询个位上上一次出现4的下一期的数字是什么数字?历史数据上显示,除第10期个位上的4以外,最近个位上出现数字4的是第5期(20070401-05 13924),与第5期对位的第6期个位上的数字9,就是我们要查询的结果。
再如,我要查十位上以第10期十位上的0和第9期十位上的2为纵向依据组,查询在十位上与这个依据组相同的历史数据组的下一期是什么数字?
20070401-09 2
20070401-10 14304
历史数据显示第5期十位上的2和第6期十位上的0与我们要查询的依据相同,第7期奖号十位上的1就是我们要查询的结果了。

...全文
197 30 打赏 收藏 转发到动态 举报
写回复
用AI写文章
30 条回复
切换为时间正序
请发表友善的回复…
发表回复
lsd123 2009-04-29
  • 打赏
  • 举报
回复
.
永不言弃SUN 2009-04-29
  • 打赏
  • 举报
回复
我等下来试试,先吃饭
等不到来世 2009-04-29
  • 打赏
  • 举报
回复
该存储过程能输出多个下次可能出现的数字,如果有多个匹配的话。
甚至可以按匹配次数,输出最有可能出现的数字。
但是不知道LZ的SQL版本能不能用,我就不画蛇添足了。
等不到来世 2009-04-29
  • 打赏
  • 举报
回复
有个参数忘改回来了,存储过程应该如下:
--创建存储过程:
if object_id('GetNext','p') is not null
drop proc GetNext
go
create proc GetNext
@grpSize int=1, --依据组的数量 [1,8000]
@index int -- 取第几位 [1,5]
as
set nocount on
--参数检查
if @grpSize<1 or @grpSize>8000 or @index<1 or @index>5
begin
print 'para error.Input again:'
return
end
--整理结果集
select id=identity(int,1,1),dt,num,keynum=substring(num,@index,1)
into #1
from tb
order by dt
--提取依据数据(组)
select * into #2
from (select top(@grpSize) * from #1 order by id desc) t
order by id
--使用CTE进行匹配并得到预测数字
declare @s varchar(8000),@minID int,@minKey varchar(1)
select @s=isnull(@s,'')+keynum from #2
select top 1 @minID=ID,@minKey=keynum from #2 order by id

;with szx as
(
select *,topid=id,cnt=1 from #1 where keynum=@minKey and id<@minID
union all
select b.*,a.topid,a.cnt+1
from szx a join #1 b
on a.id=b.id-1 and a.cnt<@grpSize
and b.keynum=substring(@s,a.cnt+1,1)
)
select a.keynum as NextNumber from #1 a
join (select distinct topid from szx) b
on a.id=b.topid+@grpSize
go
等不到来世 2009-04-29
  • 打赏
  • 举报
回复
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([dt] varchar(11),[num] varchar(5))
insert [tb]
select '20070401-01','58105' union all
select '20070401-02','48338' union all
select '20070401-03','30353' union all
select '20070401-04','24579' union all
select '20070401-05','13924' union all
select '20070401-06','52309' union all
select '20070401-07','48517' union all
select '20070401-08','38172' union all
select '20070401-09','20320' union all
select '20070401-10','14304'
go
--select * from [tb]
--创建存储过程(仅限sql2005):
if object_id('GetNext','p') is not null
drop proc GetNext
go
create proc GetNext
@grpSize int=1, --依据组的数量 [1,8000]
@index int -- 取第几位 [1,5]
as
set nocount on
--参数检查
if @grpSize<1 or @grpSize>8000 or @index<1 or @index>5
begin
print 'para error.Input again:'
return
end
--整理结果集
select id=identity(int,1,1),dt,num,keynum=substring(num,@index,1)
into #1
from tb
order by dt
--提取依据数据(组)
select * into #2
from (select top(@grpSize) * from #1 order by id desc) t
order by id
--使用CTE进行匹配并得到预测数字
declare @s varchar(8000),@minID int,@minKey varchar(1)
select @s=isnull(@s,'')+keynum from #2
select top 1 @minID=ID,@minKey=keynum from #2 order by id

;with szx as
(
select *,topid=id,cnt=1 from #1 where keynum=@minKey and id<@minID
union all
select b.*,a.topid,a.cnt+1
from szx a join #1 b
on a.id=b.id-1 and a.cnt<2
and b.keynum=substring(@s,a.cnt+1,1)
)
select a.keynum as NextNumber from #1 a
join (select distinct topid from szx) b
on a.id=b.topid+@grpSize
go

--测试结果:
exec GetNext @grpSize=1,@index=5
/*
NextNumber
----------
9
*/

exec GetNext 2,4
/*
NextNumber
----------
1
*/
叶子 2009-04-29
  • 打赏
  • 举报
回复

declare @ss varchar(20)
declare @centers varchar(2000)
set @centers=''
declare sql_cursor cursor
for
select right(中奖号码,1) from 开奖情况
open sql_cursor
fetch next from sql_cursor into @ss
while (@@fetch_status <> -1)
begin
if (@@fetch_status <> -2)
begin
set @centers=@centers+@ss;
fetch next from sql_cursor into @ss
end
end
close sql_cursor
deallocate sql_cursor;


select right(@centers,3) '最近连续三期的个位'

select substring(@centers,charindex(right(@centers,3),@centers)+3,1) '上次出现的下一期'

/*
最近连续三期的个位
---------
204

(1 row(s) affected)

上次出现的下一期
--------
6
*/
叶子 2009-04-29
  • 打赏
  • 举报
回复
思路就是把每期的个位就取出来放到一个字符串中例如991112342342134321112(连续20期的个位)
然后最近四期的个位就是1112 然后去前面找1112. 看下一个是什么就行了?(上面写麻烦了)

叶子 2009-04-29
  • 打赏
  • 举报
回复

declare @ss varchar(20)
declare @centers varchar(2000)
set @centers=''
declare sql_cursor cursor
for
select right(中奖号码,1) from 开奖情况
open sql_cursor
fetch next from sql_cursor into @ss
while (@@fetch_status <> -1)
begin
if (@@fetch_status <> -2)
begin
set @centers=@centers+@ss;
fetch next from sql_cursor into @ss
end
end
close sql_cursor
deallocate sql_cursor;


select right(@centers,3) '最近连续三期的个位'

select 开奖日期,right(中奖号码,1) as 个位号码 from (
select row_number() over (order by 开奖日期) as row ,*
from 开奖情况)
bb where bb.row=(select charindex(right(@centers,3),@centers)+3)

/*
最近连续三期的个位
---------
204

(1 row(s) affected)

开奖日期 个位号码
----------- ----
20070401-05 6

(1 row(s) affected)
*/

叶子 2009-04-29
  • 打赏
  • 举报
回复
先创建这个表

create table 开奖情况(开奖日期 varchar(11),中奖号码 varchar(20))
insert into 开奖情况
select '20070401-02','48332' union all
select '20070401-03','30350' union all
select '20070401-04','24574' union all
select '20070401-05','13926' union all
select '20070401-06','52309' union all
select '20070401-07','48517' union all
select '20070401-08','38172' union all
select '20070401-09','20320' union all
select '20070401-10','14304'

select * from 开奖情况


叶子 2009-04-29
  • 打赏
  • 举报
回复
换个思路,好做很多。

create table 开奖情况(开奖日期 varchar(11),中奖号码 varchar(20))
insert into 开奖情况
select '20070401-02','48332' union all
select '20070401-03','30350' union all
select '20070401-04','24574' union all
select '20070401-05','13926' union all
select '20070401-06','52309' union all
select '20070401-07','48517' union all
select '20070401-08','38172' union all
select '20070401-09','20320' union all
select '20070401-10','14304'

select * from 开奖情况


declare @ss varchar(20)
declare @centers varchar(2000)
set @centers=''
declare sql_cursor cursor
for
select right(中奖号码,1) from 开奖情况
open sql_cursor
fetch next from sql_cursor into @ss
while (@@fetch_status <> -1)
begin
if (@@fetch_status <> -2)
begin
select @ss;
set @centers=@centers+@ss;
select @centers
fetch next from sql_cursor into @ss
end
end
close sql_cursor
deallocate sql_cursor;

/*结果
204697204
*/
叶子 2009-04-29
  • 打赏
  • 举报
回复
说个思路吧

找到最近3期的个位号
然后用游标循环处理最近3期外的其他数据
如果某期个位满足2 并且下期个位满足0 并且下下期个位满足4,则输出下下下期的个位。

和我上面写得代码类似就是相对麻烦很多。
等不到来世 2009-04-29
  • 打赏
  • 举报
回复
sql 2000 还是 2005?
chang1216 2009-04-29
  • 打赏
  • 举报
回复
一次类推 如果 十位 上 我想找 某四期 或者 更多期 怎么做?
chang1216 2009-04-29
  • 打赏
  • 举报
回复
如果我 比如:要查找 8,9,10期 的个位 2,0,4 在上次 连续出现过 的下一期对应的各位是几? 又怎么做呢?
叶子 2009-04-29
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 chang1216 的回复:]
哈尔滨的哥们 差不多了

不过最后的结果不是我要的 我要是的 这个数在上次出现以后的下一期 同位置的数 是几!! 明白了吗?
[/Quote]
明白你的意思呀
个位最近一期是4
上次个位出现4是第五期
然后我找到第五期的下一期,也就是第六期的个位是9 (13楼代码)

十位最近一期是0
上次十位出现0的是第六期
然后我找到第六期的下一期,也就是第七期的十位是1 (14楼代码)

有问题?
chang1216 2009-04-29
  • 打赏
  • 举报
回复
哈尔滨的哥们 差不多了

不过最后的结果不是我要的 我要是的 这个数在上次出现以后的下一期 同位置的数 是几!! 明白了吗?
叶子 2009-04-29
  • 打赏
  • 举报
回复


declare @table table (开奖日期 varchar(11),中奖号码 varchar(20))
insert into @table
select '20070401-01','58105' union all
select '20070401-02','48338' union all
select '20070401-03','30353' union all
select '20070401-04','24579' union all
select '20070401-05','13924' union all
select '20070401-06','52309' union all
select '20070401-07','48517' union all
select '20070401-08','38172' union all
select '20070401-09','20320' union all
select '20070401-10','14304'

declare @最新期号 varchar(20)

declare @十位 varchar(6)
set @十位=(select top 1 substring(中奖号码,4,1) from @table order by 开奖日期 desc)
set @最新期号=(select top 1 开奖日期 from @table order by 开奖日期)

select @十位 '最近一期的十位'

--通过十位的0找到第七期的十位1

select top 1 substring(中奖号码,4,1) as '上一期的十位' from @table
where 开奖日期=
(
select top 1 substring(开奖日期,1,10)+cast(cast(substring(开奖日期,11,2) as int)+1 as varchar(8))
from @table
where substring(中奖号码,4,1)=@十位 and 开奖日期<>@最新期号
) order by 开奖日期

/*
最近一期的十位
-------
0

上一期的十位
------
1
*/

叶子 2009-04-29
  • 打赏
  • 举报
回复

declare @table table (开奖日期 varchar(11),中奖号码 varchar(20))
insert into @table
select '20070401-01','58105' union all
select '20070401-02','48338' union all
select '20070401-03','30353' union all
select '20070401-04','24579' union all
select '20070401-05','13924' union all
select '20070401-06','52309' union all
select '20070401-07','48517' union all
select '20070401-08','38172' union all
select '20070401-09','20320' union all
select '20070401-10','14304'

declare @个位 varchar(6)
set @个位=(select top 1 right(中奖号码,1) from @table order by 开奖日期 desc)
select @个位 '最近一期的个位'
--通过个位的4找到第六期的个位9
select top 1 right(中奖号码,1) as '上一期的个位' from @table
where 开奖日期=
(
select top 1 substring(开奖日期,1,10)+cast(cast(substring(开奖日期,11,2) as int)+1 as varchar(8))
from @table
where right(中奖号码,1)=@个位
)

/*
最近一期的个位
-------
4

(1 row(s) affected)

上一期的个位
------
9
*/
lg3605119 2009-04-29
  • 打赏
  • 举报
回复
现在分越来越不好抢了 JF
叶子 2009-04-29
  • 打赏
  • 举报
回复


declare @table table (开奖日期 varchar(11),中奖号码 varchar(20))
insert into @table
select '20070401-01','58105' union all
select '20070401-02','48338' union all
select '20070401-03','30353' union all
select '20070401-04','24579' union all
select '20070401-05','13924' union all
select '20070401-06','52309' union all
select '20070401-07','48517' union all
select '20070401-08','38172' union all
select '20070401-09','20320' union all
select '20070401-10','14304'
--所有记录
select * from @table
--最新一条记录
select top 1 * from @table order by 开奖日期 desc
--以个位查询
declare @个位 varchar(6)
set @个位=(select top 1 right(中奖号码,1) from @table order by 开奖日期 desc)
select @个位 '个位'
--找到第五期
select top 1 * from @table
where right(中奖号码,1)=@个位

/*
开奖日期 中奖号码
----------- --------------------
20070401-01 58105
20070401-02 48338
20070401-03 30353
20070401-04 24579
20070401-05 13924
20070401-06 52309
20070401-07 48517
20070401-08 38172
20070401-09 20320
20070401-10 14304


开奖日期 中奖号码
----------- --------------------
20070401-10 14304


个位
------
4


开奖日期 中奖号码
----------- --------------------
20070401-05 13924

*/
加载更多回复(10)

34,594

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧