表test的结果如下:序号  字段1   字段2   字段3
1      a       a       6
2      b       c       2
3      b       d       2
4      b       e       2
5      b       e       5要求:1.只要字段1和字段2的值都相同,就认为记录是相同的。
2.只要字段3的值相同,就认为记录是相同的。根据上述表可知最终的结果是2条不同记录。如果通过sql语句得到结果:2  ?

解决方案 »

  1.   


    select * from T as tmp
    where (select count(*) from T where 字段3=tmp.字段3)=1
    and 字段1<>字段2
      

  2.   

    create table T(序号 int, 字段1 char(1), 字段2 char(1), 字段3 int)
    insert T select   1,      'a',       'a',       6
    union all select 2,      'b',       'c',       2
    union all select 3,      'b',       'd',       2
    union all select 4,      'b',       'e',       2
    union all select 5,      'b',       'e',       5select * from T as tmp
    where (select count(*) from T where 字段3=tmp.字段3)=1
    and 字段1<>字段2--result
    序号          字段1  字段2  字段3         
    ----------- ---- ---- ----------- 
    5           b    e    5(1 row(s) affected)
      

  3.   

    select distinct 字段3 from test where 字段1 <> 字段2
      

  4.   

    declare @t table(序号 int, 字段1 char(1), 字段2 char(1), 字段3 int)
    insert @T select   1,      'a',       'a',       6
    union all select 2,      'b',       'c',       2
    union all select 3,      'b',       'd',       2
    union all select 4,      'b',       'e',       2
    union all select 5,      'b',       'e',       5select count(序号) from @t a where not exists
    (select 1 from @t where (字段3=a.字段3 or (字段1=a.字段1 and 字段2=a.字段2)) and 序号<a.序号)--结果
    ----------- 
    2
      

  5.   

    如要看结果的话(相同记录取第一条)
    declare @t table(序号 int, 字段1 char(1), 字段2 char(1), 字段3 int)
    insert @T select   1,      'a',       'a',       6
    union all select 2,      'b',       'c',       2
    union all select 3,      'b',       'd',       2
    union all select 4,      'b',       'e',       2
    union all select 5,      'b',       'e',       5select * from @t a where not exists
    (select 1 from @t where (字段3=a.字段3 or (字段1=a.字段1 and 字段2=a.字段2)) and 序号<a.序号)
    --结果
    序号          字段1  字段2  字段3         
    ----------- ---- ---- ----------- 
    1           a    a    6
    2           b    c    2(所影响的行数为 2 行)
      

  6.   

    --這樣?select * from T as tmp
    where not exists(select 1 from T where (字段1=tmp.字段1 and 字段2=tmp.字段2) or 字段3=tmp.字段3)
      

  7.   

    select * from T a
    where not exists
    (select 1 from T where 字段1+字段2=a.字段1+a.字段2 or 字段3=a.字段3)
      

  8.   

    hinco(桃色德鲁依) ( ) 正解~
      

  9.   

    if object_id('pubs..tb') is not null
       drop table tb
    gocreate table tb
    (
    序号  int,
    字段1 varchar(1),
    字段2 varchar(1),
    字段3 varchar(1)
    )insert into tb(序号,字段1,字段2,字段3) values(1,'a','a','6')
    insert into tb(序号,字段1,字段2,字段3) values(2,'b','c','2')
    insert into tb(序号,字段1,字段2,字段3) values(3,'b','d','2')
    insert into tb(序号,字段1,字段2,字段3) values(4,'b','e','2')
    insert into tb(序号,字段1,字段2,字段3) values(5,'b','e','5')select count(*) as result from
    (
      select 字段1,字段2 from
      (
        select * from tb where 字段3 in 
        (select 字段3 from tb group by 字段3 having count(*) = 1)
      ) t
      group by 字段1,字段2 having count(*) = 1
    ) mdrop table tbresult      
    ----------- 
    2(所影响的行数为 1 行)
      

  10.   

    hinco(桃色德鲁依) 的是最正确的
    而dawugui(潇洒老乌龟) 的对于增加一条新记录,比如
    变成如下以后:
    序号 字段1 字段2 字段3
    1 a a 6
    2 b c 2
    3 b d 2
    4 b e 2
    5 b e 5
    6 b f 5得到的结果是1,正确的应该还是2才对啊。
      

  11.   

    顺便说一下严正了一下
    hinco(桃色德鲁依)的可用
    dawugui(潇洒老乌龟) ( 两星(中级)) 信誉:100
    的没看透
      

  12.   

    select
    (select count(*) from
    (select distinct 字段3 from @test t where 
    (select count(字段3) from @test where 字段3=t.字段3)>1) as a)
    +
    (select count(*) from @test group by 字段1,字段2 having 字段1=字段2)
      

  13.   

    我自己验证了一下,我的写法也是错的……
    好像上面的所有写法都有问题- -!
    对多次关联且顺序不同意的数据,结果都是错的
    如这样的数据
    insert @T select   1,      'a',      'a',      6
    union all select 2,      'b',      'h',      5
    union all select 3,      'b',      'd',      2
    union all select 4,      'b',      'e',      2
    union all select 5,      'b',      'e',      5
    union all select 7,      'c',      'd',      6
    union all select 8,      'b',      'd',      6
      

  14.   

    仔细分析了一下,这其实是一个简单的图论问题,也就是求一个图有几个连通区域,用递归或者循环可以解决,这里用存储过程递归的方法
    先创建递归存储过程
    create proc sp_Searchsame
    as
    declare @count int
    insert into #temp1 
    select * from #temp a where 
      exists(select 1 from #temp where (字段1=a.字段1 and 字段2=a.字段2) or 字段3=a.字段3)
      and not exists(select 1 from #temp1 where 序号=a.序号) 
    if @@rowcount=0
    begin
      insert into #temp1 
      select top 1 * from #temp a where 
        not exists(select 1 from #temp1 where 序号=a.序号) order by 序号
      if @@rowcount=0
        set @count=0
      else
      begin
        exec @count=sp_Searchsame
        set @count=@count+1
      end
    end
    else
      exec @count=sp_Searchsame
    return @count
    --然后具体数据测试
    create table #temp(序号 int, 字段1 char(1), 字段2 char(1), 字段3 int)
    insert #temp select   1,      'a',      'a',    6 
    union all select 2,      'b',      'h',      5
    union all select 3,      'b',      'd',      2
    union all select 4,      'b',      'e',      2
    union all select 5,      'b',      'e',      5
    union all select 7,      'c',      'd',      6
    union all select 8,      'b',      'd',      6
    --创建临时表#temp1
    create table #temp1(序号 int, 字段1 char(1), 字段2 char(1), 字段3 int)
    --具体执行过程
    declare @count int
    delete from #temp1
    exec @count=sp_Searchsame
    print @count
    --结果
    1
      

  15.   

    declare @t table(id int ,f1 varchar(10),f2  varchar(10),f3  varchar(10))insert into @t values(1,'a','a','6')
    insert into @t values(2,'b','c','2')
    insert into @t values(3,'b','d','2')
    insert into @t values(4,'b','e','2')
    insert into @t values(5,'b','e','5')
    select * from @t where id not in
    (select b.id from @t a,@t b where ((a.f1=b.f1 and a.f2=b.f2) or(a.f3=b.f3)) and (a.id < b.id))
      

  16.   

    select * from @t where id not in
    (select b.id from @t a,@t b where ((a.f1=b.f1 and a.f2=b.f2) or(a.f3=b.f3)) and (a.id < b.id))
    id          f1         f2         f3
    ----------- ---------- ---------- ----------
    1           a          a          6
    2           b          c          2
      

  17.   

    select
    (select count(*) from (select distinct 字段3 from @test group by 字段3 having count(字段3)>1) as a
    )+
    (select count(*) from @test t1 where (select count(*) from @test where 字段1=t1.字段1 and 字段2=t1.字段2)>1
    )-
    (select count(*) from @test t2 where (select count(*) from @test where 字段1=t2.字段1 and 字段2=t2.字段2 and 字段3=t2.字段3)>1
    )*2
      

  18.   

    select
    (select count(*) from (select distinct 字段3 from @test group by 字段3 having count(字段3)>1) as a
    )+
    (select count (*) from (select distinct 字段1,字段2 from @test t1 where (select count(*) from @test where 字段1=t1.字段1 and 字段2=t1.字段2)>1) as e
    )这么写吧
      

  19.   

    select
    (select count(*) from (select distinct 字段3 from @test group by 字段3 having count(字段3)>1) as a
    )+
    (select count (*) from (select distinct 字段1,字段2 from @test t1 where (select count(*) from @test where 字段1=t1.字段1 and 字段2=t1.字段2)>1) as e
    )
    -
    (select count (*) from (select distinct 字段1,字段2,字段3 from @test t2 where
    (select 
    case 
    when count(*)=(select count(*) from @test where 字段1=t2.字段1 and 字段2=t2.字段2)
    and count(*)=(select count(*) from @test where 字段3=t2.字段3)
    then count(*)
    else
    1
    end as l
    from @test as t3 where 字段1=t2.字段1 and 字段2=t2.字段2 and 字段3=t2.字段3)>1) as h)写的不好   不过是正确答案
      

  20.   

    declare @t table(序号 int, 字段1 char(1), 字段2 char(1), 字段3 int)
    insert @T select   1,      'a',      'a',      6
    union all select 2,      'b',      'c',      2
    union all select 3,      'b',      'd',      2
    union all select 4,      'b',      'e',      2
    union all select 5,      'b',      'e',      5select count(*) from @t a 
    left join (select * from @t) b on ((a.字段1=b.字段1 and a.字段2=b.字段2) or (a.字段3=a.字段3)) and a.序号<b.序号)
    where b.序号 is null
      

  21.   

    而dawugui(潇洒老乌龟) 的对于增加一条新记录,比如
    变成如下以后:
    序号字段1字段2字段3
    1aa6
    2bc2
    3bd2
    4be2
    5be5
    6bf5得到的结果是1,正确的应该还是2才对啊。1.只要字段1和字段2的值都相同,就认为记录是相同的。
    2.只要字段3的值相同,就认为记录是相同的。上面的数据只有1aa6满足条件,结果是1,怎么会是2?
      

  22.   

    to dawugui(潇洒老乌龟)
    LZ要找的是有几种不同的记录,而不是让你找出没有重复记录的,汗
    这样求连通区域的只能用循环或者递归,想一个语句解决是很困难,如果有人觉得能用一个语句解决,请先用给的数据测试一下你得出的结果是不是1
      

  23.   

    注意审题啊   怎么可能是1 
    1aa6
    2bc2
    3bd2
    4be2
    5be5
    6bf5
    1.只要字段1和字段2的值都相同,就认为记录是相同的。
    2.只要字段3的值相同,就认为记录是相同的。字段1和字段2相同  也就是  be 和be 相同 就应该是记录是相同的
    字段3重复  就应该是相同的
    这题唯一注意的是 出现字段1 2 3 完全相同的记录并且没有在其他行出现的
      

  24.   

    select
    (select count(*) from (select distinct 字段3 from @test group by 字段3 having count(字段3)>1) as a
    )+
    (select count (*) from (select distinct 字段1,字段2 from @test t1 where (select count(*) from @test where 字段1=t1.字段1 and 字段2=t1.字段2)>1) as e
    )
    -
    (select count (*) from (select distinct 字段1,字段2,字段3 from @test t2 where
    (select 
    case 
    when count(*)=(select count(*) from @test where 字段1=t2.字段1 and 字段2=t2.字段2)
    and count(*)=(select count(*) from @test where 字段3=t2.字段3)
    then count(*)
    else
    1
    end as l
    from @test as t3 where 字段1=t2.字段1 and 字段2=t2.字段2 and 字段3=t2.字段3)>1) as h)这就是正确答案
      

  25.   

    to no1francis(天使赶不走)很遗憾你的答案是错的
    对于这组数据
    declare @test table(序号 int, 字段1 char(1), 字段2 char(1), 字段3 int)
    insert @test select   1,      'a',      'a',      6
    union all select 2,      'b',      'h',      5
    union all select 3,      'b',      'd',      2
    union all select 4,      'b',      'e',      2
    union all select 5,      'b',      'e',      5
    union all select 7,      'c',      'd',      6
    union all select 8,      'b',      'd',      6
    正确答案应该是1,而你的语句是5
      

  26.   

    我怎么同天没上~~题都看不懂了~~~
    题:
    序号  字段1   字段2   字段3
    1      a      a      6
    2      b      c      2
    3      b      d      2
    4      b      e      2
    5      b      e      5
    1.只要字段1和字段2的值都相同,就认为记录是相同的。
    2.只要字段3的值相同,就认为记录是相同的。根据上述表可知最终的结果是2条不同记录。如果通过sql语句得到结果:2  ?郁闷:
    1.只要字段1和字段2的值都相同,就认为记录是相同的。
    4和5相同
    2.只要字段3的值相同,就认为记录是相同的。
    2,3,4相同那不就只有1不同了吗~~~怎么会有2条~~
    楼上的解4下~~~看晕了~~~
      

  27.   

    select 序號,字段1,字段2,字段3,0 as 標記  into #xzx from tt
    declare @upfiled1 varchar
    declare @upfiled2 varchar
    declare @upfiled3 varchar
    declare @bj int
    set @upfiled1=''
    set @upfiled2=''
    set @upfiled3=''
    update #xzx set @bj= case when @upfiled1=字段1 and @upfiled2=字段2 or @upfiled3=字段3 then 0 else 1 end,標記=@bj,
                              @upfiled1=字段1,@upfiled2=字段2,@upfiled3=字段3
     select count(*) from #xzx where 標記=1
      

  28.   

    改善一下
    select 序號,字段1,字段2,字段3,0 as 標記  into #xzx from tt order by 字段3,字段1﹐字段2
    '說明一下﹐第一第語句將按照可能相同的記錄放在一起。
    declare @upfiled1 varchar
    declare @upfiled2 varchar
    declare @upfiled3 varchar
    declare @bj int
    set @upfiled1=''
    set @upfiled2=''
    set @upfiled3=''
    update #xzx set @bj= case when @upfiled1=字段1 and @upfiled2=字段2 or @upfiled3=字段3 then 0 else 1 end,標記=@bj,
                              @upfiled1=字段1,@upfiled2=字段2,@upfiled3=字段3
     select count(*) from #xzx where 標記=1
      

  29.   

    to 桃色 
    再次说下你审错题了
    序号  字段1   字段2   字段3
    1      a      a      6
    2      b      c      2
    3      b      d      2
    4      b      e      2
    5      b      e      5
    这是楼主得题 
    最后结果是2
    楼主说只要字段1和字段2得值都相同  说得不是序号是1得a a 而是序号4和5出现得
    字段1和字段2得值相同
    mygod 我没错额
      

  30.   

    declare @AllCount int
    select  @Allcount=count(*) from TT
    select count(*)+1 
    from(
    select  a.序號,count(*) as 數量
    from tt a inner join tt b on a.序號<>b.序號
    where a.字段3<>b.字段3 and  (a.字段1<>b.字段1 or a.字段2<>b.字段2) 
    group by  a.序號
    having count(*)=@Allcount-1) a
      

  31.   

    to no1francis(天使赶不走)
    LZ给的数据不够典型,他给的数据答案是2没错,用我原来错误但是很简单的方法(我在4搂发的)算出来也是2,但是用我后来给的数据算就是错的
    你要试试我给的那组数据
    其实这个连通图问题作可能出错的情况是下面这样的
    字段1 字段2 字段3
    e       f     2
    a       b     1
    c       d     1
    c       d     2
    这四条记录其实是相同的只算1条记录,可是一条语句很难算出来
      

  32.   

    to hinco(桃色德鲁依) 分析的真透彻,佩服!但有没有办法得到正确的sql答案呢?继续顶。
      

  33.   

    条件如下:
    1.只要字段1和字段2的值都相同,就认为记录是相同的。
    2.只要字段3的值相同,就认为记录是相同的。
    比如:
    序号  字段1 字段2 字段3
    1      e       f     2
    2      a       b     1
    3      c       d     1
    4      c       d     2结果是1序号  字段1   字段2   字段3
    1      a      a      6
    2      b      c      2
    3      b      d      2
    4      b      e      2
    5      b      e      5
    的结果是2
      

  34.   

    jycjyc(果果)~~~劳烦你把结果是哪一条给出来~~因为我还是不懂..........
      

  35.   

    其实这题我之前那个递归算法的回帖已经解释很清楚了,至于有没有更简单的方法,我不知道……反正用1个SQL语句解决是很难的(当然如果是传结果集的递归函数那就另说了,那已经不是一个SQL语句的范畴了)
      

  36.   

    hinco(桃色德鲁依) 你能把结果写一下吗~~别让不知道结果人就去看别人算法~浪费时间序号  字段1 字段2 字段3
    1      e       f     2
    2      a       b     1
    3      c       d     1
    4      c       d     2结果是哪一条序号  字段1   字段2   字段3
    1      a      a      6
    2      b      c      2
    3      b      d      2
    4      b      e      2
    5      b      e      5
    的结果是哪两条
      

  37.   

    不是问你哪两条,我打个比方,把每条记录看作是一个节点,然后给这些节点连线,连线的规则就是:1.只要字段1和字段2的值都相同的两记录(节点)连线。2.只要字段3的值相同的两记录(节点)。于是就行成了一个图,现在问这个图有几个连同区域……
    用你上面的数据说就是
    序号  字段1 字段2 字段3
    1      e       f     2
    2      a       b     1
    3      c       d     1
    4      c       d     2
    四条记录都关联,所以答案是1
    序号  字段1   字段2   字段3
    1      a      a      6
    2      b      c      2
    3      b      d      2
    4      b      e      2
    5      b      e      5
    2、3、4、5关联,1独立,于是有2个连同区域,所以答案是2
      

  38.   

    还有插播广告的....
    谢谢hinco(桃色德鲁依).....LZ真会误倒人...
    你怎么一下就看懂了~~郁闷~!!
      

  39.   

    --呵呵~~我越来越爱sql了
    create table #temp(序号 int, 字段char char(1), 字段int int)
    insert #temp select   1,      'a',    6 
    union all select 2,      'b',      3
    union all select 3,      'c',      2
    union all select 4,      'd',      5
    union all select 5,      'd',      6
    union all select 7,      'f',      6
    union all select 8,      'c',      9
    union all select 9,      'a',      9select 字段int into #1 from #temp group by 字段int
    select top 1 * into #2 from #1declare @count int
    set @count = 0while(exists(select 1 from #1))
    begin
    set @count = @count+1
    while(
    exists(select 1 
    from #temp a where 字段char in 
    (select 字段char 
    from #temp where 字段int in(select 字段int from #2))
    and not EXISTS(select 1 from #2 b where b.字段int=a.字段int))
    )
    begin
    insert #2 select 字段int 
    from #temp a where 字段char in 
    (select 字段char 
    from #temp where 字段int in(select 字段int from #2))
    and not EXISTS(select 1 from #2 b where b.字段int=a.字段int)
    enddelete from  #1
    where 字段int in(select 字段int from #2)
    delete #2
    insert #2 select top 1 * from #1
    enddrop table #1
    drop table #2print @count
      

  40.   

    hinco(桃色德鲁依)高手解释得非常到位!呵呵~~
      

  41.   

    hinco(桃色德鲁依)~~是很能猜....
    可是我觉得~猜到了就要能做到~~~
    hinco(桃色德鲁依)~~以后我们要相互学习啊....
      

  42.   

    你们太有才了,像marco08(天道酬勤) ( ) 信誉:100    Blog 
    致敬!给出这么详细的解释!
      

  43.   

    to w75251455(砍破)
    我很早就已经给出答案了,看我2007-2-16 9:51:36回的贴,怎么就是没人看呢,汗……
      

  44.   

    没仔细看回帖,原来LZ一直以为我没给正确答案啊,真是郁闷
    在LZ发帖的第二天,我就把用存储过程递归的正确答案写出来了(第一天回的那个方法是错的,但第二天我自己更正了),可能LZ看那么长没去试验……