create table tb (id int,t1 int,t2 int) 
insert tb select 1,1,1 union 
          select 2,1,1 union 
          select 3,2,1 union 
          select 4,2,1 union 
          select 5,2,1 union 
          select 6,2,2 select a.* , b.* from
(select * from tb) a,
(select * from tb) b
where a.t1 <> b.t1 and a.t2 <> b.t2drop table tb/*
id          t1          t2          id          t1          t2          
----------- ----------- ----------- ----------- ----------- ----------- 
6           2           2           1           1           1
6           2           2           2           1           1
1           1           1           6           2           2
2           1           1           6           2           2(所影响的行数为 4 行)
*/

解决方案 »

  1.   

    --还不太清楚你具体的意思,如果全部显示出来用上面的.如果是每次只随机取一条,用下面这个.
    create table tb (id int,t1 int,t2 int) 
    insert tb select 1,1,1 union 
              select 2,1,1 union 
              select 3,2,1 union 
              select 4,2,1 union 
              select 5,2,1 union 
              select 6,2,2 select top 1 a.* , b.* from
    (select * from tb) a,
    (select * from tb) b
    where a.t1 <> b.t1 and a.t2 <> b.t2
    order by newid()drop table tb/*
    --此处结果每执行一次都不同
    id          t1          t2          id          t1          t2          
    ----------- ----------- ----------- ----------- ----------- ----------- 
    6           2           2           2           1           1
    (所影响的行数为 1 行)
    */
      

  2.   

    --这又是一种结果,不知道你具体要哪个?
    create table tb (id int,t1 int,t2 int) 
    insert tb select 1,1,1 union 
              select 2,1,1 union 
              select 3,2,1 union 
              select 4,2,1 union 
              select 5,2,1 union 
              select 6,2,2 select a.* , b.* from
    (select * from tb) a,
    (select * from tb) b
    where a.t1 <> a.t2 and b.t1 <> b.t2drop table tb/*
    id          t1          t2          id          t1          t2          
    ----------- ----------- ----------- ----------- ----------- ----------- 
    3           2           1           3           2           1
    4           2           1           3           2           1
    5           2           1           3           2           1
    3           2           1           4           2           1
    4           2           1           4           2           1
    5           2           1           4           2           1
    3           2           1           5           2           1
    4           2           1           5           2           1
    5           2           1           5           2           1(所影响的行数为 9 行)
    */
      

  3.   

    create   table   tb   (id   int,t1   int,t2   int) 
    insert   tb   select   1,1,1   
    union   select   2,1,1   
    union   select   3,2,1  
    union   select   4,2,1   
    union   select   5,2,1 
    union   select   6,2,2   
    select a.id as aid,a.t1 as at1 ,a.t2 as at2,b.id as bid,b.t1 as bt1 ,b.t2 as bt2 into #
    from tb a,tb b
    where a.id <> b.id
    order by case when a.t1<>b.t1 and a.t2 <> b.t2 then 1
                  else 2 end, 
    newid()
    declare @t table(aid int,at1 int,at2 int,bid int,bt1 int,bt2 int)
    declare @aid int,@at1 int,@at2 int,@bid int,@bt1 int,@bt2 int
    declare tb cursor for select * from #
    open tb
    FETCH NEXT FROM tb INTO @aid  ,@at1  ,@at2  ,@bid  ,@bt1  ,@bt2WHILE @@FETCH_STATUS = 0
    BEGIN
    if not exists(select 1 from @t where aid = @aid or bid = @bid or aid = @bid or bid = @aid)
    insert @t select @aid  ,@at1  ,@at2  ,@bid  ,@bt1  ,@bt2
        -- Get the next author.
        FETCH NEXT FROM tb INTO @aid  ,@at1  ,@at2  ,@bid  ,@bt1  ,@bt2
    ENDCLOSE tb
    DEALLOCATE tbselect * from @t
    GOdrop table tb,#
    /*aid         at1         at2         bid         bt1         bt2         
    ----------- ----------- ----------- ----------- ----------- ----------- 
    6           2           2           1           1           1
    4           2           1           2           1           1
    3           2           1           5           2           1(所影响的行数为 3 行)\
    aid         at1         at2         bid         bt1         bt2         
    ----------- ----------- ----------- ----------- ----------- ----------- 
    6           2           2           2           1           1
    1           1           1           4           2           1
    3           2           1           5           2           1(所影响的行数为 3 行)aid         at1         at2         bid         bt1         bt2         
    ----------- ----------- ----------- ----------- ----------- ----------- 
    2           1           1           6           2           2
    5           2           1           3           2           1
    4           2           1           1           1           1(所影响的行数为 3 行)aid         at1         at2         bid         bt1         bt2         
    ----------- ----------- ----------- ----------- ----------- ----------- 
    6           2           2           2           1           1
    5           2           1           1           1           1
    4           2           1           3           2           1(所影响的行数为 3 行)
    */
      

  4.   

    aid         at1         at2         bid         bt1         bt2         
    ----------- ----------- ----------- ----------- ----------- ----------- 
    6           2           2           2           1           1
    1           1           1           5           2           1
    4           2           1           3           2           1(所影响的行数为 3 行)
    aid         at1         at2         bid         bt1         bt2         
    ----------- ----------- ----------- ----------- ----------- ----------- 
    1           1           1           6           2           2
    4           2           1           3           2           1
    2           1           1           5           2           1(所影响的行数为 3 行)aid         at1         at2         bid         bt1         bt2         
    ----------- ----------- ----------- ----------- ----------- ----------- 
    6           2           2           1           1           1
    4           2           1           3           2           1
    2           1           1           5           2           1(所影响的行数为 3 行)
      

  5.   

    厉害,特别是ORDER 的那段,我没有想到,佩服佩服!!!!
    不过............................................
    create   table   tb   (id   int,t1   int,t2   int) 
    insert   tb   select   1,1,1   
    union   select   2,2,1   
    union   select   3,1,2  
    union   select   4,2,2   
    union   select   5,1,3 
    union   select   6,2,3  当用上面的数据时,经过几次运行会得到如下结果,显然,这样的结果是不符合要求的。
    /*
    aid         at1         at2         bid         bt1         bt2         
    ----------- ----------- ----------- ----------- ----------- ----------- 
    2           2           1           3           1           2
    1           1           1           4           2           2
    6           2           3           5           1           3
    */
      

  6.   

    order by case when a.t1<>b.t1 and a.t2 <> b.t2 then 1
                  else 2 end, 
    newid()--->order by case when a.t1<>b.t1 and a.t2 <> b.t2 then 1
                  when a.t1<>b.t1 then 2
                  when a.t2<>b.t2 then 3
                  else 4 end, 
    newid()
      

  7.   

    create   table   tb   (id   int,t1   int,t2   int) 
    insert   tb   select   1,1,1   
    union   select   2,1,1   
    union   select   3,2,1  
    union   select   4,2,1   
    union   select   5,2,1 
    union   select   6,2,2   
    select a.id as aid,a.t1 as at1 ,a.t2 as at2,b.id as bid,b.t1 as bt1 ,b.t2 as bt2 into #
    from tb a,tb b
    where a.id<b.id
    order by case when a.t1=b.t1 then 2 else 0 end
    + case when a.t2=b.t2 then 1 else 0 end
            , newid()
    declare @t table(aid int,at1 int,at2 int,bid int,bt1 int,bt2 int)
    declare @aid int,@at1 int,@at2 int,@bid int,@bt1 int,@bt2 int
    declare tb cursor for select * from #
    open tb
    FETCH NEXT FROM tb INTO @aid  ,@at1  ,@at2  ,@bid  ,@bt1  ,@bt2WHILE @@FETCH_STATUS = 0
    BEGIN
        if not exists(select 1 from @t where aid = @aid or bid = @bid or aid = @bid or bid = @aid)
        insert @t select @aid  ,@at1  ,@at2  ,@bid  ,@bt1  ,@bt2
        -- Get the next author.
        FETCH NEXT FROM tb INTO @aid  ,@at1  ,@at2  ,@bid  ,@bt1  ,@bt2
    ENDCLOSE tb
    DEALLOCATE tbselect * from @t
    /*
    test 1:
    aid         at1         at2         bid         bt1         bt2
    ----------- ----------- ----------- ----------- ----------- -----------
    2           1           1           6           2           2
    1           1           1           3           2           1
    4           2           1           5           2           1(3 row(s) affected)test 2:
    aid         at1         at2         bid         bt1         bt2
    ----------- ----------- ----------- ----------- ----------- -----------
    2           1           1           6           2           2
    1           1           1           4           2           1
    3           2           1           5           2           1(3 row(s) affected)test 3:
    aid         at1         at2         bid         bt1         bt2
    ----------- ----------- ----------- ----------- ----------- -----------
    2           1           1           6           2           2
    1           1           1           3           2           1
    4           2           1           5           2           1(3 row(s) affected)
    */GOdrop table tb,#
      

  8.   

    create   table   #tb   (id   int,t1   int,t2   int) 
    insert   #tb   select   1,1,1   union   select   2,1,1   
    union   select   3,2,1   union     select   4,2,1   
    union   select   5,2,1    union   select   6,2,2   select * into #t from #tb
    declare @t table (aid int,at1 int,at2 int,bid int,bt1 int,bt2 int)
    declare @i int,@t1 int,@t2 int
    select @i = 1
    while @i<(select max(id) from #t)
    begin
        select @t1 =t1,@t2=t2 from #t where id=@i
        if exists(select 1 from #t where id>@i and ((t1<>@t1 and t2<>@t2) or (t1<>@t1 and t2=@t2) or (t1=@t1 and t2<>@t2)) 
            and id not in (select aid from @t) and id not in (select bid from @t))
        begin
            insert into @t select top 1 @i,@t1,@t2,id,t1,t2 from #t
            where id>@i and ((t1<>@t1 and t2<>@t2) or (t1<>@t1 and t2=@t2) or (t1=@t1 and t2<>@t2)) 
            and id not in (select aid from @t) and id not in (select bid from @t)
            order by case when t1<>@t1 and t2<>@t2 then 0
            when t1<>@t1 and t2=@t2 then 1
            when t1=@t1 and t2<>@t2 then 2
            end,newid()
        end
        select @i=@i+1
    end
    select * into #t2 from #t where id not in (select aid from @t) and id not in (select bid from @t)
    insert into  @t select top 1 a.*,b.* from #t2 a full join #t2 b on a.id<>b.id 
    drop table #t drop table #t2
    select * from @t
      

  9.   

    try:select * ,row_number() over(order by OrderColumn) 'PKID' INTO #T from 
    (
    select t1.id 't1_id',t1.t1 't1_t1',t1.t2 't1_t2',t2.id 't2_id',t2.t1 't2_t1',t2.t2 't2_t2',
    case when t1.t1 <> t2.t1 and t1.t2 <> t2.t2 then 0
    when t1.t1 <> t2.t1 and t1.t2 = t2.t2 then 1
    when t1.t1 = t2.t1 and t1.t2 <> t2.t2 then 2
    else 3 
    end 'OrderColumn'
    from tb t1 inner join tb t2 on t1.id < t2.id 
    ) as tblTempdeclare @a int,@b int,@c int,@d int,@e int ,@f int,@g int,@h int
    declare @table table(i int)
    declare tb cursor for select * from #T
    open tb
    FETCH NEXT FROM tb INTO @a ,@b ,@c ,@d ,@e ,@f ,@g,@h 
    insert into @table select @a
    insert into @table select @d
    WHILE @@FETCH_STATUS = 0
    BEGIN
    FETCH NEXT FROM tb INTO @a ,@b ,@c ,@d ,@e  ,@f ,@g,@h 
    if(select count(*) from @table where i = @a) > 0 or (select count(*) from @table where i = @d) > 0
    delete #T where PKID =  @h
    else
    begin
    insert into @table select @a
    insert into @table select @d
    end
    END
    CLOSE tb
    DEALLOCATE tb
    select * from #t
    drop table #t
      

  10.   

    aid         at1         at2         bid         bt1         bt2         
    ----------- ----------- ----------- ----------- ----------- ----------- 
    3           1           2           2           2           1
    4           2           2           1           1           1
    5           1           3           6           2           3
    ---
    1、记录组合尽量满足T1,T2均不同。 
    2、如果无法全部满足1,则剩余记录组合尽量满足T1不同。 
    3、如果无法满足1、2,则剩余记录组合尽量满足T2不同。 
    4、结果具有随机性,即执行两次可能得到不同的结果。 
    ---
    因为前面已经把4条提取,
    最后只能这样组合了呀
    ,满足1,2,4
    条件3按题目的意思也是尽量呀
      

  11.   

    只要能出正确结果,不考虑性能。
    上面的都不能说是正确结果。
    create       table       tb       (id       int,t1       int,t2       int)   
    insert       tb       select       1,1,1       
    union       select       2,2,1       
    union       select       3,1,2     
    union       select       4,2,2       
    union       select       5,1,3   
    union       select       6,2,3   
    -------------------
    有结果
    1 1 1 4 2 2
    2 2 1 5 1 3
    3 1 2 6 2 3
    如果你的结果中存在 相同的T1 或T2 则说明不对。
      

  12.   


    set nocount oncreate   table   tb   (id   int,t1   int,t2   int) 
    insert               tb               select               1,1,1               
    union               select               2,2,1               
    union               select               3,1,2           
    union               select               4,2,2               
    union               select               5,1,3       
    union               select               6,2,3  godeclare @t table(aid int,at1 int,at2 int,bid int,bt1 int,bt2 int)
    insert @t select 1,1,1,1,1,1
    while exists(select 1 from @t where at1 = bt1 or at2 = bt2)
    begin delete from @t select a.id as aid,a.t1 as at1 ,a.t2 as at2,b.id as bid,b.t1 as bt1 ,b.t2 as bt2 into #
    from tb a,tb b
    where a.id <> b.id --or (a.t1 <>b.t1 or a.t2 <> b.t2)
    order by case when a.t1<>b.t1 and a.t2 <> b.t2 then 1
                  else 2 end, 
    newid()



    declare @aid int,@at1 int,@at2 int,@bid int,@bt1 int,@bt2 int
    declare tb cursor for select * from #
    open tb
    FETCH NEXT FROM tb INTO @aid  ,@at1  ,@at2  ,@bid  ,@bt1  ,@bt2

    WHILE @@FETCH_STATUS = 0
    BEGIN
        if not exists(select 1 from @t where aid = @aid or bid = @bid or aid = @bid or bid = @aid)
        insert @t select @aid  ,@at1  ,@at2  ,@bid  ,@bt1  ,@bt2
        -- Get the next author.
        FETCH NEXT FROM tb INTO @aid  ,@at1  ,@at2  ,@bid  ,@bt1  ,@bt2
    END

    CLOSE tb
    DEALLOCATE tb
    drop table #

    endselect * from @t
    godrop table tb
      

  13.   

    /*
    aid         at1         at2         bid         bt1         bt2         
    ----------- ----------- ----------- ----------- ----------- ----------- 
    3           1           2           6           2           3
    5           1           3           2           2           1
    4           2           2           1           1           1
    aid         at1         at2         bid         bt1         bt2         
    ----------- ----------- ----------- ----------- ----------- ----------- 
    3           1           2           6           2           3
    5           1           3           2           2           1
    1           1           1           4           2           2
    aid         at1         at2         bid         bt1         bt2         
    ----------- ----------- ----------- ----------- ----------- ----------- 
    2           2           1           5           1           3
    1           1           1           4           2           2
    6           2           3           3           1           2
    aid         at1         at2         bid         bt1         bt2         
    ----------- ----------- ----------- ----------- ----------- ----------- 
    3           1           2           2           2           1
    6           2           3           1           1           1
    4           2           2           5           1           3
    */
      

  14.   

    perfectaction 
    完美行动 
    等 级:
     发表于:2008-02-28 18:47:3029楼 得分:0 
    同意毛,楼主告诉你有1000W数据了?   说不定只有1000条呢,谈个毛性能。 
     --------
    别说是1000条,100条都够你受的,你知道有多少种组合吗?
      

  15.   

    to 35L 
    while exists(select 1 from @t where at1 = bt1 or at2 = bt2)
    ------------------------------------
     是不重了,但并没有告诉你数据一定能够满足T1 T2都不同的情况,题目只是要求达到这种效果。
     如果能够做到T1 T2 都不同,则结果就必须做到。
     如果给的数据做不到,则只能退而求其次。
     我给的第一组数据做不到,应此存在 T1 T2相同的情况。
     我给的第二组数据能够满足要求,因此任何随机给出的结果都应该满足要求。
     举个例子,比如给定1-20的编号的球,球的颜色有5种(红、白、兰、绿、黄)
     现要你随机取3个球,使得取的球尽量为红色,如果满足不了就尽量取白色,如果还满足不了就随便。
     现假设给你的20个球只有1个红的1个白,那这个红的和白的肯定是必须取出来的,另一个随便。
       假设给你的球就3个红的,则结果只有一种(随机取也只能取到一种结果),就这3个球。
       假设给你的球有3个以上的红的,那你取出的球就不能有其它颜色的,否则就不符合要求。
    我想我已经通过上面的例子解释得很清楚了。
    特别说明:上面举的例子只是为了说明题目,与题目没有任何关系,请不要将我的题目转变为上面的例子。
      

  16.   

    dobear_0922 
    do熊 
    等 级:
     发表于:2008-02-28 19:45:3937楼 得分:0 
    perfectaction   
    完美行动   
    等   级: 
      发表于:2008-02-28   18:47:3029楼   得分:0   
    同意毛,楼主告诉你有1000W数据了?       说不定只有1000条呢,谈个毛性能。   
      -------- 
    别说是1000条,100条都够你受的,你知道有多少种组合吗? 
     
    ------------------------------------
    100条?很好很强大。那麻烦你拿出个100条的测试结果来看看。
      

  17.   

    不知道这样是否满足楼主要求?create table tb (id int,t1 int,t2 int) 
    insert               tb               select               1,1,1               
    union               select               2,2,1               
    union               select               3,1,2           
    union               select               4,2,2               
    union               select               5,1,3       
    union               select               6,2,3  /*
    insert   tb   select   1,1,1   
    union   select   2,1,1   
    union   select   3,2,1   
    union   select   4,2,1   
    union   select   5,2,1 
    union   select   6,2,2   */go
    /*
    select a.*,cnt,(select count(*) from tb where t1!=a.t1 and t2!=a.t2) repetition,
    (select count(*) from tb where t1!=a.t1 and t2=a.t2) repetition_t1,
    (select count(*) from tb where t2!=a.t2 and t1=a.t1) repetition_t2
     from tb a
    inner join 
    (select count(*) cnt from tb) b
    on 1=1
    */select gid=1,willbebid=null,r=null,r1=null,r2=null,id,t1,t2 into # from tb /* select top 3 * from
    (
    select a.*,cnt,(select count(*) from tb where t1!=a.t1 and t2!=a.t2) repetition,
    (select count(*) from tb where t1!=a.t1 and t2=a.t2) repetition_t1,
    (select count(*) from tb where t2!=a.t2 and t1=a.t1) repetition_t2
     from tb a
    inner join 
    (select count(*) cnt from tb) b
    on 1=1
    ) base
    order by repetition desc,CAST(RAND(CHECKSUM(NEWID())) * repetition + 1 AS INT),
    repetition_t1 desc,CAST(RAND(CHECKSUM(NEWID())) * repetition_t1 + 1 AS INT),
    repetition_t2 desc,CAST(RAND(CHECKSUM(NEWID())) * repetition_t2 + 1 AS INT),newid()
    */update x set gid=2,r=repetition,r1=repetition_t1,r2=repetition_t2 from # x
    inner join
    (
    select top 3 * from
    (
    select a.*,cnt,(select count(*) from tb where t1!=a.t1 and t2!=a.t2) repetition,
    (select count(*) from tb where t1!=a.t1 and t2=a.t2) repetition_t1,
    (select count(*) from tb where t2!=a.t2 and t1=a.t1) repetition_t2
     from tb a
    inner join 
    (select count(*) cnt from tb) b
    on 1=1
    ) base
    order by repetition desc,CAST(RAND(CHECKSUM(NEWID())) * repetition + 1 AS INT),
    repetition_t1 desc,CAST(RAND(CHECKSUM(NEWID())) * repetition_t1 + 1 AS INT),
    repetition_t2 desc,CAST(RAND(CHECKSUM(NEWID())) * repetition_t2 + 1 AS INT),newid()
    ) y
    on x.id=y.id
    --select * from #
    /*
    select a.id aid,a.t1 at1,a.t2 at2,
           b.id bid,b.t1 bt1,b.t2 bt2
    from # a
    left join # b
    on a.t1 != b.t1 + case when exists(select 1 from # where gid=1 and t1!=a.t1) then 10000 else 0 end
    and a.t2 != b.t2 + case when exists(select 1 from # where gid=1 and t1!=a.t1) and not exists(select 1 from # where gid=1 and t2!=a.t2) then 10000 else 0 end
    or a.t1 = case when exists(select 1 from # where gid=1 and (t1!=a.t1 or t2!=a.t2)) then a.t1 else 10000 end
    where a.gid=2 and b.gid=1
    *//*
    select * from # where gid=2 order by r desc,CAST(RAND(CHECKSUM(NEWID())) * r + 1 AS INT),
    r1 desc,CAST(RAND(CHECKSUM(NEWID())) * r1 + 1 AS INT),
    r2 desc,CAST(RAND(CHECKSUM(NEWID())) * r2 + 1 AS INT),newid()
    */--不喜欢用游标所以多用一次临时表. 意义跟用游标一样,就是拿个indentity列去循环而已.
    select identity(int) nid ,* into #1 from #
    where gid=2
    order by r desc,CAST(RAND(CHECKSUM(NEWID())) * r + 1 AS INT),
    r1 desc,CAST(RAND(CHECKSUM(NEWID())) * r1 + 1 AS INT),
    r2 desc,CAST(RAND(CHECKSUM(NEWID())) * r2 + 1 AS INT),
    newid()declare @i int,@id int,@xid int,@xt1 int,@xt2 int
    set @i=0
    while @i<3
    begin
    set @i=@i+1
    select @xid=id,@xt1=t1,@xt2=t2 from #1 where nid=@i

    /*这里是近似算法,用匹配率得到的.
    update a set willbebid = b.id
    from # a
    inner join #1 b
    on 1=1
    inner join
    (
    select top 1 id from # where willbebid is null and gid=1 order by r desc,CAST(RAND(CHECKSUM(NEWID())) * r + 1 AS INT),
    r1 desc,CAST(RAND(CHECKSUM(NEWID())) * r1 + 1 AS INT),
    r2 desc,CAST(RAND(CHECKSUM(NEWID())) * r2 + 1 AS INT),
    newid()

    ) c
    on a.id=c.id
    where b.nid=@i
    */ select top 1 @id=id from # where willbebid is null and gid=1 order by
    case when t1!=@xt1 and t2!=@xt2 then 0
    when t1!=@xt1 and t2=@xt2 then 1
    when t2!=@xt2 and t1=@xt1 then 2
    else 3
    end,
    r desc,CAST(RAND(CHECKSUM(NEWID())) * r + 1 AS INT),
    r1 desc,CAST(RAND(CHECKSUM(NEWID())) * r1 + 1 AS INT),
    r2 desc,CAST(RAND(CHECKSUM(NEWID())) * r2 + 1 AS INT),
    newid()
    --select @id
    update # set willbebid=@xid where id=@idend

    go select a.id aid,a.t1 at1,a.t2 at2,
    b.id bid,b.t1 bt1,b.t2 bt2
    from tb a
    inner join # c
    on a.id=c.willbebid
    inner join tb b
    on b.id=c.id
    order by 
    case when a.t1!=b.t1 and a.t2!=b.t2 then 0
    when a.t1!=b.t1 and a.t2=b.t2 then 1
    when a.t1=b.t1 and b.t2!=a.t2 then 2
    else 3
    end
    drop table #1
    go
    drop table #
    go
    go
    drop table tb
    go
    /*
    3 1 2 2 2 1
    1 1 1 4 2 2
    6 2 3 5 1 3
    -------------------------------------------------
    5 1 3 2 2 1
    1 1 1 4 2 2
    3 1 2 6 2 3
    -------------------------------------------------
    1 1 1 4 2 2
    2 2 1 5 1 3
    3 1 2 6 2 3
    -------------------------------------------------
    2 2 1 3 1 2
    5 1 3 4 2 2
    1 1 1 6 2 3
    -------------------------------------------------
    4 2 2 1 1 1
    5 1 3 2 2 1
    6 2 3 3 1 2
    不一一贴结果了.*/当然了, 我是按只有6条记录来处理的.
    如果记录个数不定,适当修改查询语句就行了,比如2005中的top 变量的支持, 或2k中用set rowcount或动态语句. 这些是次要的,不影响算法.不多说了.
      

  18.   


    先取出at1<>bt1 and at2<>bt2 并且这样组合出来的数量最多的一种组合方式
    在剩余的里面
    再取出at1<>bt1 and at2=bt2  并且这样组合出来的数量最多的一种组合方式
    再在剩余的里面
    再取出at1=bt1 and at2<>bt2  并且这样组合出来的数量最多的一种组合方式
    再再在剩余的里面
    最后full join 剩余的很难很复杂.
      

  19.   

    出现问题的原因是按重复率+随机取 cnt/2 条记录做为以后连表时的a即gid=2来标识,其它的记录做为连表时的b即gid=1来标识,
    重复率不能得到同gid里记录的相似性.
    想似性: record1.t1!=record2.t1 and record1.t2!=record2.t2 那么相似性最差,(即楼主的需要满足的第一个条件的情况)
    接下来是 r1.t1!=r2.t1 and r1.t2=r2.t2
    再下来是 r1.t1=r2.t1 and r1.t2!=r2.t2
    最后是 r1.t1=r2.t1 and r1.t2=r2.t2那么要依距相似性的分布,尽量将相似性最差的不要分在同一个gid里. cnt(总记录数)如果相似性最差的两条记录(r1,r2)分在同一gid(gid=2或gid=1)里,又没有其它记录(gid=1或gid=2,与前面不同组)与r1或r2相似度最差,那么本能满足第一条件的便选不出了.晚上我又花了两小时对算法进行改进, 将 update ..(top 3 ) 改成用循环update cnt/2 次,每次update一条记录, 使gid=2的 cnt/2 条记录相似度最高,
    那么引起的结果就是 可能出现 剩余的 gid=1的记录中出现 相似性最差的情况.
    然后,再用循环对 gid=1的 cnt/2 条记录两两对比,得到相似性, 将相似性最差的两两,再与 gid=2的记录对比,在一定条件下随机将相似性最差的一对(或一条)记录(在每次循环中)与gid=2的记录进行置换. 即用循环进行相似度分组微调.这样做,在cnt=6,=8,=10这样的小量数据下没有什么问题, 当我插入50记录时,有一定机率会出错.
    后来就又仔细去分析这个问题了.其实这个题与:
    从n1,n2,n3,n4,,nN 的 m个数中找出一些累加使得值分别最接近 x,y,z ,每个nX 只能用一次.
    本质一样.这种题都有一个关健字"尽可能的"结论是,这题需要 穷举.
    不依赖穷举的任何算法,都会不会有完全正确的结果的. 因为一但随机的数组组合对于条件一的组合(任一个组合)一变, 都会引起其它组合的变化.
      

  20.   

    哈哈.fcuandy,这个问题就交给你对穷举吧,人民会记住你的.再见
      

  21.   

    不一定就需要穷举,我进行了一些总结,还有些问题没解决,现共享出来大家共同探讨。
    declare @t1 int,@t2 int,@c1 int,@c2 int
    select @t1=t1,@c1=a.s-b.s from (select t1,2*count(*) s from tb group by t1) a,(select count(*) s from tb ) b where a.s>b.s
    select @t2=t2,@c2=a.s-b.s from (select t2,2*count(*) s from tb group by t2) a,(select count(*) s from tb ) b where a.s>b.s
    A、如果@t1和@t2 均为空,则一定能够符合条件1,用33L 的代码改改就行了。
    B、如果@t1不为空,@t2为空,则我们一定可以做到取出@c1个t1等于@t1的数据(这种组合很简单),并使得剩下的数据满足A,分两部分组合后拼起来就可以了。
    C、如果@t2不为空,@t1为空,同B,只是用@t2和@c2。
    D、如果@t1,@t2均不为空,则取@c1,@c2中较大的(相等就随便取),采用B或C现在的关键是怎么取出这@c个数能够符合要求呢?
      

  22.   

    我觉得你总结的这几个条件只能说是在做穷举时简化或优化算法. 
    很简单的说: 就以你的A 结论为例, @t1,@t2均为空不同情况下(每执行第x次)下有n种组合,设每一种组合都符合条件1, 但是组合一变的化,引起的符合条件2的个数就会产生变化.个数一减少就意味着,在尊循条件1的限制下,第一批(符合条件1)的组合是失败的.比如简单的数据5  //符合条件1的最大组合数
    3  //在上一前提下,符合条件2的最大组合数
    1  //在前两前提下,符合条件3的最大组合数
    下一次可能取到:

    2  //由于第一批组合数据的变化,引起这里的减少.
    2在这种情况下,这个结果不对,要重推回去,使得第2次运算的第二批数据组合数也是3, 这样的结果才是正确的.那么,首先要穷举取出最优的结果, 比如 5,3,1
    然后只能在多次出现最优结果的情况里随机选 一个结果.