找到最大数量

时间:2012-10-30 04:19:45

标签: sql

假设我有一个包含以下值的表

A         B           C
------------------------
5        ABC         $20
3        BCD         $40
5        BCD         $40
2        ABC         $20
5        ABC         $30

如何编写将返回最大计数的查询,假设为A列 它应返回值为3的最大值为5。

3 个答案:

答案 0 :(得分:4)

你可以像这样使用子查询:

SELECT COUNT(*) AS COUNTS FROM Table1
WHERE A = (SELECT MAX(A) AS AD FROM Table1)

See this SQLFiddle

答案 1 :(得分:2)

一种方式(不确定它是最快的):

SELECT A, count(*) FROM my_table GROUP BY 1 ORDER BY 2 LIMIT 1;

答案 2 :(得分:2)

虽然不是很典型,但

也是如此

(自我反对不平等)

SELECT 
    COUNT(t1.a) AS COUNTS 
FROM Table1 t1
   LEFT JOIN Table1 t2
    ON t1.A < t2.A
WHERE 
      t2.a is null

Demo