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 8939667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:52:08+00:00 2026-06-15T10:52:08+00:00

I need to group some rows in a database, in groups of n. Example:

  • 0

I need to group some rows in a database, in groups of n.

Example:

select max(data) from tableA group by (groups of 2) order by id;

result: 20, 30, 5

so…

group 1 holds id 1 & 2, group 2 holds id 3 & 4, and group 3 would hold id 5 & 6.

tableA
| id  |  data |     
---------------
| 1   | 10    |
| 2   | 20    |
| 3   | 15    |
| 4   | 30    |
| 5   | 5     |
| 6   | 0     |
---------------

Is there a way to achieve this using SQL?

Note: The system is running on 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-06-15T10:52:10+00:00Added an answer on June 15, 2026 at 10:52 am

    Since the GROUP BY can hold an arbitrary expression, you can use CASE to return the id if its value MOD 2 is equal to 1, and id - 1 otherwise. This groups them in pairs incrementing from 1.

    SELECT MAX(data) AS maxdata
    FROM tableA
    /* GROUP BY the id or (id - 1) depending on the result of its modulo 2 */
    GROUP BY CASE WHEN id MOD 2 = 1 THEN id ELSE (id - 1) END
    

    Here is a demo on SQLfiddle.com

    Allow for non-sequential ids

    Update

    Overnight I felt badly about the fact that this solution only works correctly for sequential values of id. If you deleted rows, you would no longer get correct results. So here is a version that accounts for non-sequential, gapped ids:

    SET @rowid = 0;
    SELECT 
      MAX(data) AS maxdata
    FROM (
      SELECT  
        @rowid := @rowid + 1 AS rd,
        data
      FROM tableA
    ) subq
    GROUP BY CASE WHEN rd MOD 2 = 1 THEN rd ELSE (rd - 1) END
    

    And the updated SQLfiddle…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to select some rows from second table and concatenate them in comma-separated
I have s MySQL database and I need to insert some specific data in
I need some help getting data from several tables. This is the tables I
I need to group the data in my indexed array of indexed arrays by
I need to select statements where is fixed post id, group by user id
I have a skewed data set and I need to do a group by
I have a view with some joins in it. I'm doing a select from
I've got some MySQL tables with redundant data that I need to remove. For
I have a bit of a problem. I need to select a status, from
I need to update some rows in a DB table. How I identify the

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.