我有一个查询,它获取由三个字段分组的内部选择的最大计数。现在我不想只有这个数字,而是整行。我无法弄明白。以下是返回数字的查询:
SELECT MAX (count) FROM
(SELECT COUNT (*) AS count,
field1,
field2,
field3
FROM someScheme.someTable
GROUP BY field1, field2, field3
) AS counts
答案 0 :(得分:3)
使用order by
和limit
:
SELECT COUNT(*) AS count, field1, field2, field3
FROM someScheme.someTable
GROUP BY field1, field2, field3
ORDER BY COUNT(*) DESC
LIMIT 1;
注意:这只返回一个最大值的例子。
如果你想要所有可能的行,那就更复杂了:
SELECT COUNT(*) AS count, field1, field2, field3
FROM someScheme.someTable
GROUP BY field1, field2, field3
HAVING COUNT(*) = (SELECT MAX(cnt)
FROM (SELECT COUNT(*) AS cnt
FROM someScheme.someTable
GROUP BY field1, field2, field3
) t
);