我真的不知道你在做什麼,問題不是很清楚。此外,並非所有的SQL是相同的(他們使用的是什麼OP不說了),所以這裏是一個辦法做到這一點在SQL Server:
DECLARE @YourTable table (KeyID int, text1 varchar(10))
INSERT @YourTable VALUES (1,'aaa')
INSERT @YourTable VALUES (2,'bbb')
INSERT @YourTable VALUES (3,'aaa')
INSERT @YourTable VALUES (4,'ccc')
INSERT @YourTable VALUES (5,'ccc')
INSERT @YourTable VALUES (6,'ccc')
INSERT @YourTable VALUES (7,'ddd')
INSERT @YourTable VALUES (8,'aaa')
--just display the first occurrence rows of each text1 value
SELECT
*
FROM (SELECT KeyID,text1,ROW_NUMBER() OVER (PARTITION BY text1 ORDER BY KeyID) AS RowNumber FROM @YourTable) dt
WHERE RowNumber=1
--delete all duplicates
;WITH NumberedRows AS
(SELECT
KeyID,ROW_NUMBER() OVER (PARTITION BY text1 ORDER BY KeyID) AS RowNumber
FROM @YourTable
)
DELETE NumberedRows
WHERE RowNumber>1
select * from @YourTable --<<display remaining rows
OUTPUT:
KeyID text1
----------- ---------- <<from 1st select
1 aaa
2 bbb
4 ccc
7 ddd
(4 row(s) affected)
KeyID text1
----------- ---------- <<from second select after delete
1 aaa
2 bbb
4 ccc
7 ddd
(4 row(s) affected)
使用ROW_NUMBER()OVER Text1 and delete where> 1 –
不知道我理解你的評論,你能否進一步解釋? – htm11h