mssql 中如何查询某一列数据中出现次数最多的的一条 比如我有一个表 dbo.school 里面有列id name sex score
现在想查询score中出现次数做多的一条数据
求高手指点
坐等
mssql查询
分享到:
------解决方案-------------------- 那你这怎么会是获取一条呢,应该是多条才对吧?
select * from tb where score in(
select top 1 score from tb group by score order by count(1) desc
) ------解决方案--------------------
适合2005及以后的版本:
declare @school table(id int, name varchar(20), sex varchar(10), score int)
insert into @school
select 1,'张三','男',85 union all
select 2,'李四','男',95 union all
select 3,'王五','男',75 union all
select 4,'李六','男',70 union all
select 5,'王二','男',85
select top 1 score
from @school
order by (COUNT(*) over(PARTITION by score)) desc
/*
score
85
*/
------解决方案-------------------- select top 1 score,count(1) from dbo.school group by score order by count(1) desc