SQLite How to find the most common occurrences of a value

Say I have table A with attributes X How do I find the X with the most occurrences? (There may be several that have the same highest value)

i.e. table A

 X
--
'a'
'b'
'c'
'c'
'b'

I would like to return

X
--
'b'
'c'

I cannot use the ALL keyword in Sqlite so that I am at a loss.

I thought about getting the counts of each X, and then sorting it, and then somehow using ORDER BY DESC so that the largest one is at the top and then LIMIT with a comparison to check if the values ​​below the first tuple are equal (which means they are also common), but I'm not sure about the syntax of LIMIT, and if I can have this state

, , , - , , ?

+4
4

,

SELECT X FROM yourTable
GROUP BY X
HAVING COUNT(*) = (
                   SELECT MAX(Cnt) 
                   FROM(
                         SELECT COUNT(*) as Cnt
                         FROM yourTable
                         GROUP BY X
                        ) tmp
                    )

SQL FIDDLE

+4

SELECT x,COUNT(x) AS cnt FROM a
GROUP BY x
ORDER BY cnt DESC;

cnt.

+2

LIMIT, , .

, , , , .

0
select X, count(X) from table group by X;
0

Source: https://habr.com/ru/post/1523868/


All Articles