如果不存在2列的组合则更新

时间:2016-09-09 00:08:59

标签: sql sql-server sql-update

我有2张桌子。

group_owner

group_id  |   user_id
--------- | ----------
  51      |     1
  51      |     2
  51      |     3
  52      |     2
  53      |     4

用户

user_id   |   username
--------- | -----------
   1      |    John
   2      |    John
   3      |    John
   4      |    Peter
user_id的{​​{1}}与group_owner的{​​{1}}具有外键关系。

我正从users表中删除重复项。如果有重复项,我需要让用户保持最低ID,并删除其余部分。我创建了一个临时表,如下所示。

user_id

我需要将users替换为SELECT t2.id AS Old_id, t1.MinID AS New_id INTO #Translation FROM (SELECT MIN(id) AS MinID, username FROM users GROUP BY username) t1 INNER JOIN users t2 ON t1.username = t2.username 表中的Old_idsNew_idsgroup_ownergroup_owner有约束力。

group_id

如果该组合已经存在,它将会爆炸。因此,执行以下操作将无效。

user_id

如果该组合不存在,有人可以帮我更新group_owner吗?如果该组合存在,可以删除该组合吗?

以下是group_owner和users表在处理后应该如下:

group_owner

ALTER TABLE [dbo].[group_owner] ADD  CONSTRAINT [pk_group_owner] PRIMARY KEY CLUSTERED 
(
    [group_id] ASC,
    [user_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
GO

用户

UPDATE group_owner 
  SET group_owner.[user_id] = New_id
  FROM group_owner 
    INNER JOIN #Translation T
      ON group_owner.[user_id] = T.Old_id
  WHERE T.Old_id <> T.New_id

参考:http://weblogs.sqlteam.com/jeffs/archive/2004/10/07/2190.aspx

1 个答案:

答案 0 :(得分:1)

尝试以下步骤。

  1. 从group_owner表中删除不需要的记录。

    正如你所说的group_id和user_id组合是唯一的,我们只需要一个group_id = 51和user_id = 1的条目,所以我们可以从group_owner中删除(51,2)和(51,3)组合。

  2. 脚本:

    ;with cte_1
        as
        (SELECT Username,[user_id],COUNT([user_id])OVER(PARTITION BY Username Order by [Username]) as CNT
        FROM users  )
        ,cte_2
        as
        (SELECT Username,a.[user_id],b.group_id  , ROW_NUMBER()OVER(PARTITION BY b.group_id Order by [user_id]) as Rno
        FROM cte_1 a
        JOIN group_owner b on a.[user_id]=b.[user_id]
        WHERE CNT>1)
        DELETE c3
        FROM cte_2 c2
          JOIN group_owner c3 on c2.group_id=c3.group_id and c2.[user_id]=c3.[user_id]
        WHERE Rno>1
    
    1. 下一步是使用user table中的Minimum user_id更新group_owner表中的[user_id]。
    2. 脚本:

      ;with cte_1
      as
      (SELECT Username,[user_id]
       ,COUNT([user_id])OVER(PARTITION BY Username Order by [Username]) as CNT
       ,MIN([user_id])OVER(PARTITION BY Username Order by [user_id]) as [MINUserID]
      FROM users  )
      UPDATE b
        SET b.user_id=a.[MINUserID]
      FROM cte_1 a
       JOIN group_owner b on a.[user_id]=b.[user_id]
       WHERE CNT>1
      
      1. 第三步也是最后一步是通过保持用户ID的最小值来从User表中删除重复的条目。
      2. 脚本:

        ;with cte_1
            as
            (SELECT Username,[user_id],ROW_NUMBER()OVER(PARTITION BY Username Order by [user_id]) as RNO
            FROM users  )
            DELETE 
            FROM cte_1
            WHERE RNO>1