开发者

T-SQL stripping redundant data efficiently

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/2开发者_如何学编程011 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:

select
    pp.Product,
    pp.Date,
    pp.Price

from Product_Prices pp

where pp.Price <> (select top 1 
                       pp2.Price 

                   from Product_Prices pp2 

                   where pp2.Date < pp.Date 
                     and pp2.Product = pp.Product 

                   order by pp2.Date desc)

(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_Prices where the price for that product is not equal to the previous record for that product.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜