用临时表
select 姓名,count(*) 重名个数 into #b from student group by 姓名
select max(重名个数) as 最大重名数 from #b
其实可更简单,一句
select top 1 姓名,count(*) 重名个数 from student group by 姓名 order by count(*) desc
select 姓名,count(*) 重名个数 into #b from student group by 姓名
select max(重名个数) as 最大重名数 from #b
其实可更简单,一句
select top 1 姓名,count(*) 重名个数 from student group by 姓名 order by count(*) desc
select 姓名,max(count(姓名)) as 重名个数 from student group by 姓名
非常谢谢!我还想问一下:#b这个临时表在SQL语句执行完成以后怎么清除掉?我不想它在sql server中占用内存to Softlee81307(孔腎):
你这个SQL语句有问题呀,错误提示:
服务器: 消息 130,级别 15,状态 1,行 2
不能对包含聚合或子查询的表达式执行聚合函数。
select max(最大重名数) from
(select count(姓名) as 最大重名数 from student group by 姓名) a