[英]Find the row with Maximum Count
I need to find the row with maximum count. 我需要找到最大数量的行。 There's only one table, so it should be easy, but it isn't.
只有一张桌子,所以应该很容易,但事实并非如此。 Below is the content of table:
下表内容:
+------+------+------+
| row1 | row2 | row3 |
+------+------+------+
| 3 | 2 | 1 |
| 6 | 4 | 5 |
| 6 | 2 | 1 |
+------+------+------+
I need to find maximum count item for row1: Using: SELECT COUNT(*) AS c, row1 AS name FROM draw GROUP BY name; 我需要找到最大计数为项目ROW1:使用:SELECT COUNT(*)为C,ROW1 AS名借鉴GROUP BY的名字; Give me the result:
给我结果:
+------+------+
| c | name |
+------+------+
| 1 | 3 |
| 2 | 6 |
+------+------+
But I want to display only one row with the maximum "c": 但是,我想最大的“C”,只显示一行:
+------+------+
| c | name |
+------+------+
| 2 | 6 |
+------+------+
Using: SELECT MAX(c), name ( SELECT COUNT(*) AS c, row1 AS name FROM draw GROUP BY name ) AS counts; 使用:SELECT MAX(C),姓名(SELECT COUNT(*)为C,ROW1 AS名借鉴GROUP BY名)AS计数;
Give me result: 给我结果:
+------+------+
| c | name |
+------+------+
| 2 | 3 |
+------+------+
It means that it gives maximum count (c), but give first number in name row. 这意味着它给出最大计数(c),但在名称行中给出第一个数字。
Is there way to fix it? 有办法解决吗?
I think this is what you want, but your requirements are a little unclear. 我认为这是您想要的,但是您的要求尚不清楚。 If this was any other platform you could use windowing functions would would be better.
如果这是任何其他平台,则可以使用窗口功能会更好。
SELECT *
FROM (
SELECT COUNT(*) AS c, row1
FROM draw
GROUP BY row1
) as Sub1
WHERE Sub.c = (
SELECT Max(c)
FROM (
SELECT COUNT(*) AS c
FROM draw
GROUP BY row1
) as Sub2
) as Sub3
select top 1 row1, count(*)
from draw
group by row1
order by count(*) desc
Order by the count descending (ie highest count first) and take the first one only. 按照降序排列(即最高计数优先)并仅取第一个。
You can try this 你可以试试这个
SELECT COUNT( `row1` ) AS c, `row1`
FROM `draw`
GROUP BY `row1`
ORDER BY `row1` DESC
LIMIT 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.