程序员家园论坛软件开发数据库技术讨论区 11-03题目:用T-SQL获取26选7的所有组合

1  /  2  页   12 跳转 查看:5884

11-03题目:用T-SQL获取26选7的所有组合

11-03题目:用T-SQL获取26选7的所有组合

题目详述:从数字0,1,2,3,4,....25这二十六个数字选出任何7个数字,数字不能有重复,用T-SQL实现这些数字中任意7个数字的所有组合列表
暂时没有想好
 

回复:11-03题目:用T-SQL获取26选7的所有组合

先后次序不同算不算,比如 1-2-3-4-5-6-7 与7-6-5-4-3-2-1 算不算重复?

declare @one int,@two int,@three int,@four int,@five int,@six int ,@seven int,@maxvalue int
set @maxvalue = 27
select  @one=1 
while  @one<=@maxvalue   
begin 
    select  @two=1 
    while  @two<=@maxvalue 
    begin 
        select  @three=1 
        while  @three<=@maxvalue 
        begin
            select @four = 1
            while @four < @maxvalue
            begin
                select @five = 1
               while @five < @maxvalue
                begin   
                      select @six = 1
                   while @six < @maxvalue
                    begin   
                        select @seven = 1
                       while @seven < @maxvalue
                        begin   
                            print(cast(@one as varchar(3))+'-'+cast(@two as varchar(3))+'-'
                                    +cast(@three as varchar(3))+'-'+cast(@four as varchar(3))+'-'
                                    +cast(@five as varchar(3))+'-'+cast(@six as varchar(3))+'-'
                                    +cast(@seven as varchar(3))) 
                            select @seven = @seven + 1
                        end
                        select @six = @six + 1
                    end
                    select @five = @five +1
                end
                select @four = @four + 1
            end
            select  @three=@three+1 
        end 
        select  @two=@two+1 
    end 
select  @one=@one+1 
end 
/* 
1-1-1-1-1-1-1
1-1-1-1-1-1-2
1-1-1-1-1-1-3
1-1-1-1-1-1-4
1-1-1-1-1-1-5
1-1-1-1-1-1-6
1-1-1-1-1-1-7
1-1-1-1-1-1-8
1-1-1-1-1-1-9
1-1-1-1-1-1-10
1-1-1-1-1-1-11
1-1-1-1-1-1-12
1-1-1-1-1-1-13
1-1-1-1-1-1-14
1-1-1-1-1-1-15
1-1-1-1-1-1-16
1-1-1-1-1-1-17
1-1-1-1-1-1-18
1-1-1-1-1-1-19
1-1-1-1-1-1-20
1-1-1-1-1-1-21
1-1-1-1-1-1-22
1-1-1-1-1-1-23
1-1-1-1-1-1-24
1-1-1-1-1-1-25
1-1-1-1-1-1-26
1-1-1-1-1-2-1
1-1-1-1-1-2-2
1-1-1-1-1-2-3
1-1-1-1-1-2-4
1-1-1-1-1-2-5
1-1-1-1-1-2-6
1-1-1-1-1-2-7
1-1-1-1-1-2-8
1-1-1-1-1-2-9
1-1-1-1-1-2-10
1-1-1-1-1-2-11
1-1-1-1-1-2-12
1-1-1-1-1-2-13
1-1-1-1-1-2-14
1-1-1-1-1-2-15
1-1-1-1-1-2-16
1-1-1-1-1-2-17
1-1-1-1-1-2-18
1-1-1-1-1-2-19
1-1-1-1-1-2-20
1-1-1-1-1-2-21
1-1-1-1-1-2-22
1-1-1-1-1-2-23
1-1-1-1-1-2-24
1-1-1-1-1-2-25
1-1-1-1-1-2-26
1-1-1-1-1-3-1
1-1-1-1-1-3-2
1-1-1-1-1-3-3
1-1-1-1-1-3-4
1-1-1-1-1-3-5
1-1-1-1-1-3-6
1-1-1-1-1-3-7
1-1-1-1-1-3-8
1-1-1-1-1-3-9
1-1-1-1-1-3-10
1-1-1-1-1-3-11
1-1-1-1-1-3-12
1-1-1-1-1-3-13
1-1-1-1-1-3-14
1-1-1-1-1-3-15
1-1-1-1-1-3-16
1-1-1-1-1-3-17
1-1-1-1-1-3-18
1-1-1-1-1-3-19
1-1-1-1-1-3-20
1-1-1-1-1-3-21
1-1-1-1-1-3-22
1-1-1-1-1-3-23

...........

26-26-26-26-26-26-26

*/

--我的娃哟,我的本本跑了10分钟才全部打完哦
心若无尘,一花一世界,一鸟一天堂
 

回复:11-03题目:用T-SQL获取26选7的所有组合

不好意思,只要到25呀

declare @one int,@two int,@three int,@four int,@five int,@six int ,@seven int,@maxvalue int
set @maxvalue = 25
select  @one=1 
while  @one<=@maxvalue   
begin 
    select  @two=1 
    while  @two<=@maxvalue 
    begin 
        select  @three=1 
        while  @three<=@maxvalue 
        begin
            select @four = 1
            while @four < @maxvalue
            begin
                select @five = 1
              while @five < @maxvalue
                begin   
                      select @six = 1
                  while @six < @maxvalue
                    begin   
                        select @seven = 1
                      while @seven < @maxvalue
                        begin   
                            print(cast(@one as varchar(3))+'-'+cast(@two as varchar(3))+'-'
                                    +cast(@three as varchar(3))+'-'+cast(@four as varchar(3))+'-'
                                    +cast(@five as varchar(3))+'-'+cast(@six as varchar(3))+'-'
                                    +cast(@seven as varchar(3))) 
                            select @seven = @seven + 1
                        end
                        select @six = @six + 1
                    end
                    select @five = @five +1
                end
                select @four = @four + 1
            end
            select  @three=@three+1 
        end 
        select  @two=@two+1 
    end 
select  @one=@one+1 
end
心若无尘,一花一世界,一鸟一天堂
 

回复:11-03题目:用T-SQL获取26选7的所有组合

恩,执行这个是需要一端时间的
暂时没有想好
 

回复:11-03题目:用T-SQL获取26选7的所有组合

人呢,找你
心若无尘,一花一世界,一鸟一天堂
 

回复:11-03题目:用T-SQL获取26选7的所有组合

出去玩了,嘿嘿
暂时没有想好
 

回复:11-03题目:用T-SQL获取26选7的所有组合

版主的T-SQL果然厉害!炉火纯青,已臻化境啊!
宠辱不惊,看庭前花开花落,去留无意,望天空云卷云舒
 

回复:11-03题目:用T-SQL获取26选7的所有组合

我记的兔子曾经写过一个SQL语句来实现这个功能的,好像就一句,我记不起来了efault3:
暂时没有想好
 

回复: 11-03题目:用T-SQL获取26选7的所有组合



引用:
原帖由 hongjuan 于 2007-11-4 22:51:00 发表
我记的兔子曾经写过一个SQL语句来实现这个功能的,好像就一句,我记不起来了efault3: 



挖他出来
心若无尘,一花一世界,一鸟一天堂
 

回复:11-03题目:用T-SQL获取26选7的所有组合

CREATE FUNCTION SelectNFromM (@N INT, @M INT)
RETURNS @Temp TABLE (Value NVARCHAR(50),
         MaxIndex INT)
AS
BEGIN
    DECLARE @IndexTemp TABLE ([Index] INT)

    DECLARE @INDEX INT

    SET @INDEX = 0
    WHILE (@INDEX < @M)
    BEGIN
        INSERT @IndexTemp VALUES (@INDEX)
        SET @INDEX = @INDEX + 1
    END

    IF (@N = 1)
    BEGIN
        INSERT @Temp SELECT [Index], [Index] FROM @IndexTemp
    END
    ELSE
    BEGIN
        SET @INDEX = @N - 1;
        INSERT @Temp SELECT a.Value + ' ' + CONVERT(NVARCHAR(50), b.[Index]), b.[Index] FROM SelectNFromM(@INDEX, @M) AS a, @IndexTemp b WHERE b.[Index] > a.MaxIndex
    END

    RETURN
END
GO
SELECT Value FROM SelectNFromM (7, 26)
 
1  /  2  页   12 跳转

版权所有 程序员家园论坛   Sitemap

Powered by Discuz!NT 2.1.202    Copyright © 2001-2008 Comsenz Inc.
Processed in 0.0625 second(s) , 4 queries. 浙ICP备07502118号
返顶部