2

我正在对我的数据库进行一些手动重复检查,并且有一个复杂的案例。

我需要根据我已经完成的 A 列中的值检查重复行。但是,在这种特定情况下,可能有多个记录在 A 列中具有相同的值,但在 E 列中具有不同的值。

这是我的原始查询:

SELECT ColumnA, COUNT(*) TotalCount
FROM TableA
INNER JOIN TableA_1 on fID = hID
WHERE dateCreated > '2013-05-08 00:00:00'
GROUP BY ColumnA
HAVING COUNT(*) > 1
ORDER BY COUNT(*) DESC

我现在需要过滤掉 ColumnE 不同或唯一的 ColumnA 的重复项。我在原始查询中添加了伪代码

SELECT ColumnA, COUNT(*) TotalCount
FROM TableA
INNER JOIN TableA_1 on fID = hID
WHERE dateCreated > '2013-05-08 00:00:00'
AND ColumnE is not unique
GROUP BY ColumnA
HAVING COUNT(*) > 1
ORDER BY COUNT(*) DESC

我希望这是有道理的。

4

2 回答 2

1

您可以将ColumnE添加到分组中,如下所示:

    SELECT ColumnA, ColumnE, COUNT(*) TotalCount
    FROM TableA
    INNER JOIN TableA_1 on fID = hID
    WHERE dateCreated > '2013-05-08 00:00:00'
    GROUP BY ColumnA, ColumnE
    HAVING COUNT(*) > 1
    ORDER BY COUNT(*) DESC
于 2013-05-09T19:25:18.937 回答
1

您需要 ColumnA 列上的 GROUP BY 子句和 DISTINCT ColumnE 上的 HAVING 子句

SELECT ColumnA, COUNT(*) TotalCount
FROM TableA INNER JOIN TableA_1 on fID = hID
WHERE dateCreated > '2013-05-08 00:00:00'
GROUP BY ColumnA
HAVING COUNT(DISTINCT ColumnE) > 1
ORDER BY COUNT(*) DESC
于 2013-05-09T19:51:48.123 回答