using count and odd "group by" errors...
Posted in 2004
I've been reading the Informix SQL manual for a while and can't seem
to find an explanation which clears up an issue I have regarding the
use of "count."
I'm looking for a way to select rows which have duplicate values of a
non-key field. If I have a "people" table keyed on person_id, I would
like to display all people who share a "nickname" with other people.
Hopefully grouped and ordered by nickname.
I tried:
SELECT COUNT(nickname), nickname
FROM people
GROUP BY nickname
HAVING COUNT(nickname) > 1
ORDER BY 1 DESC
which gives me a nice list of the most frequently used nicknames in
descending order (and the count, of course).
But when I change the select statement to:
SELECT COUNT(nickname), nickname, person_id
I get an error stating that person_id needs to be added to the group
by list. Then when I do add it, I get no results!
I realize that I'm not grasping something important here. Can someone
tell me what it is?