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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:56:37+00:00 2026-05-13T18:56:37+00:00

I have a simple table called that contains share prices in MySQL: Table `share_prices`

  • 0

I have a simple table called that contains share prices in MySQL:

Table `share_prices`

+----------+-------+---------------------+
| stock_id | price | date                |    
+----------+-------+---------------------+    
|        1 |  0.05 | 2010-02-24 01:00:00 |
|        2 |  3.25 | 2010-02-24 01:00:00 |
|        3 |  3.30 | 2010-02-24 01:00:00 |
|        1 |  0.50 | 2010-02-23 23:00:00 |
|        2 |  1.90 | 2010-02-23 23:00:00 | 
|        3 |  2.10 | 2010-02-23 23:00:00 |
|        1 |  1.00 | 2010-02-23 19:00:00 |
|        2 |  1.00 | 2010-02-23 19:00:00 | 
|        3 |  1.00 | 2010-02-23 19:00:00 | 
+----------+-------+---------------------+

Every time a share price is updated, a new row is inserted into the table.

With this structure, how can I return a query that shows the price change in the last 24 hours?

The desired result would be:

+----------+------+------+------------+
| stock_id | then | now  | difference |
+----------+------+------+------------+    
|        3 | 1.00 | 3.30 |       2.30 |
|        2 | 1.00 | 3.25 |       2.25 |
|        1 | 1.00 | 0.05 |      -0.95 |
+----------+------+------+------------+

What’s the best way to go about this? Some kind of join? A sub-query?

What I think I’m aiming for is to essentially query once to get then, query again to get now and then somehow glue it all together at the end.

Edit: I need to account for negative changes too.

  • 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-13T18:56:37+00:00Added an answer on May 13, 2026 at 6:56 pm

    Ok, got home, and was able to figure this out.

    SELECT stock_id, t1.price AS `then`, t2.price AS `now`, ROUND(t2.price - t1.price, 2) AS `difference`
    FROM (
            SELECT stock_id, price, date FROM share_prices sp
            WHERE  date = (SELECT MIN(date) FROM share_prices sp2
                           WHERE date BETWEEN '2010/02/23 10:00:00'
                           AND '2010/02/24 10:00:00'
                           AND sp2.stock_id = sp.stock_id)
        ) t1
        JOIN
        (
            SELECT stock_id, price, date FROM share_prices sp
            WHERE  date = (SELECT MAX(date) FROM share_prices sp2
                           WHERE date BETWEEN '2010/02/23 10:00:00'
                           AND '2010/02/24 10:00:00'
                           AND sp2.stock_id = sp.stock_id)
        ) t2 USING(stock_id)
    ORDER BY `difference` DESC
    

    Uses the results from 2 subqueries, each with their own subquery to the first and last, respectively, record for that range.

    I was using integer for stock_id, float for price and timestamp for date, since there may be issues (notably with the MIN and MAX) with other data types.

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

Sidebar

Related Questions

Lets say I have a simple table that only contains two columns: MailingListUser -
I have a very simple table called Member , which consists of the following:
I have a simple ActiveRecord model called Student with 100 records in the table.
I have a table called products containing a field called price and I simply
I have a simple table that has this structure: <table> <thead> <tr> <td>Some info</td>
The Situation I have a table in a DB that contains job types, basically
I have a table called devicesegments , each row of which contains a large
I have a table called orders this table contains all the information we need.
I have a EF entity that is tied to a SQL table that contains
I have a stored procedure that gets called hundreds of times per minute. Every

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.