查询没有重复的SQL

时间:2015-09-22 03:32:32

标签: sql sql-server

我有重复查询。现在我需要构建没有重复的查询。我试图这样做,但我的查询需要很长时间。我的查询有重复项:

SELECT
    c.*
FROM
    Clients c 
    INNER JOIN
    (
        SELECT
            iin,
            COUNT(iin) AS countIIN
        FROM
            Clients
        GROUP BY
            iin
        HAVING
            COUNT(iin) > 1
    ) cc
    ON c.IIN = cc.IIN
ORDER BY
    c.last_name DESC

我需要上面的反查询。

1 个答案:

答案 0 :(得分:0)

要查找duplicates in SQL Row_Number()函数是最佳选项,

请检查以下查询

WITH [CTE NoDuplicates] AS
(
SELECT
  RN = ROW_NUMBER() OVER (PARTITION BY iin ORDER BY c.last_name DESC),
  *
FROM Clients
)
DELETE FROM [CTE DUPLICATE] WHERE RN = 1