Return 行,其中特定列具有重复值
Return rows where specific column has duplicate values
在下面的 table 中,我想显示第 3 列中的值重复的两行:
ID
Col2
Col3
1
a
123
2
b
123
3
c
14
4
d
65
5
e
65
这意味着我需要的查询应该 return ID 为 1、2 和 4、5 的行。
我使用 having
:
编写查询
SELECT *
FROM t1
INNER JOIN (SELECT col3 FROM t1
GROUP BY col3
HAVING COUNT(*) > 1) a
ON t1.col3 = a.col3
此查询虽然只有 returns 1 和 4 行,但并非所有重复项。
我将不胜感激。
您的查询应该有效,但我建议 window 函数:
select t1.*
from (select t1.*, count(*) over (partition by col3) as cnt
from t1
) t1
where cnt > 1;
在下面的 table 中,我想显示第 3 列中的值重复的两行:
ID | Col2 | Col3 |
---|---|---|
1 | a | 123 |
2 | b | 123 |
3 | c | 14 |
4 | d | 65 |
5 | e | 65 |
这意味着我需要的查询应该 return ID 为 1、2 和 4、5 的行。
我使用 having
:
SELECT *
FROM t1
INNER JOIN (SELECT col3 FROM t1
GROUP BY col3
HAVING COUNT(*) > 1) a
ON t1.col3 = a.col3
此查询虽然只有 returns 1 和 4 行,但并非所有重复项。 我将不胜感激。
您的查询应该有效,但我建议 window 函数:
select t1.*
from (select t1.*, count(*) over (partition by col3) as cnt
from t1
) t1
where cnt > 1;