tags:

views:

65

answers:

2

Hey,

So I have a table (person), that contains columns such as persons name, age, eye-color, favorite movie.

How do I find the most popular eye color(s), returning just the eye color (not the count) using SQL (Microsft Access), without using top as there might be multiple colours with the same count.

Thank you

+1  A: 
SELECT
  EyeColor
FROM
  Person
GROUP BY  
  EyeColor
HAVING
  COUNT(*) = (
    SELECT MAX(i.EyeColorCount) FROM (
      SELECT COUNT(*) AS EyeColorCount FROM Person GROUP BY EyeColor 
    ) AS i
  )
Tomalak
I have tried this in Access both plain and with SQL Server compatible syntax set, and it returns "Cannot have aggregate function in expression MAX(COUNT(*)"
Remou
@Remou: I have no Access to test this with, currently. Could you try the changed query again? Thanks!
Tomalak
"At most one record can be returned by this subquery" :( :)
Remou
@Remou: Sigh. Does Access not respect the `TOP 1`? Changed the SQL once more.
Tomalak
It does. TOP 1 = 2 records, both having equal count : ) Now, just being awkward, "Syntax error (missing operator) in query expression SELECT COUNT(*) EyeColorCount" ;) BTW, after adding the missing AS, the above query returns two records.
Remou
@Remou: Aaahhrg. Curse you, MS Access. :-) (Also, in my book, TOP 1 is never more than one record.)
Tomalak
How would you want the database engine to guess which of the two tied records to return?
David-W-Fenton
A: 

In Access, I think you need something on the lines of:

SELECT First(t.Eyecolor) AS FirstOfEyeColor
FROM (SELECT p.EyeColor, Count(p.EyeColor) AS C
FROM Person p
GROUP BY p.EyeColor
ORDER BY Count(p.EyeColor) DESC)  AS t;
Remou
But that does not select two colors if they have the same count, does it?
Tomalak
@Tomalak No, first only takes the first, unlike top. I tested with duplicate counts.
Remou
Several years ago, I read a comment that suggested First was not the best idea, and I have been a little wary ever since, but it was only one comment, albeit from a generally trustworthy source, and I have never been able to find it again. And First has not bitten me so far :)
Remou
Hm. If there are 4 people with blue and 4 with brown eyes, would the query return two rows?
Tomalak
No, it would not. As I said, I tested with duplicate counts, I did not go as high as four people with the same eye colour, just two blue and two green, but I will try four brown and four blue, if you think it would help :D
Remou
No, I think this will not be necessary. :) However the OP stated the wants N records returned when there are N instances having max count.
Tomalak
@Tomalek Perhaps Select Distinct From (Select Top n From ... ) ? It would be a bit hit and miss, I must paly around.
Remou
The DISTINCT will only work if all the fields in the SELECT are in the ORDER BY (which determines which records get returned by TOP N), or if all the fields other than those in the ORDER BY are the same in all the tied records. If you require no ties, then I think this needs a subquery.
David-W-Fenton