Skip to main content
1 of 6
Erwin Brandstetter
  • 182.2k
  • 28
  • 457
  • 620

get the most frequently appearing user_code where the month is specified

Just add a WHERE clause:

SELECT user_code, COUNT(invite_code) AS counted
FROM   invite_table
WHERE  month = 'May'  -- or whatever is stored in your varchar(3) column
GROUP  BY user_code
ORDER  BY counted DESC, user_code  -- to break ties in deterministic fashion
LIMIT  10;

You would GROUP BY user_code of course, not by invite_code.

A month column as varchar(3) doesn't seem very useful if there can be data for more than a single year. I would use data type date for it. You can format that any way you like for presentation.

Erwin Brandstetter
  • 182.2k
  • 28
  • 457
  • 620