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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:26:30+00:00 2026-05-19T12:26:30+00:00

I have a MySQL table like this one: day int(11) hour int(11) amount int(11)

  • 0

I have a MySQL table like this one:

day     int(11) 
hour    int(11)    
amount  int(11)   

Day is an integer with a value that spans from 0 to 365, assume hour is a timestamp and amount is just a simple integer. What I want to do is to select the value of the amount field for a certain group of days (for example from 0 to 10) but I only need the last value of amount available for that day, which pratically is where the hour field has its max value (inside that day). This doesn’t sound too hard but the solution I came up with is completely inefficient.

Here it is:

SELECT q.day, q.amount 
    FROM amt_table q 
    WHERE q.day >= 0 AND q.day <= 4 AND q.hour = (
        SELECT MAX(p.hour) FROM amt_table p WHERE p.day = q.day
    ) GROUP BY day

It takes 5 seconds to execute that query on a 11k rows table, and it just takes a span of 5 days; I may need to select a span of en entire month or year so this is not a valid solution.

Anybody who can help me find another solution or optimize this one is really appreciated

EDIT

No indexes are set, but (day, hour, amount) could be a PRIMARY KEY if needed

  • 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-19T12:26:31+00:00Added an answer on May 19, 2026 at 12:26 pm

    Use:

    SELECT a.day, 
           a.amount
      FROM AMT_TABLE a
      JOIN (SELECT t.day,
                   MAX(t.hour) AS max_hour
              FROM AMT_TABLE t
          GROUP BY t.day) b ON b.day = a.day
                           AND b.max_hour = a.hour
     WHERE a.day BETWEEN 0 AND 4
    

    I think you’re using the GROUP BY a.day just to get a single amount value per day, but it’s not reliable because in MySQL, columns not in the GROUP BY are arbitrary — the value could change. Sadly, MySQL doesn’t yet support analytics (ROW_NUMBER, etc) which is what you’d typically use for cases like these.

    Look at indexes on the primary keys first, then add indexes on the columns used to join tables together. Composite indexes (more than one column to an index) are an option too.

    • 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 looks like this: `id` int(10) unsigned NOT NULL
I have a mysql table that looks like this: id col2 col3 col4 1
I have a MySQL table structured somewhat like this: type name value ===================== 1
I have a MySQL table that looks something like this: +-----+------------+ | id |
I have a mySQL table that has a column like this: ID ----- 0352
Let's say I have a MySQL table that is something like this: software table:
I have a table that looks like this one : +------+------+------------------+ | item |
I need to have MySQL query like this one: UPDATE table_name SET 1 =
I have a MySQL table like this: Payments +----+---------------------+---------+ | id | date (sorted
I have a MySQL table looking like this: > describe books; +---------------+--------------+------+-----+---------+----------------+ | Field

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.