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

  • Home
  • SEARCH
  • 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 6084073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:28:45+00:00 2026-05-23T11:28:45+00:00

I have some data formatted like this: Lane Series 1 680 1 685 1

  • 0

I have some data formatted like this:

Lane         Series
1            680
1            685
1            688
2            666
2            425
2            775
...

And I’d like to grab the highest n series per lane (let’s say 2 for the sake of this example, but it could be many more than that)

So the output should be:

Lane         Series
1            688
1            685
2            775
2            666

Getting the highest series per lane is easy, but I can’t seem to find a way to get the highest 2 results.

I use a MAX aggregate function with a GROUP BY to get the MAX, but there’s no “TOP N” function as in SQL Server and using ORDER BY… LIMIT only returns the highest N results overall, not per lane.

Since I use a JAVA application I coded myself to query the database and choose what N is, I could do a loop and use a LIMIT and loop through every lane, making a different query each time, but I want to learn how to do it using MySQL.

  • 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-23T11:28:45+00:00Added an answer on May 23, 2026 at 11:28 am

    This solution is the fastest for MySQL and will work with very large tables, but it uses “funky” MySQL features, so wouldn’t be of use for other database flavours.

    (Edited to sort before applying logic)

    set @count:=-1, @lane:=0; 
    select lane, series
    from (select lane, series from lane_series order by lane, series desc) x
    where if(lane != @lane, @count:=-1, 0) is not null
    and if(lane != @lane, @lane:=lane, lane) is not null
    and (@count:=@count+1) < 2; -- Specify the number of row at top of each group here
    

    To put this query on steroids, define an index on lane and series: CREATE INDEX lane_series_idx on lane_series(lane, series); and it will do (super fast) index-only scan – so your other text columns don’t affect it.

    Good points of this query are:

    1. It requires only one table pass (albeit sorted)
    2. It handles ties at any level, for example if there’s a tie for 2nd, only one of the 2nd will be displayed – ie the row count is absolute and never exceeded

    Here’s the test output:

    create table lane_series (lane int, series int);
    
    insert into lane_series values (1, 680),(1, 685),(1, 688),(2, 666),(2, 425),(2, 775);
    
    -- Execute above query:
    
    +------+--------+
    | lane | series |
    +------+--------+
    |    1 |    688 |
    |    1 |    685 |
    |    2 |    775 |
    |    2 |    666 |
    +------+--------+
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some data like this : (AT sql sERVER) MemberID,ParemtID,Amt,OtherInfo 1, NULL, 200,dfdsf
I've got some data sent via a server, formatted like this: [ {Username:user1@domain.com, id:1},
Say I have a file which contains one line of data formatted like this:
I have some data formated like the following 2009.07.02 02:20:14 40.3727 28.2330 6.4 2.6
I have been trying all day to get some data properly formatted in a
I have been given some poorly formatted data and need to pull numbers out
I have some data from log files and would like to group entries by
I have some data and need to create a json file with this structure
I have some records like this: Datetime | Type | 2012-02-23 22:00:11 1 2012-02-23
I have weather forecast data formatted like so: loc_id#location#state#forecast_date#issue_date#issue_time#min_0#max_0#min_1#max_1#min_2#max_2#min_3#max_3#min_4#max_4#min_5#max_5#min_6#max_6#min_7#max_7#forecast_0#forecast_1#forecast_2#forecast_3#forecast_4#forecast_5#forecast_6#forecast_7# 090180#Airey's Inlet#VIC#20091204#20091204#161830###12#19#12#21#12#19#12#17#11#20#12#20#11#17#Mostly clear.#Shower or two.#Light

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.