mssql 中如何查询某一列数据中出现次数最多的的一条

我才是二亮 2013-11-09 10:53:13
比如我有一个表 dbo.school 里面有列id name sex score
现在想查询score中出现次数做多的一条数据

求高手指点

坐等
...全文
1109 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
LongRui888 2013-11-10
  • 打赏
  • 举报
回复
引用 13 楼 yupeigu 的回复:
[quote=引用 12 楼 u010436900 的回复:] [quote=引用 10 楼 u010436900 的回复:] [quote=引用 9 楼 yupeigu 的回复:] [quote=引用 8 楼 u010436900 的回复:] [quote=引用 6 楼 yupeigu 的回复:] [quote=引用 4 楼 u010436900 的回复:] [quote=引用 3 楼 szm341 的回复:] 那你这怎么会是获取一条呢,应该是多条才对吧? select * from tb where score in( select top 1 score from tb group by score order by count(1) desc )
因为我只要那个分数是多少 其他的可以不要 最好可以让他显示出现的次数[/quote] 适合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
*/
[/quote] 大牛 那可以把出现次数最多的 次多的 再次 这样的排名前十位的都列出来呢?? 如果可以给出出现次数 那就再好不过了 求大牛指教 [/quote] 哦,是这样吗:
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   


--score出现次数一样的,也会排下去
select score,
       rownum
from
(
	select score,
		   row_number() over(order by c desc) as rownum
	from
	(
		select distinct
			   score,
			   COUNT(*) over(PARTITION by score) c
		from @school
	)t
)tt
where rownum <=10
/*
score	rownum
85	1
70	2
75	3
95	4
*/


--score出现次数一样的,都排成第2名
select top 10 score,
       rownum
from
(
	select score,
		   rank() over(order by c desc) as rownum
	from
	(
		select distinct
			   score,
			   COUNT(*) over(PARTITION by score) c
		from @school
	)t
)tt
--where rownum <=10
/*
score	rownum
85	1
70	2
75	2
95	2
*/
[/quote] 谢谢大牛 我先试试[/quote] 大神 不知道为何 我这边还是没有达到我的目的 可以给你QQ吗 想请教您下[/quote] 我加你关注了,也加我,可以发私信[/quote] 我给你发了私信
LongRui888 2013-11-10
  • 打赏
  • 举报
回复
引用 12 楼 u010436900 的回复:
[quote=引用 10 楼 u010436900 的回复:] [quote=引用 9 楼 yupeigu 的回复:] [quote=引用 8 楼 u010436900 的回复:] [quote=引用 6 楼 yupeigu 的回复:] [quote=引用 4 楼 u010436900 的回复:] [quote=引用 3 楼 szm341 的回复:] 那你这怎么会是获取一条呢,应该是多条才对吧? select * from tb where score in( select top 1 score from tb group by score order by count(1) desc )
因为我只要那个分数是多少 其他的可以不要 最好可以让他显示出现的次数[/quote] 适合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
*/
[/quote] 大牛 那可以把出现次数最多的 次多的 再次 这样的排名前十位的都列出来呢?? 如果可以给出出现次数 那就再好不过了 求大牛指教 [/quote] 哦,是这样吗:
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   


--score出现次数一样的,也会排下去
select score,
       rownum
from
(
	select score,
		   row_number() over(order by c desc) as rownum
	from
	(
		select distinct
			   score,
			   COUNT(*) over(PARTITION by score) c
		from @school
	)t
)tt
where rownum <=10
/*
score	rownum
85	1
70	2
75	3
95	4
*/


--score出现次数一样的,都排成第2名
select top 10 score,
       rownum
from
(
	select score,
		   rank() over(order by c desc) as rownum
	from
	(
		select distinct
			   score,
			   COUNT(*) over(PARTITION by score) c
		from @school
	)t
)tt
--where rownum <=10
/*
score	rownum
85	1
70	2
75	2
95	2
*/
[/quote] 谢谢大牛 我先试试[/quote] 大神 不知道为何 我这边还是没有达到我的目的 可以给你QQ吗 想请教您下[/quote] 我加你关注了,也加我,可以发私信
LongRui888 2013-11-10
  • 打赏
  • 举报
回复
引用 8 楼 u010436900 的回复:
[quote=引用 6 楼 yupeigu 的回复:] [quote=引用 4 楼 u010436900 的回复:] [quote=引用 3 楼 szm341 的回复:] 那你这怎么会是获取一条呢,应该是多条才对吧? select * from tb where score in( select top 1 score from tb group by score order by count(1) desc )
因为我只要那个分数是多少 其他的可以不要 最好可以让他显示出现的次数[/quote] 适合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
*/
[/quote] 大牛 那可以把出现次数最多的 次多的 再次 这样的排名前十位的都列出来呢?? 如果可以给出出现次数 那就再好不过了 求大牛指教 [/quote] 哦,是这样吗:
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   


--score出现次数一样的,也会排下去
select score,
       rownum
from
(
	select score,
		   row_number() over(order by c desc) as rownum
	from
	(
		select distinct
			   score,
			   COUNT(*) over(PARTITION by score) c
		from @school
	)t
)tt
where rownum <=10
/*
score	rownum
85	1
70	2
75	3
95	4
*/


--score出现次数一样的,都排成第2名
select top 10 score,
       rownum
from
(
	select score,
		   rank() over(order by c desc) as rownum
	from
	(
		select distinct
			   score,
			   COUNT(*) over(PARTITION by score) c
		from @school
	)t
)tt
--where rownum <=10
/*
score	rownum
85	1
70	2
75	2
95	2
*/
我才是二亮 2013-11-10
  • 打赏
  • 举报
回复
引用 10 楼 u010436900 的回复:
[quote=引用 9 楼 yupeigu 的回复:] [quote=引用 8 楼 u010436900 的回复:] [quote=引用 6 楼 yupeigu 的回复:] [quote=引用 4 楼 u010436900 的回复:] [quote=引用 3 楼 szm341 的回复:] 那你这怎么会是获取一条呢,应该是多条才对吧? select * from tb where score in( select top 1 score from tb group by score order by count(1) desc )
因为我只要那个分数是多少 其他的可以不要 最好可以让他显示出现的次数[/quote] 适合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
*/
[/quote] 大牛 那可以把出现次数最多的 次多的 再次 这样的排名前十位的都列出来呢?? 如果可以给出出现次数 那就再好不过了 求大牛指教 [/quote] 哦,是这样吗:
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   


--score出现次数一样的,也会排下去
select score,
       rownum
from
(
	select score,
		   row_number() over(order by c desc) as rownum
	from
	(
		select distinct
			   score,
			   COUNT(*) over(PARTITION by score) c
		from @school
	)t
)tt
where rownum <=10
/*
score	rownum
85	1
70	2
75	3
95	4
*/


--score出现次数一样的,都排成第2名
select top 10 score,
       rownum
from
(
	select score,
		   rank() over(order by c desc) as rownum
	from
	(
		select distinct
			   score,
			   COUNT(*) over(PARTITION by score) c
		from @school
	)t
)tt
--where rownum <=10
/*
score	rownum
85	1
70	2
75	2
95	2
*/
[/quote] 谢谢大牛 我先试试[/quote] 大神 不知道为何 我这边还是没有达到我的目的 可以给你QQ吗 想请教您下
SakuraGaara 2013-11-10
  • 打赏
  • 举报
回复
select top 1 score,count(1) from dbo.school group by score order by count(1) desc
我才是二亮 2013-11-10
  • 打赏
  • 举报
回复
引用 9 楼 yupeigu 的回复:
[quote=引用 8 楼 u010436900 的回复:] [quote=引用 6 楼 yupeigu 的回复:] [quote=引用 4 楼 u010436900 的回复:] [quote=引用 3 楼 szm341 的回复:] 那你这怎么会是获取一条呢,应该是多条才对吧? select * from tb where score in( select top 1 score from tb group by score order by count(1) desc )
因为我只要那个分数是多少 其他的可以不要 最好可以让他显示出现的次数[/quote] 适合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
*/
[/quote] 大牛 那可以把出现次数最多的 次多的 再次 这样的排名前十位的都列出来呢?? 如果可以给出出现次数 那就再好不过了 求大牛指教 [/quote] 哦,是这样吗:
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   


--score出现次数一样的,也会排下去
select score,
       rownum
from
(
	select score,
		   row_number() over(order by c desc) as rownum
	from
	(
		select distinct
			   score,
			   COUNT(*) over(PARTITION by score) c
		from @school
	)t
)tt
where rownum <=10
/*
score	rownum
85	1
70	2
75	3
95	4
*/


--score出现次数一样的,都排成第2名
select top 10 score,
       rownum
from
(
	select score,
		   rank() over(order by c desc) as rownum
	from
	(
		select distinct
			   score,
			   COUNT(*) over(PARTITION by score) c
		from @school
	)t
)tt
--where rownum <=10
/*
score	rownum
85	1
70	2
75	2
95	2
*/
[/quote] 谢谢大牛 我先试试
我才是二亮 2013-11-09
  • 打赏
  • 举报
回复
引用 6 楼 yupeigu 的回复:
[quote=引用 4 楼 u010436900 的回复:] [quote=引用 3 楼 szm341 的回复:] 那你这怎么会是获取一条呢,应该是多条才对吧? select * from tb where score in( select top 1 score from tb group by score order by count(1) desc )
因为我只要那个分数是多少 其他的可以不要 最好可以让他显示出现的次数[/quote] 适合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
*/
[/quote] 大牛 那可以把出现次数最多的 次多的 再次 这样的排名前十位的都列出来呢?? 如果可以给出出现次数 那就再好不过了 求大牛指教
szm341 2013-11-09
  • 打赏
  • 举报
回复
那就把子查询部分提出来就是了 select top 1 score,count(1) as num from tb group by score order by num desc
我才是二亮 2013-11-09
  • 打赏
  • 举报
回复
引用 3 楼 szm341 的回复:
那你这怎么会是获取一条呢,应该是多条才对吧? select * from tb where score in( select top 1 score from tb group by score order by count(1) desc )
因为我只要那个分数是多少 其他的可以不要 最好可以让他显示出现的次数
szm341 2013-11-09
  • 打赏
  • 举报
回复
那你这怎么会是获取一条呢,应该是多条才对吧? select * from tb where score in( select top 1 score from tb group by score order by count(1) desc )
我才是二亮 2013-11-09
  • 打赏
  • 举报
回复
引用 1 楼 szm341 的回复:
是指同一个分数出现次数最多的数据?还是分数最高的数据?
是同一分数出现此时最多的数据
szm341 2013-11-09
  • 打赏
  • 举报
回复
是指同一个分数出现次数最多的数据?还是分数最高的数据?
LongRui888 2013-11-09
  • 打赏
  • 举报
回复
根据上面修改的,适合2000的:
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 
group by score 
order by COUNT(*) desc
/*
score
85
*/
LongRui888 2013-11-09
  • 打赏
  • 举报
回复
引用 4 楼 u010436900 的回复:
[quote=引用 3 楼 szm341 的回复:] 那你这怎么会是获取一条呢,应该是多条才对吧? select * from tb where score in( select top 1 score from tb group by score order by count(1) desc )
因为我只要那个分数是多少 其他的可以不要 最好可以让他显示出现的次数[/quote] 适合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
*/

22,296

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧