日期:2014-05-17  浏览次数:20740 次

关于重复记录的的更改
有表 T 结构如下:
  id name qty
  1 a 10
  2 b 20
  3 c 40
  4 a 10
现在想更改如下:
  id name qty
  1 a 10
  2 b 20
  3 c 40
  4 a 0
请高手指教!最好能附上代码,谢
 


------解决方案--------------------
SQL code
-- try
update yourTable
   set qty = 0
 where id in (select id
                from (select t.*,
                             row_number() over(partition by name, qty order by id) rn
                        from yourTable t)
               where rn <> 1)

------解决方案--------------------
SQL code
-- TRY IT ..
UPDATE TT T1
   SET QTY = 0
 WHERE EXISTS (SELECT 1
          FROM TT T2
         WHERE T1.NAME = T2.NAME
           AND T1.ID > T2.ID);