I have a table that tracks price data over time for various goods. Here’s a simplified example:
Table name [Product_Prices]
PRODUCT DATE PRICE
------------------
Corn 1/1/2011 1.35
Corn 1/2/2011 1.40
Corn 1/3/2011 1.40
Corn 1/4/2011 1.50
Beef 1/1/2011 1.35
Beef 1/2/2011 1.15
Beef 1/3/2011 1.15
Beef 1/4/2011 1.30
Beef 1/5/2011 1.30
Beef 1/6/2011 1.35
I want a query that pulls the earliest date that the prices changed, for each instance where the price actually did change. Based on the sample table above, this is the output I want:
PRODUCT DATE PRICE
------------------
Corn 1/1/2011 1.35
Corn 1/2/2011 1.40
Corn 1/4/2011 1.50
Beef 1/1/2011 1.35
Beef 1/2/2011 1.15
Beef 1/4/2011 1.30
Beef 1/6/2011 1.35
I am currently doing it in a cursor but it’s incredibly inefficient and I feel that there must be a simpler way to get this data. The table I’m working with has about 2.3 million records.
SQL 2000
Thanks!
SQL is, unfortunately, not a language that’s well-suited to working with ordered sets (relational databases are great for it, but the SQL language is not). Additionally, some of the T-SQL features that make working with these sets easier (
ROW_NUMBER(), for example) were not introduced until SQL Server 2005.Given the restriction to SQL Server 2000, you’ll have to do something like this:
(I don’t have SQL Server 2000 available to test, but I believe this should function correctly on 2000)
This will retrieve every row from
Product_Priceswhere the price for that product is not equal to the previous record for that product.