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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:15:17+00:00 2026-06-01T18:15:17+00:00

I have a price history table that has these 4 fields Id Date Product

  • 0

I have a price history table that has these 4 fields

  1. Id
  2. Date
  3. Product
  4. Price

There is a record for ever day for every product. I’m trying to write a query to return a record for the starting price of each product along with a record for each time the price changed.

I tried grouping by price but obviously this breaks when a price changes than at a later date changes back as it will only return 1 record.

I’ve written this query to generate sample data that I am trying to work off

CREATE TABLE #PriceHistory
(
 [Id] INT IDENTITY,
 [Date] DATETIME ,
 [Product] NVARCHAR(30) ,
 [Price] MONEY 
)

INSERT INTO #PriceHistory([Date],[Product],[Price])
SELECT '20120101', 'Tesco', 1.99
UNION ALL
SELECT '20120102', 'Tesco', 1.97
UNION ALL
SELECT '20120103', 'Tesco', 1.97
UNION ALL
SELECT '20120105', 'Tesco', 1.99
UNION ALL
SELECT '20120104', 'Tesco', 1.99
UNION ALL
SELECT '20120106', 'Tesco', 1.99
UNION ALL
SELECT '20120101', 'BP', 1.99
UNION ALL
SELECT '20120102', 'BP', 1.01
UNION ALL
SELECT '20120103', 'BP', 1.99

SELECT * FROM #PriceHistory


DROP TABLE #PriceHistory

From that sample data the results I’m expecting should be

1 2012-01-01 Tesco 1.99
2 2012-01-02 Tesco 1.97
5 2012-01-04 Tesco 1.99
9 2012-02-11 BP 1.99
8 2012-02-20 BP 1.01

Any ideas on the best way to achieve this?

  • 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-01T18:15:18+00:00Added an answer on June 1, 2026 at 6:15 pm

    This query starts with a price, and attempts to find the previous record for the same product with the same price… if there’s no record for the same product at the same price, it returns the record:

    SELECT ph.* 
    FROM 
        #PriceHistory ph 
        LEFT JOIN #PriceHistory ph2 ON
            ph.Product = ph2.Product
            AND ph.Price = ph2.Price
            AND ph2.Date = (
                SELECT MAX(ph3.Date)
                FROM #PriceHistory ph3
                WHERE 
                    ph3.Product = ph.Product
                    AND ph3.Date < ph.Date
                )
    WHERE ph2.ID IS NULL
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 2 tables product and history product table: id name type price location
I have two MySQL tables (product and price history) that I would like to
I have the following table: Order Product Price Quantity Description Order1 Product1 12 1
We have an item price file that consists of Item number, Price Effective Date,
I have two tables. First table have columns: price, code_one Second table has: code_one,
I have a product table and a price table where a product can have
I have a 'price' variable that contains some integer number from a MySQL database.
I have a form that saves the price into the database but whn a
I have a problem getting the right Price for a product based on Effectivity
I have a script which returns a price for a product. However, the price

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.