I have a nice trivial quiz for the weekend ... quite sorry it is even hard to type down the title.
Context
Let me do an example (uuid
and value
are strings):
Table1
---------------
uuid | value
---------------
1 | x
1 | a
3 | z
2 | y
1 | x
1 | x
3 | b
This is the expected result
---------------
uuid | value
---------------
1 | a
3 | z
3 | b
Because the above are the rows that contain for a given uuid
the less common value (or equal) among the average values used.
I am playing with GROUP BY
, DISTINCT
, COUNT
...
Attempt ONE
SELECT uuid, COUNT(uuid) as time_used, value
FROM table1
GROUP BY uuid, value ORDER BY COUNT(*) DESC;
Attempt TWO
SELECT uuid, COUNT(uuid) as occ
FROM (
SELECT uuid, COUNT(uuid) as occ, value
FROM table1
GROUP BY uuid, value ORDER BY COUNT(*)) as t
GROUP BY uuid;
Attempt THREE
SELECT uuid as occ
FROM (
SELECT uuid, COUNT(uuid) as occ, value
FROM table1
GROUP BY uuid, value ORDER BY COUNT(*)) as t
GROUP BY uuid
HAVING COUNT(uuid) > 1;
But it is not the right one, I still miss a step...
http://sqlfiddle.com/#!9/593a74/3
Note I am using MySQL, few data (around 2000) but still want to be 'performant', they will grow and I would like to be compliant with the only_full_group_by
statement of MySQL.
Thanks.