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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:29:02+00:00 2026-06-15T03:29:02+00:00

I have a table that holds price information. I need to select the max

  • 0

I have a table that holds price information. I need to select the max value of every three rows. EXAMPLE:

Table `daily_high`
 ____ _______
| ID | HIGH  |
| 1  | 24.65 |
| 2  | 24.93 |
| 3  | 26.02 |
| 4  | 25.33 |
| 5  | 25.16 |
| 6  | 25.91 |
| 7  | 26.05 |
| 8  | 28.13 |
| 9  | 27.07 |
|____|_______|


Desired output to new table (ID will be auto-increment so don't assume an association exists between this ID 1 and the daily_high ID 1:
 ____ ___________
| ID | 3MaxHIGH  |
|____|___________|
| 1  | 26.02     |
| 2  | 25.91     |
| 3  | 28.13     |
|____|___________|

I want to compare IDs 1,2, and 3 to determine the high value among them. Then once I have compared 1-3, I want to move on to 4 through 6, then 7 through 9, etc until I’ve done this for all values contained in the table (currently about 400,000 values). I have written code that uses

SELECT max(HIGH) FROM daily_high as dh1 JOIN (SELECT max(HIGH) FROM daily_high WHERE id >= dh1 AND id < (dh1.id + 3))

This works but is horribly slow. I’ve tried using the SELECT statement where I identify the column values to be pull for display, meaning between the SELECT and FROM parts of the query.

I’ve tried to use JOIN to join all 3 rows onto the same table for comparison but it too is horribly slow. By slow I mean just under 10 seconds to gather information for 20 rows. This means that the query has analyzed 60 rows (20 groups of 3) in 9.65879893303 seconds (I didn’t make this up, I used microtime() to calculate it.

Anyone have any suggestions for faster code than what I’ve got?

Keep in mind that my actual table is not the same as what I’ve posted above, but it the concept is the same.

Thanks for any 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-06-15T03:29:04+00:00Added an answer on June 15, 2026 at 3:29 am

    Why not to use DIV operator for grouping your aggregation:

    SELECT (id-1) DIV 3 + 1 AS ID, MAX(high) AS 3MaxHIGH 
    FROM daily_high 
    GROUP BY (id-1) DIV 3
    

    This query gives the same result.

    ID  3MaxHIGH
    1   26.02
    2   25.91
    3   28.13
    

    I was unable to run your query, and I believe that this one is faster.

    UPD: To ensure that you have valid groups for your ranges, use this query:

    select id, high, (id-1) div 3 + 1 from daily_high
    

    result:

    id  high    (id-1) div 3 + 1
    1   24.65   1
    2   24.93   1
    3   26.02   1
    4   25.33   2
    5   25.16   2
    6   25.91   2
    7   26.05   3
    8   28.13   3
    9   27.07   3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MYSQL table that holds events information. I need a mechanism that
I have a table that holds information about different events, for example CREATE TABLE
Given that I have a table that holds vehicle information and one of those
i have a table that holds my users information like username , password ,
Hi I have a datatables based table that holds data. I need to be
I have a table (table variable in-fact) that holds several thousand (50k approx) rows
I have a table that holds subscription information. Some of the mailing zip codes
I have a table that holds listing information for housing properties. A property may
I want to have a table that holds information about a task. PriorityId, StateId,
I have a table in my SQL Server DB that holds auditing information for

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.