日期:2014-05-18  浏览次数:20925 次

SQL SERVER 2008排列组合算法
我想学习做一个彩票分析软件,排列于组合可以很快生成出来,但是我想生成出来的号码里面包含某些数字不知道怎么做
生成:
create table #number(T1 tinyint)
declare @T1 tinyint
SET @T1 = 1
WHILE @T1 <= 33
BEGIN
INSERT INTO #number VALUES(@T1)
SET @T1 = @T1 + 1
END
select * from #number a1,#number a2,#number a3,#number a4,#number a5,#number a6
where a1.t1 < a2.t1 and
  a2.t1 < a3.t1 and
  a3.t1 < a4.t1 and
  a4.t1 < a5.t1 and
  a5.t1 < a6.t1
1.然后在这100多万注里有 1,2,3,4,5,6,7,8,9,10里1-3个号码的挑出来,超过4个以上的不显示出来怎么弄?
2.连号数字不需要5连号以上的数字,如1,2,3,4,5,8,最多4连号,并且连号对数不超过2个,如1,2,6,7,8,11
3.怎么计算遗漏值的,我有个历史开奖档,里面有T1,T2,T3,T4,T5,T6分别对应大小顺序排列的开奖记录
  我想算下T1中的某个数字最大遗漏了多少期!怎么查
能解决以上问题者,膜拜,重谢,做了这个软件,我的算法又提升了一步,哈哈

------解决方案--------------------
1.
SQL code
select * from #number a1,#number a2,#number a3,#number a4,#number a5,#number a6
where a1.t1 < a2.t1 and
  a2.t1 < a3.t1 and
  a3.t1 < a4.t1 and
  a4.t1 < a5.t1 and
  a5.t1 < a6.t1 
and (case when a1.t1<=10 then 1 else 0 end)+
(case when a2.t1<=10 then 1 else 0 end)+
(case when a3.t1<=10 then 1 else 0 end)+
(case when a4.t1<=10 then 1 else 0 end)+
(case when a5.t1<=10 then 1 else 0 end)+
(case when a6.t1<=10 then 1 else 0 end) between 1 and 3

------解决方案--------------------
2.2 三连号必定是三对:
SQL code
select * from #number a1,#number a2,#number a3,#number a4,#number a5,#number a6
where a1.t1 < a2.t1 and
  a2.t1 < a3.t1 and
  a3.t1 < a4.t1 and
  a4.t1 < a5.t1 and
  a5.t1 < a6.t1 
and not(a1.t1=a2.t2-1 and a2.t1<>a3.t1-1 and a3.t1=a4.t1-1 and a4.t1<>a5.t1-1 and a5.t1=a6.t1-1)

------解决方案--------------------
SQL code

-- 3.怎么计算遗漏值的,我有个历史开奖档,里面有T1,T2,T3,T4,T5,T6分别对应大小顺序排列的开奖记录
--   我想算下T1中的某个数字最大遗漏了多少期!怎么查

--> 沒有表結構,以下是偽代碼供參考.
declare @t int
select @t=[某个数字]

with t0 as
(select row_number() over(order by QIHAO) rn, QIHAO,
T1,T2,T3,T4,T5,T6 from HISTORY),
t1 as
(select row_number() over(order by QIHAO) rn2,* from t0 
where T1=@t or T2=@t or T3=@t or T4=@t or T5=@t or T6=@t),
t2 as
(select b.rn-a.rn y
 from t1 a
 left join t1 b on a.rn2=b.rn2-1)
select max(y) '最大遗漏了期数' from t2