我有2张桌子。
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_ids
。 New_ids
对group_owner
和group_owner
有约束力。
group_id
如果该组合已经存在,它将会爆炸。因此,执行以下操作将无效。
user_id
如果该组合不存在,有人可以帮我更新group_owner吗?如果该组合存在,可以删除该组合吗?
以下是group_owner和users表在处理后应该如下:
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
答案 0 :(得分:1)
尝试以下步骤。
从group_owner表中删除不需要的记录。
正如你所说的group_id和user_id组合是唯一的,我们只需要一个group_id = 51和user_id = 1的条目,所以我们可以从group_owner中删除(51,2)和(51,3)组合。
脚本:
;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
脚本:
;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
脚本:
;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