T-SQL有效剥离冗余数据

发布于 2024-10-19 19:40:57 字数 745 浏览 1 评论 0原文

我有一个表,用于跟踪各种商品随时间的价格数据。下面是一个简化的示例:

表名 [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

我想要一个查询,对于价格实际发生变化的每个实例,提取价格更改的最早日期。根据上面的示例表,这是我想要的输出:

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

我目前正在游标中执行此操作,但效率非常低,我觉得必须有一种更简单的方法来获取这些数据。我正在使用的表大约有 230 万条记录。

SQL 2000

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不疑不惑不回忆 2024-10-26 19:40:57

不幸的是,SQL 并不是一种非常适合处理有序集的语言(关系数据库非常适合它,但 SQL 语言则不然)。此外,直到 SQL Server 2005 才引入了一些使处理这些集更容易的 T-SQL 功能(例如,ROW_NUMBER())。

考虑到 SQL Server 2000 的限制,您将必须做这样的事情:(

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)

我没有可供测试的 SQL Server 2000,但我相信这应该在 2000 上正确运行)

这将从 Product_Prices 检索该产品价格的每一行不等于该产品的先前记录。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文