MySQL - 连接两个带有日期时间列的表和该日期之前的三个最新条目
我的 SQL 中有两个表
例如 Table1 - ItemPrice:
DATETIME | ITEM | PRICE
2011-08-28 | ABC 123
2011-09-01 | ABC 125
2011-09-02 | ABC 124
2011-09-03 | ABC 127
2011-09-04 | ABC 126
Table2 - DayScore:
DATETIME | ITEM | SCORE
2011-08-28 | ABC 1
2011-08-29 | ABC 8
2011-09-01 | ABC 4
2011-09-02 | ABC 2
2011-09-03 | ABC 7
2011-09-04 | ABC 3
我想编写一个查询,给出一个项目 ID(例如 ABC< /em>),将从 ItemPrice
返回该日期的价格(如果该日期没有价格,则查询不应返回任何内容)。如果找到查询日期的有效价格,则查询应返回(9 列)
- 过去三天
ItemPrice
中商品的价格(即查询日期之前的最近 3 个价格) )。 - 在接下来的三列中,它应该从
DayScore
返回从 ItemPrice 中选择的 3 个日期的匹配分数。 - 最后选择日期(t-1 到 t-3)
换句话说,仅查看 date='2011-09-03' 作为 item='abc' 示例的此查询的结果将返回:
DATE | ITEM | PRICE | SCR | PRC_t-1 | PRC_t-2 | PRC_t-3 | SCR_t-1 | SCR_t-2 | SCR_t-3 | DATE_t-1 | DATE_t-2 | DATE_t-3
2011-09-03| ABC | 127 | 7 | 124 | 125 | 123 | 2 | 4 | 1 | 2011-09-02| 2011-09-01| 2011-08-28
....
Etc 对于出现的每个日期在 ItemPrice
表中。
运行此查询的最简洁、最有效的方法是什么(因为它将运行数百万行)?
干杯!
I have two tables in my SQL
For example Table1 - ItemPrice:
DATETIME | ITEM | PRICE
2011-08-28 | ABC 123
2011-09-01 | ABC 125
2011-09-02 | ABC 124
2011-09-03 | ABC 127
2011-09-04 | ABC 126
Table2 - DayScore:
DATETIME | ITEM | SCORE
2011-08-28 | ABC 1
2011-08-29 | ABC 8
2011-09-01 | ABC 4
2011-09-02 | ABC 2
2011-09-03 | ABC 7
2011-09-04 | ABC 3
I want to write a query, which given a item ID (e.g. ABC), will return the price at that date from ItemPrice
(of there is no price for that date then the query should not return anything). If a valid price is found for the query date, the query should return (in 9 columns)
- the price of the item from
ItemPrice
for the past three days (i.e. the most recent 3 prices before the date queried). - In the next three columns it should return, from
DayScore
, the matching score for those 3 dates selected from ItemPrice. - Finally the dates (t-1 to t-3) selected
In otherwords the results for this query looking at just date='2011-09-03' as an example for item='abc' would return:
DATE | ITEM | PRICE | SCR | PRC_t-1 | PRC_t-2 | PRC_t-3 | SCR_t-1 | SCR_t-2 | SCR_t-3 | DATE_t-1 | DATE_t-2 | DATE_t-3
2011-09-03| ABC | 127 | 7 | 124 | 125 | 123 | 2 | 4 | 1 | 2011-09-02| 2011-09-01| 2011-08-28
....
Etc for each date that appears in ItemPrice
table.
What is the neatest and most efficient way to run this query (as its something that will be run over many millions of rows)?
Cheers!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
几乎没有,但它确实产生了结果。您可能可以删除一些子选择并使其少一点sql,但我尝试逐步构建它,以便您可以推断它正在做什么。
核心部分是这个选择:
这将返回一个包含日期的表(现在,t-1,t-2,t-3)。从那里可以简单地连接每个日期的价格和分数。包括 testdata 在内的所有内容都变成了一大堆 sql,
当您对 1M 行执行此操作时,我很好奇您的解释计划:) 如果您拥有正确的索引(您可能会这样做),那么它甚至可能不会那么可怕。
Pretty no but it does produce the results. You could probably get rid of some subselects and make it a bit less sql but I tried to build it up in steps so you can deduct what it is doing.
The core part is this select:
This returns a table with the dates (now, t-1, t-2, t-3). From there is is simple joining with price and score for each of those dates. The whole things including testdata the becomes this bulk of sql
I'm curious about your explain plan when you do this on 1M rows :) It might not even be that horrible if you have the right indexes which you probably do.