显示多个条目及其计数的SQL查询

显示多个条目及其计数的SQL查询,sql,sql-server,Sql,Sql Server,我有一张这样的桌子(看看Col1和Col2) 我需要在(Col1,Col2)中找到重复项 我需要的是这样的输出: 1a 2b --> occurred 2 times 1c 2d --> occurred 3 times 是否有SQL查询可以生成该结果? 如有可能,包括按Col2排序(或分组) (我不确定数据库版本是什么,但它是Microsoft SQL Server)您可以试试 select count(*) as c, col1, col2 from foobar group

我有一张这样的桌子(看看Col1和Col2)

我需要在(Col1,Col2)中找到重复项

我需要的是这样的输出:

1a 2b --> occurred 2 times
1c 2d --> occurred 3 times
是否有SQL查询可以生成该结果?
如有可能,包括按Col2排序(或分组)

(我不确定数据库版本是什么,但它是Microsoft SQL Server)

您可以试试

select count(*) as c, col1, col2 from foobar group by col1, col2 having c > 1
select count(*) as c, col1, col2 from foobar group by col1, col2 having c > 1
select col1, col2, count(*)
from yourTable
group by col1, col2
having count(*) > 1