日期:2014-05-17  浏览次数:20628 次

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
)
------解决方案--------------------
引用:
Quote: 引用:

那你这怎么会是获取一条呢,应该是多条才对吧?
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