1: 用最笨的方法,不知有没有高招
declare @jh int,@th int
Select @jh = (case when jh1 = @h1 then 1 else 0)
   + (case when jh2 = @h2 then 1 else 0)
   + (case when jh3 = @h3 then 1 else 0)
   + (case when jh4 = @h4 then 1 else 0)
   + (case when jh5 = @h5 then 1 else 0)
   + (case when jh6 = @h6 then 1 else 0) ,
   @th = (case when th = @th then 1 else 0)   from fc_det where qs = @qs
Select (case when @jh + @th = 4 then '6'
            when @jh = 4 and @th = 1 then '5'
            when @jh = 5 and @th = 0 then '4'
            when @jh = 5 and @th = 1 then '3'
            when @jh = 6 and @th = 0 then '2'
            when @jh = 6 and @th = 1 then '1'
            else 0 end)

解决方案 »

  1.   

    Select
    Case 
    When CharIndex(q.djh,p.jh)=1 And q.dth=p.th Then '一等奖'
    When CharIndex(q.djh,p.jh)=1 And q.dth!=p.th Then '二等奖'
    When CharIndex(q.djh,p.jh)>0 And CharIndex(q.djh,p.jh)<3 And q.dth=p.th Then '三等奖'
    When CharIndex(q.djh,p.jh)>0 And CharIndex(q.djh,p.jh)<3 And q.dth!=p.th Then '四等奖'
    When CharIndex(q.djh,p.jh)>0 And CharIndex(q.djh,p.jh)<4 And q.dth=p.th Then '五等奖'
    When (CharIndex(q.djh,p.jh)>0 And CharIndex(q.djh,p.jh)<4 And q.dth!=p.th) or (CharIndex(q.djh,p.jh)>0 And CharIndex(q.djh,p.jh)<5 And q.dth=p.th) Then '六等奖'
    End As 中奖情况
    From
    (Select jh1+jh2+jh3+jh4+jh5+jh6 as jh,th From fc_det where qs=期数) As p,
    (Select djh1+djh2+djh3+djh4+djh5+djh6 as djh,dth) As q  --中奖品号码构成表
      

  2.   

    samuelpan(欧乐)的方法好象只能核对一等奖和二等奖
      

  3.   

    第二点:我的方法
    Create   view v_fc_det
    As
    Select qs,jh1 from fc_det
    union 
    select qs,jh2 from fc_det
    union 
    select qs,jh3 from fc_det
    union 
    select qs,jh4 from fc_det
    union 
    select qs,jh5 from fc_det
    union 
    select qs,jh6 from fc_det
    union 
    select qs,th from fc_det/*
    Select jh1,count(*) from v_fc_det 
    where qs > '2003'
    group by jh1
    order by count(*) desc
    */
    不能查询次数为0的资料
      

  4.   

    表结构见题,附2003年数据如下
    insert fc_det values('2003001','19','22','23','25','28','33','06')
    insert fc_det values('2003002','04','12','14','18','19','30','03')
    insert fc_det values('2003003','07','09','15','20','25','31','32')
    insert fc_det values('2003004','04','08','10','15','18','25','28')
    insert fc_det values('2003005','10','13','15','29','30','33','34')
    insert fc_det values('2003006','08','12','16','20','22','24','29')
    insert fc_det values('2003007','02','04','12','13','15','30','33')
    insert fc_det values('2003008','06','22','26','27','28','33','05')
    insert fc_det values('2003009','06','07','18','20','21','34','24')
    insert fc_det values('2003010','03','15','20','23','30','34','25')
    insert fc_det values('2003011','01','03','05','24','26','33','08')
    insert fc_det values('2003012','14','21','22','25','30','35','05')
    insert fc_det values('2003013','01','04','17','23','30','33','35')
    insert fc_det values('2003014','02','14','15','18','24','29','28')
    insert fc_det values('2003015','11','18','20','30','32','35','33')
    insert fc_det values('2003016','05','07','15','30','31','33','14')
    insert fc_det values('2003017','11','24','25','28','29','30','04')
    insert fc_det values('2003018','02','07','11','13','17','31','05')
    insert fc_det values('2003019','01','04','05','27','31','36','35')
    insert fc_det values('2003020','08','13','17','18','20','26','31')
    insert fc_det values('2003021','03','08','20','26','35','36','34')
    insert fc_det values('2003022','09','10','18','28','31','34','27')
    insert fc_det values('2003023','08','19','25','26','29','35','07')
    insert fc_det values('2003024','10','12','15','24','32','36','11')
    insert fc_det values('2003025','01','02','11','18','25','31','33')
    insert fc_det values('2003026','10','11','20','21','24','32','13')
    insert fc_det values('2003027','05','06','11','14','25','26','23')
    insert fc_det values('2003028','02','11','14','18','31','34','25')
    insert fc_det values('2003029','03','12','13','14','20','22','30')
    insert fc_det values('2003030','02','03','17','20','23','29','24')
    insert fc_det values('2003031','06','14','17','18','31','35','27')
    insert fc_det values('2003032','03','09','12','14','34','35','17')
    insert fc_det values('2003033','07','09','19','20','22','32','14')
    insert fc_det values('2003034','05','24','26','27','32','35','31')
    insert fc_det values('2003035','01','06','11','14','20','24','17')
    insert fc_det values('2003036','09','19','22','25','29','35','07')
    insert fc_det values('2003037','01','14','25','26','31','34','28')
    insert fc_det values('2003038','07','09','12','15','33','34','24')
    insert fc_det values('2003039','08','10','11','13','17','19','14')
    insert fc_det values('2003040','11','13','15','28','30','31','20')
    insert fc_det values('2003041','02','06','16','29','33','34','27')
    insert fc_det values('2003042','09','11','15','17','20','21','33')
    insert fc_det values('2003043','05','06','21','30','34','35','29')
    insert fc_det values('2003044','03','19','24','27','32','33','07')
    insert fc_det values('2003045','01','05','07','10','16','19','33')
    insert fc_det values('2003046','04','18','23','24','31','32','36')
    insert fc_det values('2003047','06','07','09','11','12','16','21')
    insert fc_det values('2003048','08','09','15','16','25','28','36')
    insert fc_det values('2003049','02','03','06','09','10','15','30')
    insert fc_det values('2003050','02','03','07','17','22','32','35')
    insert fc_det values('2003051','04','06','07','11','19','28','35')
    insert fc_det values('2003052','01','03','04','15','20','24','33')
    insert fc_det values('2003053','05','15','16','18','21','24','19')
    insert fc_det values('2003054','01','03','10','22','25','27','30')
    insert fc_det values('2003055','12','20','25','28','30','36','09')
    insert fc_det values('2003056','02','07','09','18','22','26','24')
    insert fc_det values('2003057','04','15','16','17','22','30','23')
    insert fc_det values('2003058','08','09','18','25','31','36','33')
    insert fc_det values('2003059','02','06','12','27','28','34','32')
    insert fc_det values('2003060','05','18','27','32','35','36','26')
    insert fc_det values('2003061','08','14','15','27','28','32','30')
    insert fc_det values('2003062','03','05','07','21','22','23','31')
    insert fc_det values('2003063','06','12','20','21','30','36','33')
    insert fc_det values('2003064','12','14','18','28','33','34','01')
    insert fc_det values('2003065','01','17','22','25','29','34','21')
    insert fc_det values('2003066','07','21','22','23','28','35','27')
    insert fc_det values('2003067','02','13','21','28','29','35','16')
    insert fc_det values('2003068','03','08','10','12','16','28','05')
    insert fc_det values('2003069','02','05','06','19','27','32','17')
    insert fc_det values('2003070','12','13','15','29','33','36','30')
    insert fc_det values('2003071','04','06','07','16','21','23','27')
    insert fc_det values('2003072','03','06','08','13','15','21','17')
    insert fc_det values('2003073','06','16','33','34','35','36','11')
    insert fc_det values('2003074','04','17','21','23','27','30','24')
    insert fc_det values('2003075','01','14','17','21','24','36','15')
    insert fc_det values('2003076','03','12','17','27','32','34','05')
    insert fc_det values('2003077','04','06','08','12','19','22','03')
    insert fc_det values('2003078','06','10','11','13','15','17','31')
    insert fc_det values('2003079','01','03','08','15','18','30','05')
    insert fc_det values('2003080','01','05','21','32','35','36','25')
    insert fc_det values('2003081','03','04','22','26','30','32','18')
    insert fc_det values('2003082','04','09','17','18','20','36','13')
    insert fc_det values('2003083','02','04','08','14','35','36','19')
    insert fc_det values('2003084','05','08','17','24','28','34','29')
      

  5.   

    --问题2:先建立一个计表
    Create Table JH(jh varchar(4) Null,cCount Int Null)
    Go
    Declare @i Int
    Set @i=0
    While (@i<36)
    Begin
      Set @i=@i+1
      Insert Into JH Values(@i,0)
    End
    GoSelect Jh,Sum(cCount) From 
    (
    Select jh1 As Jh, Sum(1) As cCount From fc_det Group by jh1
    Union All
    Select jh2, Sum(1) From fc_det Group by jh2
    Union All
    Select jh3, Sum(1) From fc_det Group by jh3
    Union All
    Select jh4, Sum(1) From fc_det Group by jh4
    Union All
    Select jh5, Sum(1) From fc_det Group by jh5
    Union All
    Select jh6, Sum(1) From fc_det Group by jh6
    Union All
    Select jh7, Sum(1) From fc_det Group by jh7
    Union All
    Select jh, cCount From jh
    ) As t
    Group by t.Jh
    Order by Sum(t.cCount) Desc
    Go---------------------我对彩票的兴趣大大的。------------------
    ------给我一次中500万的机会吧-----------------
    -------------一生一次就够了-----------
      

  6.   

    第二个问题修改:拿去运行就可以了。
    --问题2:先建立一个计表
    if object_id('jh') is not nUll
    Drop table jh
    go
    Create Table JH(jh varchar(4) Null,cCount Int Null)
    Go
    Declare @i Int,@f Varchar(4)
    Set @i=0
    While (@i<36)
    Begin
      Set @i=@i+1
      if @i<9
         set @f='0'+cast(@i as Varchar)
      Insert Into JH Values(@f,0)
    End
    GoSelect t.Jh,Sum(t.cCount) From 
    (
    Select jh1 As Jh, Sum(1) As cCount From fc_det Group by jh1
    Union All
    Select jh2, Sum(1) From fc_det Group by jh2
    Union All
    Select jh3, Sum(1) From fc_det Group by jh3
    Union All
    Select jh4, Sum(1) From fc_det Group by jh4
    Union All
    Select jh5, Sum(1) From fc_det Group by jh5
    Union All
    Select jh6, Sum(1) From fc_det Group by jh6
    Union All
    Select th, Sum(1) From fc_det Group by th
    Union All
    Select jh, cCount From jh
    ) As t
    Group by t.Jh
    Order by Sum(t.cCount) Desc
      

  7.   

    txlicenhe(不做技术高手) 中奖查询时未考虑连号的问题。
    中奖查询我是否有困难了。
    第二问题根据txlicenhe(不做技术高手)再优化
    Select t.Jh,Sum(t.cCount) From 
    (
    Select qs,jh1 as jh,1 As cCount From fc_det
    Union All
    Select qs,jh2,1 fc_det From fc_det
    Union All
    Select qs,jh3,1 fc_det From fc_det
    Union All
    Select qs,jh4,1 fc_det From fc_det
    Union All
    Select qs,jh5,1 fc_det From fc_det
    Union All
    Select qs,jh6,1 fc_det From fc_det
    Union All
    Select qs,th,1 fc_det From fc_det
    Union All
    Select Null,jh,cCount From jh
    ) As t
    Group by t.Jh
    Order by Sum(t.cCount) Desc第三个问题
    --问题2:先建立一个计表
    if object_id('jh') is not nUll
    Drop table jh
    go
    Create Table JH(jh varchar(4) Null,cCount Int Null)
    Go
    Declare @i Int,@f Varchar(4)
    Set @i=0
    While (@i<36)
    Begin
      Set @i=@i+1
      if @i<9
         set @f='0'+cast(@i as Varchar)
      Insert Into JH Values(@f,0)
    End
    GoSelect t.Jh,Sum(t.cCount) From 
    (
    Select qs,jh1 as jh,1 As cCount From fc_det Where jh1='01' or jh2='01' or jh3='01' or jh4='01' or jh5='01' or jh6='01' or th='01' 
    Union All
    Select qs,jh2,1 fc_det From fc_det Where jh1='01' or jh2='01' or jh3='01' or jh4='01' or jh5='01' or jh6='01' or th='01' 
    Union All
    Select qs,jh3,1 fc_det From fc_det Where jh1='01' or jh2='01' or jh3='01' or jh4='01' or jh5='01' or jh6='01' or th='01' 
    Union All
    Select qs,jh4,1 fc_det From fc_det Where jh1='01' or jh2='01' or jh3='01' or jh4='01' or jh5='01' or jh6='01' or th='01' 
    Union All
    Select qs,jh5,1 fc_det From fc_det Where jh1='01' or jh2='01' or jh3='01' or jh4='01' or jh5='01' or jh6='01' or th='01' 
    Union All
    Select qs,jh6,1 fc_det From fc_det Where jh1='01' or jh2='01' or jh3='01' or jh4='01' or jh5='01' or jh6='01' or th='01' 
    Union All
    Select qs,th,1 fc_det From fc_det Where jh1='01' or jh2='01' or jh3='01' or jh4='01' or jh5='01' or jh6='01' or th='01' 
    Union All
    Select Null,jh,cCount From jh
    ) As t
    Group by t.Jh
    Order by Sum(t.cCount) Desc
      

  8.   

    Where jh1='01' or jh2='01' or jh3='01' or jh4='01' or jh5='01' or jh6='01' or th='01' 
    不如用where charindex('01',jh1 + ',' + jh2 + ','+ jh3+','+jh4+','+jh5 + ','+jh6 + ',' + th) > 0 代替
      

  9.   

    中奖测试成功declare @qs varchar(8)
    declare @jh1 varchar(4)
    declare @jh2 varchar(4)
    declare @jh3 varchar(4)
    declare @jh4 varchar(4)
    declare @jh5 varchar(4)
    declare @jh6 varchar(4)
    declare @th varchar(4)
    Set @qs='2003001'
    Set @jh1='19'
    Set @jh2='21'
    Set @jh3='23'
    Set @jh4='25'
    Set @jh5='28'
    Set @jh6='33'
    Set @th='06'Select 
    Case
    When CharIndex('111111',t.jh)=1 And th=1 Then '一等奖'
    When CharIndex('111111',t.jh)=1 And th=0 Then '二等奖'
    When CharIndex('11111',t.jh)>0 And CharIndex('11111',t.jh)<3 And th=1 Then '三等奖'
    When CharIndex('11111',t.jh)>0 And CharIndex('11111',t.jh)<3 And th=0 Then '四等奖'
    When CharIndex('1111',t.jh)>0 And CharIndex('1111',t.jh)<4 And th=1 Then '五等奖'
    When (CharIndex('1111',t.jh)>0 And CharIndex('1111',t.jh)<4 And th=0) or (CharIndex('111',t.jh)>0 And CharIndex('111',t.jh)<5 And th=1) Then '六等奖'
    Else '未中奖'
    End As 中奖情况 
    From
    (
    Select 
    qs,
    Case When jh1=@jh1 Then '1' Else '0' End
    +
    Case When jh2=@jh2 Then '1' Else '0' End
    +
    Case When jh3=@jh3 Then '1' Else '0' End
    +
    Case When jh4=@jh4 Then '1' Else '0' End
    +
    Case When jh5=@jh5 Then '1' Else '0' End
    +
    Case When jh6=@jh6 Then '1' Else '0' End
    As jh,
    Case When th=@th Then 1 Else 0 End as th 
    From fc_det Where qs=@qs
    )
    As t