Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 352525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:44:43+00:00 2026-05-12T11:44:43+00:00

There are a lot of SQL Top N questions on stackoverflow but I can’t

  • 0

There are a lot of SQL Top N questions on stackoverflow but I can’t seem to find one that matches the situation I’m having. I would like to perform some grouping within a top n query. My data looks like this (obviously with fake values).

MY_DATE    IP_ADDRESS
1/1/09     999.999.999.999
1/1/09     999.999.999.999
1/1/09     999.999.999.998
... a lot more rows

The date range for the table covers several months and has many thousands of rows per month. What I would like to do is have a single query tell me which 10 IP Addresses occurred the most frequently for each month. I can do this for a single month using the following:

SELECT DATE_FORMAT(MY_DATE, '%b-%y') AS "MONTH", IP_ADDRESS, COUNT(*) AS HITS
FROM MY_DATA
WHERE DATE_FORMAT(MY_DATE, '%b-%y') = 'JAN-09'
GROUP BY DATE_FORMAT(MY_DATE, '%b-%y'), IP_ADDRESS
ORDER BY HITS DESC
LIMIT 10

But what I really want is to be able to see the top n for every month in the data set. That essentially prohibits me from using the where clause I specified. Of course, when I do that, then I just get the to 10 for all months. The result I’m looking for should look like this:

MONTH    IP_ADDRESS        COUNT(*)
JAN-09   999.999.999.999   200
JAN-09   999.999.999.998   150
... ( 8 more rows of January )
FEB-09   999.999.999.999   320
FEB-09   999.999.999.998   234
... ( 8 more rows of February)
MAR-09   999.999.999.999   440
... ETC.

Can this be done in MySQL? It seems the barrier I’m hitting is that MySQL doesn’t allow an ORDER BY within a query statement included in a UNION. Thanks for the help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T11:44:43+00:00Added an answer on May 12, 2026 at 11:44 am

    I just tried a query very similar to the one given by @Charles Bretana and it does work. I used a VIEW to help clarify things.

    CREATE TABLE my_data (
     my_date DATE,
     ip_address CHAR(15)
    );
    

    Insert a bunch of date/IPaddress pairs (not shown)…

    Create a view for all counts per month and IP address:

    CREATE VIEW my_data_per_month as
     SELECT EXTRACT(YEAR_MONTH FROM my_date) AS month,
       ip_address, COUNT(*) AS hits
     FROM my_data
     GROUP BY month, ip_address;
    
    SELECT * FROM my_data_per_month
    ORDER BY month ASC, hits DESC;
    
    +--------+-----------------+------+
    | month  | ip_address      | hits |
    +--------+-----------------+------+
    | 200901 | 999.999.999.999 |    8 | 
    | 200901 | 999.999.999.998 |    6 | 
    | 200901 | 999.999.999.997 |    5 | 
    | 200901 | 999.999.999.996 |    4 | 
    | 200901 | 999.999.999.995 |    3 | 
    | 200901 | 999.999.999.994 |    2 | 
    | 200902 | 999.999.999.998 |    8 | 
    | 200902 | 999.999.999.997 |    6 | 
    | 200902 | 999.999.999.996 |    5 | 
    | 200902 | 999.999.999.995 |    4 | 
    | 200902 | 999.999.999.994 |    3 | 
    | 200902 | 999.999.999.993 |    2 | 
    | 200903 | 999.999.999.997 |    8 | 
    | 200903 | 999.999.999.996 |    6 | 
    | 200903 | 999.999.999.995 |    5 | 
    | 200903 | 999.999.999.994 |    4 | 
    | 200903 | 999.999.999.993 |    3 | 
    | 200903 | 999.999.999.992 |    2 | 
    +--------+-----------------+------+
    

    Now show the top three IP addresses per month:

    SELECT m1.month, m1.ip_address, m1.hits
    FROM my_data_per_month m1
    LEFT OUTER JOIN my_data_per_month m2
      ON (m1.month = m2.month AND m1.hits < m2.hits)
    GROUP BY m1.month, m1.ip_address
    HAVING COUNT(*) < 3
    ORDER BY m1.month ASC, m1.hits DESC;
    
    +--------+-----------------+------+
    | month  | ip_address      | hits |
    +--------+-----------------+------+
    | 200901 | 999.999.999.999 |    8 | 
    | 200901 | 999.999.999.998 |    6 | 
    | 200901 | 999.999.999.997 |    5 | 
    | 200902 | 999.999.999.998 |    8 | 
    | 200902 | 999.999.999.997 |    6 | 
    | 200902 | 999.999.999.996 |    5 | 
    | 200903 | 999.999.999.997 |    8 | 
    | 200903 | 999.999.999.996 |    6 | 
    | 200903 | 999.999.999.995 |    5 | 
    +--------+-----------------+------+
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking to learn LINQ, but I'm finding that there is a lot more
I seem to be asking a lot of SQL questions at the moment. (I
There are a lot of Sql table -> C# class methodologies, but I'm looking
There are a lot of questions about how to delete item from list, but
I realize there are a lot of questions already about this. But my method
Can someone post any simple explanation of cache aware algorithms? There are lot of
There are a lot of new features that came with the .Net Framework 3.5.
There are a lot of webs still using classic ASP instead of ASP.NET but
There's a lot of advice out there that you shouldn't expose your fields publically,
I am working on a class that deals with a lot of Sql objects

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.