主表格为Table2
,我想删除RollID
中的重复Table1
,但保留1,如下例6456
(注意:主键表ID是自动的) -incremental)
表1(子表)
Table1_ID RollID
1 6456
2 6456
3 6459
表2(父母)
Table2_ID RollID
1 6456
2 6459
我怎样才能达到这种情况..你的帮助很受赞赏......
答案 0 :(得分:0)
试试这个:
WITH CTE AS(
SELECT Table1_ID, RollID,
RN = ROW_NUMBER()OVER(PARTITION BY RollID ORDER BY RollID)
FROM dbo.Table1
)
DELETE FROM CTE WHERE RN > 1
答案 1 :(得分:0)
DELETE FROM Table1 WHERE ID NOT IN (SELECT MIN(ID) FROM Table1 GROUP BY ROLLID)