我在col1中有一些重复的表:
co1, col2, col3
a, d, f
r, h, d
e, g, h
z, k, m
e, s, t
a, b, c
e, k, t
感谢this post,我可以选择重复的所有col1值
SELECT col1
FROM mytable
GROUP BY col1
HAVING ( COUNT(col1) > 1 )
如何选择具有重复col1值的所有行并按col1排序?预期结果如下:
co1, col2, col3
a, d, f
a, b, c
e, g, h
e, s, t
e, k, t
答案 0 :(得分:3)
使用EXISTS
SELECT *
FROM mytable a
WHERE EXISTS (SELECT 1
FROM mytable b
WHERE a.col1 = b.col1
HAVING Count(col1) > 1)
Order by col1
另一种方式
SELECT *
FROM (SELECT *,
Count(1)OVER( partition BY col1) AS cnt
FROM mytable) a
WHERE cnt > 1
ORDER BY col1
答案 1 :(得分:2)
您可以使用窗口功能:
select co1, col2, col3
from (
select co1, col2, col3, count(*) over (partition by col1) as cnt
from the_table
) t
where cnt > 1
order by col1;
答案 2 :(得分:1)
您可以使用原始表Inner Join
结果select t1.*
from your_table t1
inner join (
select col1
from your_table
group by col1
having count(col1) > 1 ) t2
on t1.col1 = t2.col1
order by t1.col1;
:
{{1}}
答案 3 :(得分:1)
Select *
From ( Select * ,count(*) over (Partition By Col1) as cnt from t) t
Where cnt>1
答案 4 :(得分:1)
Select co1,col2,col3 From
( Select * ,count(*) over (Partition By Co1) as cnt from #b) a
Where cnt>1