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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:51:29+00:00 2026-06-16T16:51:29+00:00

I have two tables sharing a common key ‘itemID’, the first table holds the

  • 0

I have two tables sharing a common key ‘itemID’, the first table holds the latest price for each itemID, the second table holds the date and value of any change to the itemID price as shown. The tLatestItemPrice table is automatically updated whenever a new entry is put into tPriceHistory table

tLatestItemPrice TABLE
itemID,   latestPrice
---------------------
item1     400
item2     75
item3     621


tPriceHistory TABLE
itemID,   PriceChangeDate,  NewPrice
------------------------------------
item1     Jan 8th 2012      400
item1     Jan 7th 2012      300
item1     Jan 6th 2012      280
item1     Jan 3rd 2012      270
item2     Jan 8th 2012      75
item2     Jan 5th 2012      72
item2     Jan 1st 2012      60
item3     Jan 7th 2012      621
item3     Jan 6th 2012      601
item3     Jan 2nd 2012      598

I want to have a query that returns me the price difference between the latest price in tLatestItemPrice table and the price of the item at a specific date. ie if I ask for the change of price between the latest and the 4th of January I want a query to return the following dataset

itemID    Price change from 4th Jan
--------------------------
item1     130  (i.e. 400-270)
item2      15
item3      23

Running mysql Ver 14.14 Distrib 5.5.29, for Linux (x86_64)

  • 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-16T16:51:30+00:00Added an answer on June 16, 2026 at 4:51 pm

    This is what it become when you have date stored as string in the table

    SELECT  a.ITEMID, 
            d.NewPrice - a.NEWPRICE `Price change from 4th Jan`
    FROM    tPriceHistory a
            INNER JOIN
            (
              SELECT itemID, 
                     DATE_FORMAT(MAX(STR_TO_DATE(PriceChangeDate, '%b %D %Y')),'%b %D %Y') maxDate
              FROM   tPriceHistory
              WHERE  STR_TO_DATE(PriceChangeDate, '%b %D %Y') <= '2012-01-04'
              GROUP BY itemID
            ) AsOfDatePrice ON a.itemID = AsOfDatePrice.ItemID AND
                               a.PriceChangeDate = AsOfDatePrice.MaxDate
            INNER JOIN 
            (
              SELECT itemID, 
                     DATE_FORMAT(MAX(STR_TO_DATE(PriceChangeDate, '%b %D %Y')),'%b %D %Y') maxDate
              FROM   tPriceHistory
              GROUP BY itemID 
            ) LatestDate ON a.itemID = LatestDate.itemid
            INNER JOIN tPriceHistory d
                ON LatestDate.itemid = d.itemID AND
                   LatestDate.maxDate = d.PriceChangeDate
    
    • SQLFiddle Demo Link
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables with multiple connections, for example: First table: Tbl_Flights, it has
I have two tables: orders and orders_items. Both sharing the field orderID. I want
I have two tables, lets say table1 and table2 with common columns, id and
I have two tables: table a ida valuea 1 a 2 b 3 c
I have two tables one with ID and NAME table 1 ID | NAME
I have two tables user table user_id | name | 1 | peter |
I have two tables, a user table and a application table: User id username
I have two tables, like this: master --------- empcode INT PRIMARY KEY name VARCHAR
I have two tables: A (col1,col2,col3,col4) B (col1,col2,col3,col4) Table A has 4 records(rows) and
I have a table that has two foreign keys to two different tables with

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.