How can I correctly choose maximum number of occurrences of a string while grouping by another field?
8
votes
1
answer
7951
views
I am using Postgresql 9.0. I have the following fields in a table:
id, name
.
id name
1 John
1 Mary
1 Mary
1 Mary
1 John
1 Mary
3 Paul
3 Paul
3 George
. .
. .
For each id
, I want to select the name that occurs the most. How can I do that?
I tried with the following query but it doesn't work:
select id, max(name)
from table
group by id;
Asked by Tudor
(191 rep)
Dec 15, 2012, 07:59 AM
Last activity: Dec 16, 2012, 08:48 PM
Last activity: Dec 16, 2012, 08:48 PM