我想在下表中找到所有重复项。 我知道我可以使用CTE包装查询,然后语法正确。 我对可以直接访问cnt列的解决方案感兴趣。 我也想在(partition by)子句上使用count()。
CREATE TABLE #People
(
ID INT,
[NAME] varchar(50),
AGE INT
)
INSERT INTO #PEOPLE
SELECT 1,'Mark',23
UNION
SELECT 2,'Tomm',23
UNION
SELECT 3,'Ann',26
select * from #People
select [NAME] , AGE, count(1) over (partition by [NAME], AGE) as cnt
from #People
having count(cnt) > 2
答案 0 :(得分:4)
为什么不只使用group by
?
select name, age, count(*) as cnt
from #People
group by name, age
having count(*) > 2
如果您想要重复,那么比较应该是:
having count(*) >= 2