简体   繁体   English

SQL查询以获取列中的最大单词数

[英]Sql query to fetch the max word count in a column

I have a table with 3 columns. 我有一张三列的桌子。 How do I query the column to get the words that appear the most in the column alone? 如何查询该列以获取仅在该列中出现最多的单词?

ID  Name   Age

#A  Gersh   9
#A  Marn    9  
#A  Drake   9 
#A  Hayford 2

My query should return the first 3 rows since 9 appears the most. 我的查询应该返回前3行,因为9出现的最多。

SQL SQL

Select * from table group by age

use max() 使用max()

select * from t where age= ( select max(age) from t)

or use rank() which sopported by most dbms 或使用大多数dbms支持的rank()

 select ID Name  Age from 
(
 select *,rank()over(order by age desc) rn from t
) a where rn=1

or you can use below if you need the most similar age occurred rows need 或者如果您需要最相似的年龄发生行,则可以在下面使用

with cte as
(
select 'A' as n ,9 as age
union all
select 'B',9
union all
select 'C',9
union all
select 'D',2
)
 select n as name ,age from (
 select *,
 count(*) over(partition by age order by age) as cnt
 from cte
 ) t where cnt= ( select max(cnt) from
   (
   select *,
  count(*) over(partition by age order by age) as cnt
  from cte
   )a

      )

demo link 演示链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM