请问
表1:结构如下
pkid skuid   locid    cntqty
 1   123     c001       2
 2   123     c001       3
 3   234     c002       4
 4   234     c002       4
 5   123     c001       1现在需要保留重复数据(skuid+locid相等即为重复)中cntqty 值较大的一个,如何操作呢?谢谢

解决方案 »

  1.   

    delete cntqty from cntqty c1,(select pkid,skuid,locid,max(cntqty) as cntqty from 表1 group by pkid,skuid,locid) c2 where c1.pkid=c2.pid and c1.skuid=c2.skuid and c1.locid=c2.locid and c1.cntqty <> c2.cntqty;
      

  2.   

    例如:select * from 表1 where  skuid=(select max(skuid) from 表一 group by skuid)然后将查询表保存就好了。
    要完成你的要求需要先生成一个临时表,在临时表中需要包含‘skuid+locid’(用‘+’将两个字段连接起来就可以了)详细的代码为:
    select pkid,skuid+locid ,cntqty in #<新表的名称>from 表一
    select pkid ,skuid+locid  ,cntqty from <新表的名称> where cntqty=
                     (select pkid,skuid+locid ,cntqty where cntqty=
                            select max(cntqty) from <新表的名称> group by skuid+locid)
      

  3.   

    保留重复数据(skuid+locid相等即为重复)中cntqty 值较大的一个
    delete a from ta a where cntqty not in (select max(cntqty) from ta where skuid=a.skuid and locid =a.locid )
      

  4.   

    declare @ta table(pkid int, skuid int,   locid  varchar(4),   cntqty int)
    insert @ta
    select  1,   123,     'c001',       2
    union all select  2,   123,     'c001',       3
    union all select  3,   234,     'c002',       4
    union all select  4,   234,     'c002',      4
    union all select  5,   123,     'c001',       1delete a from @ta a where  pkid not in
    (select top 1 pkid from @ta where skuid=a.skuid and locid =a.locid order by cntqty desc )select *from @ta(所影响的行数为 5 行)
    (所影响的行数为 3 行)pkid        skuid       locid cntqty      
    ----------- ----------- ----- ----------- 
    2           123         c001  3
    4           234         c002  4(所影响的行数为 2 行)
      

  5.   


    if object_id('ta')>0 drop table tacreate table ta (pkid smallint identity(1,1),skuid varchar(12),locid varchar(20),cntqty smallint) insert into ta select '123','c001',2 union all select '123','c001',3 union all
     select '234','c002',4 union all select '234','c002',4 union all select '123','c001',1--执行
    select * from 
    (
    select a.pkid,a.skuid,a.locid,a.cntqty from ta a inner join (select skuid,locid,max(cntqty) as cntqty from ta group by skuid,locid) b on 
    a.skuid=b.skuid and a.locid=b.locid and a.cntqty=b.cntqty
    ) t order by t.pkid/*结果
    pkid   skuid        locid                cntqty
    ------ ------------ -------------------- ------
    2      123          c001                 3
    4      234          c002                 4(2 行受影响)
    */