T-SQL有效剥离冗余数据
我有一个表,用于跟踪各种商品随时间的价格数据。下面是一个简化的示例:
表名 [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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,SQL 并不是一种非常适合处理有序集的语言(关系数据库非常适合它,但 SQL 语言则不然)。此外,直到 SQL Server 2005 才引入了一些使处理这些集更容易的 T-SQL 功能(例如,
ROW_NUMBER()
)。考虑到 SQL Server 2000 的限制,您将必须做这样的事情:(
我没有可供测试的 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:
(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.