[SQL]消除重复的数据
DECLARE @t TABLE(
id_num INT IDENTITY(1,1) UNIQUE CLUSTERED,
i INT
)
INSERT INTO @t(i)
SELECT 1 UNION ALL
SELECT 1 UNION ALL
SELECT 1 UNION ALL
SELECT 2 UNION ALL
SELECT 2 UNION ALL
SELECT 3
DELETE @t
FROM @t AS t,(SELECT MIN(id_num) AS id_num,i FROM @t GROUP BY i) AS a
WHERE t.id_num <> a.id_num AND t.i = a.i
/* DELETE FROM @t WHERE id_num NOT IN (SELECT MIN(id_num) FROM @t GROUP BY i) */
SELECT * FROM @t
id_num INT IDENTITY(1,1) UNIQUE CLUSTERED,
i INT
)
INSERT INTO @t(i)
SELECT 1 UNION ALL
SELECT 1 UNION ALL
SELECT 1 UNION ALL
SELECT 2 UNION ALL
SELECT 2 UNION ALL
SELECT 3
DELETE @t
FROM @t AS t,(SELECT MIN(id_num) AS id_num,i FROM @t GROUP BY i) AS a
WHERE t.id_num <> a.id_num AND t.i = a.i
/* DELETE FROM @t WHERE id_num NOT IN (SELECT MIN(id_num) FROM @t GROUP BY i) */
SELECT * FROM @t
posted on 2004-10-11 09:39 Goodspeed 阅读(1204) 评论(0) 编辑 收藏 举报