--建立个function
create table mytest
(
sn int,
code varchar(8),
ins_no varchar(6),
)
goinsert mytest values(1, '01', 'A')
insert mytest values(1, '01', 'A')
insert mytest values(1, '02', 'B')
insert mytest values(2, '01', 'A')
insert mytest values(2, '02', 'B')
insert mytest values(2, '02', 'B')
insert mytest values(4, '03', 'A')
insert mytest values(4, '03', 'B')
insert mytest values(4, '03', 'C')
insert mytest values(5, '03', 'B')
insert mytest values(5, '03', 'C')
insert mytest values(5, '03', 'E')
insert mytest values(6, '02', 'A')
insert mytest values(6, '02', 'C')
insert mytest values(6, '02', 'D')
insert mytest values(7, '02', 'C')
insert mytest values(7, '02', 'D')
insert mytest values(8, '02', 'C')
insert mytest values(8, '02', 'D')
go--select * from mytest
CREATE function MyFc_1(@content1 varchar(50))
returns varchar(2000)
as 
begin
declare @str varchar(2000)
set @str=''
select @str=@str+code+ins_no  from mytest where sn=@content1 order by code,ins_no
return @str
end
go----然后用下面的语句
select * from mytest where sn in ( select min(sn) as sn from mytest group by dbo.myfc_1(sn))
----删除实验环境
drop function  MyFc_1
drop table mytest
-- 
-- 字段SN是用来分组的。如果不同SN下的记录完全一样(除了SN本身),
-- 包括记录条数、字段内容,则我希望只保留一个分组,
-- 例子中分组7和8满足这种情况,所以把分组7(或8)剔除掉。
-- 能不能用一个查询语句做到?

解决方案 »

  1.   

    多谢solidpanther,你所建函数的思路很好。但是可不可以不建函数用一条查询语句做到?谢谢大家。
      

  2.   

    一句话,没感觉。。
     Jianli2004(健力) :不错,不过最好贴到水版。
      

  3.   

    那如果 是同一SN下的重复如果要考虑 选出重复的来的语句是下面的语句,
    然后我晕了,实在不想写这么低效率的语句,就停手了。。 select sn from mytest t1

    where sn  not in (select sn3 from 
    (
    select t3.sn sn3,t4.sn sn4,t3.code tc3 ,t3.ins_no ti3,t4.code tc4 ,t4.ins_no ti4
     from mytest t3 full join mytest t4 on t3.code=t4.code and t3.ins_no=t4.ins_no and t3.sn<>t4.sn
    and (select count(*) from mytest t6 where t6.sn=t3.sn and t6.code=t3.code and t6.ins_no=t3.ins_no)
    =(select count(*) from mytest t6 where t6.sn=t4.sn and t6.code=t4.code and t6.ins_no=t3.ins_no)
     ) t5 where t5.sn3=t1.sn and t5.sn4 is  null --and sn3 <sn4
        )
      

  4.   

    只想到一种方法写出来,将视图 select t3.sn sn3,t4.sn sn4,t3.code tc3 ,t3.ins_no ti3,t4.code tc4 ,t4.ins_no ti4
     from mytest t3 full  join mytest t4 on t3.code=t4.code and t3.ins_no=t4.ins_no and t3.sn<>t4.sn
    and (select count(*) from mytest t6 where t6.sn=t3.sn and t6.code=t3.code and t6.ins_no=t3.ins_no)
    =(select count(*) from mytest t6 where t6.sn=t4.sn and t6.code=t4.code and t6.ins_no=t3.ins_no)
    和自己Join。。
      

  5.   

    delete A from mytest A,mytest B
    where A.ins_no=B.ins_no 
    and A.sn>B.sn
    and not exists (select 1 
                    from mytest 
                    where sn=A.sn 
                    and code+','+ins_no not in(select code+','+ins_no 
                                               from mytest 
                                               where sn=B.sn 
                                            ) )
    and not exists (select 1 
                    from mytest 
                    where sn=B.sn 
                    and code+','+ins_no not in(select code+','+ins_no 
                                               from mytest 
                                               where sn=A.sn 
                                               ) )
    and not exists(select 1 from 
    (select code+','+ins_no as cod,count(*) con 
     from mytest 
     where sn=A.sn 
     group by code+','+ins_no) C,
    (select code+','+ins_no as cod,count(*) con 
     from mytest 
     where sn=B.sn 
     group by code+','+ins_no) D
        where C.cod=D.cod and C.con<>D.con)select * from mytest
      

  6.   

    再来一问题,在WIN2003上装SQL2000企业版本有问题,并1433端口telnet 不通。请高手回答。
      

  7.   

    多谢上面各位朋友的帮忙,特别感谢solidpanther(我爱机器猫)和j9988(j9988)两位。朋友的做法,他是个博士不太熟悉MSSQL的,他查阅了语法很快做出来了。
    他的分析和做法
    两个要点,
    一, 要对数据按sn进行分组
    二, 每组数据相同的充分必要条件为件数相等且相同记录件数相等且记录相同
    select * from mytest t1
    where sn in
    (select sn from
    (select sn, count(*) as cnt from mytest group by sn) t1 --MYGROUP
    where not exists(
    select sn from
    (select sn, count(*) as cnt from mytest group by sn) t2 --MYGROUP
    where t2.sn < t1.sn and t1.cnt=t2.cnt
    and not exists(
    select null from
    (select sn, code, ins_no, count(*) as cnt from mytest group by sn, code, ins_no) t3 --MYITEM
    where t3.sn = t1.sn
    and not exists(
    select null from
    (select sn, code, ins_no, count(*) as cnt from mytest group by sn, code, ins_no) t4 --MYITEM
    where t4.sn = t2.sn
    and t4.code=t3.code
    and t4.ins_no=t3.ins_no
    and t4.cnt=t3.cnt)))) ---------------------------------------------------------------------------
    我的分析和做法:
    两组纪录相同的充分必要条件是
    1.根据sn分组纪录数相同
    2.根据sn,code+ins_no分组纪录数相同
    3.code,ins_no所在字段值相同
    select * from mytest where sn in(
    select min(a.sn)as sn
    from mytest a,(select sn,code+ins_no as series,count(1) as num from mytest b group by sn,code+ins_no)b,(select sn,count(*) as num from mytest group by sn) c
    where a.sn=b.sn and a.sn=c.sn
    group by a.code,a.ins_no,b.series,b.num,c.num
    )
    order by sn --min换成max也可以。
      

  8.   

    TO  j9988(j9988)能不能用一组数据来证明我写的语句不能吗。因为我没发现自己的分析错在哪里。谢谢。
      

  9.   

    原来我的想法和博士一样啊,可惜思维方向错了,应该直接在Join里过滤, 不是先Join 再过滤。
    学习!up
      

  10.   

    TO realgz(realgz) 我这位博士朋友熟悉oracle,他是用MSSQL里的not exists来代替oracle的minus的做法。呵呵,可惜我minus不懂,对not exists也不太会用。
      

  11.   

    就是在每次MIN或MAX时都被滤去的。
    就不存在了。万万没想到。
      

  12.   

    JSS,是啊,有时候俺们这个弯死活就是没拐过来啊
      

  13.   

    其实你的思维方法,跟REALGZ很象。
    你们往往会出奇制胜。
    PFPF!
      

  14.   

    realgz,其实你们一开始的想法就很象。看看你前面的join思路。差不多的。
    你们join用的是求同,
    而博士和我的是排除法。
      

  15.   

    TO  j9988(j9988)和realgz(realgz)谢谢你们的帮助。其实,我是not exists不太会用,且看到我朋友的答案,不服,硬想出来的。让你们见笑了。我还有一个问题,就是怎么修改自己帖子的言论。
      

  16.   

    谢谢。我找了一下,刷新帖子好像只能用browse上的功能,csdn好像不提供这样的功能,是不是这样的。还有这里可可以上传附件?还有如果答案正确的话,我就结贴了。结贴加分是不是在回帖人下面点击加分就可以了。
      

  17.   

    要我写SELECT我会这么写:select * from mytest where sn in (
    select A.sn from mytest A,mytest B 
    where a.code=B.code 
    and a.ins_no=B.ins_no 
    and A.sn<>B.sn
    and (exists(select 1 from mytest where sn=A.sn and code+','+ins_no not in 
        (select code+','+ins_no from mytest where sn=B.sn))
    or  exists(select 1 from mytest where sn=A.sn and code+','+ins_no not in 
        (select code+','+ins_no from mytest where sn=A.sn))
    or  exists(select 1 where (select count(*) from mytest where sn=A.sn and code+','+ins_no=A.code+','+A.ins_no)<>
    (select count(*) from mytest where sn=B.sn and sn<A.sn and code+','+ins_no=A.code+','+A.ins_no)
    )))
      

  18.   

    但效率还是GROUP BY高。上面的语句比博士高,但你GROUP BY低。
      

  19.   

    我从头到尾都以为要写DELETE,因为看了上一贴、
      

  20.   

    TO j9988(j9988)厉害,我是想了好久才想出来的。我这位朋友擅长设计。他的分析能力很强,我比较佩服的。呵呵,他对MSSQL不熟的。是先分析出来,再查语法做的。