带有重复值的update语句

时间:2012-05-04 07:01:09

标签: sql-server-2005

嗨我有这样的表 俱乐部卡

AssoicaticeCard      MainCard  custID
A                       A           1
B                       B           1
C                       A           1
AA                      AA          2
BC                      BC          2
CC                      CC          2
Ar                      Ar          3
Bs                      Bs          3
Cv                      Cv          3

现在我需要将结果设置为这样,我的AssoicaticeCard和MainCard已正确连接

我需要显示这样的结果 如何为此创建更新语句以获取

AssoicaticeCard      MainCard  custID
A                       A           1
B                       A           1
C                       A           1
AA                      AA          2
BC                      AA          2
CC                      AA          2
Ar                      Ar          3
Bs                      Ar          3
Cv                      Ar          3

我有一个表格,数据与whic是不一致的,现在需要写一个更新statemenyt来解决问题。我的桌子有大约100万条记录 任何想法如何得到它,任何帮助都会很棒

由于 王子

2 个答案:

答案 0 :(得分:2)

update T1
set MainCard = T2.AssoicaticeCard
from YourTable as T1
  inner join
      (
        select min(AssoicaticeCard) as AssoicaticeCard,
               custID
        from YourTable
        group by custID
      ) T2
    on T1.custID = T2.CustID

答案 1 :(得分:0)

update clubcard set Maincard = 'A' where custID = 1;
update clubcard set Maincard = 'AA' where custID = 2;
update clubcard set Maincard = 'Ar' where custID = 3;