如何在 SQL 服务器 table 中查找重复值?
How do I find duplicate values in a SQL Server table?
我的 SQL Server 2008 数据库有一个名为 Reading
的 table,其列 Timestamp
是一个 DateTime 值,Probe_id
是一个外键列Probe
table.
不幸的是,我的数据库在 Reading
table 中有一些重复的值,我想删除,但我很难通过查询找到它们。
我认为这样的事情会找到重复项:
select * from Reading where Probe_id = Probe_id and Timestamp = Timestamp;
有人有什么想法吗?
按使记录重复的列分组,然后计算每组有多少
select Probe_id, Timestamp, count(*) as num_of_duplicates
from Reading
group by Probe_id, Timestamp
having count(*) > 1
WITH Duplicates
AS (SELECT Probe_id,
Timestamp,
ROW_NUMBER() OVER (PARTITION BY Probe_id, Timestamp ORDER BY Probe_id, Timestamp) AS [RowNumber]
FROM Reading
)
DELETE FROM Duplicates
WHERE Duplicates.RowNumber > 1
我的 SQL Server 2008 数据库有一个名为 Reading
的 table,其列 Timestamp
是一个 DateTime 值,Probe_id
是一个外键列Probe
table.
不幸的是,我的数据库在 Reading
table 中有一些重复的值,我想删除,但我很难通过查询找到它们。
我认为这样的事情会找到重复项:
select * from Reading where Probe_id = Probe_id and Timestamp = Timestamp;
有人有什么想法吗?
按使记录重复的列分组,然后计算每组有多少
select Probe_id, Timestamp, count(*) as num_of_duplicates
from Reading
group by Probe_id, Timestamp
having count(*) > 1
WITH Duplicates
AS (SELECT Probe_id,
Timestamp,
ROW_NUMBER() OVER (PARTITION BY Probe_id, Timestamp ORDER BY Probe_id, Timestamp) AS [RowNumber]
FROM Reading
)
DELETE FROM Duplicates
WHERE Duplicates.RowNumber > 1