Answers:
SELECT `column`,
COUNT(`column`) AS `value_occurrence`
FROM `my_table`
GROUP BY `column`
ORDER BY `value_occurrence` DESC
LIMIT 1;
替换column
和my_table
。1
如果要查看列的N
最常用值,请增加。
SELECT * FROM my_table GROUP BY value ORDER BY count(*) DESC;
WHERE 'value_occurrence' = 1
?
HAVING
,而不是WHERE
在这种情况下。
尝试类似的方法:
SELECT `column`
FROM `your_table`
GROUP BY `column`
ORDER BY COUNT(*) DESC
LIMIT 1;
COUNT(*)
直接在中使用ORDER BY
。我知道关于GROUP BY
/ HAVING
和聚合列有一些限制,我一直认为那是行不通的。
让我们将表名视为tblperson
并将列名视为city
。我想从“城市”列中检索重复次数最多的城市:
select city,count(*) as nor from tblperson
group by city
having count(*) =(select max(nor) from
(select city,count(*) as nor from tblperson group by city) tblperson)
这nor
是别名。
下面的查询似乎对我在SQL Server数据库中很好:
select column, COUNT(column) AS MOST_FREQUENT
from TABLE_NAME
GROUP BY column
ORDER BY COUNT(column) DESC
结果:
column MOST_FREQUENT
item1 highest count
item2 second highest
item3 third higest
..
..
与SQL Server一起使用。
由于没有限制命令支持。
在这种情况下,您可以使用top 1命令在特定列中找到最大出现值(值)
SELECT top1
`value`,
COUNT(`value`) AS `value_occurrence`
FROM
`my_table`
GROUP BY
`value`
ORDER BY
`value_occurrence` DESC;
假设表为“ SalesLT.Customer
”,而您要找出的列为“ CompanyName
”,并且AggCompanyName
是别名。
Select CompanyName, Count(CompanyName) as AggCompanyName from SalesLT.Customer
group by CompanyName
Order By Count(CompanyName) Desc;
如果您不能使用LIMIT或LIMIT则不是查询工具的选项。您可以改用“ ROWNUM”,但需要一个子查询:
SELECT FIELD_1, ALIAS1
FROM(SELECT FIELD_1, COUNT(FIELD_1) ALIAS1
FROM TABLENAME
GROUP BY FIELD_1
ORDER BY COUNT(FIELD_1) DESC)
WHERE ROWNUM = 1
ROWNUM
LIMIT 1
改用;语法显示在接受的答案中。
如果您有一个ID列,并且想从每个ID的另一列中找到最重复的类别,则可以使用以下查询,
表:
查询:
SELECT ID, CATEGORY, COUNT(*) AS FREQ
FROM TABLE
GROUP BY 1,2
QUALIFY ROW_NUMBER() OVER(PARTITION BY ID ORDER BY FREQ DESC) = 1;
结果:
我喜欢使用的一种方法是:
选择 ,计数()为来自Table_Name的VAR1
通过...分组
按VAR1 desc订购
限制1