简体   繁体   English

获取每个日期的最近记录

[英]Get the closest record for every date

I'm trying to create a query that returns all the prices from table skucompetition for every date in table creationdateformat . 我试图创建一个返回所有的价格从表的查询skucompetition在表中的每个日期creationdateformat

To clarify, creationdateformat has these dates in it: 要澄清的是,creationdateformat中包含以下日期:

creationdateformat
2010-10-25
2010-10-26
2010-11-10
2010-11-24
2010-11-25

skucompetition has these prices for it: skucompetition具有以下价格:

sku              creationdate           price
PCR-BR2495112   2010-10-26 16:06:03    24.99
PCR-BR2495112   2010-11-10 13:01:43    27.99
PCR-BR2495112   2010-11-25 12:24:26    26.51

For date 2010-10-25 it should return 0, as it was before the first price existed. 对于日期2010-10-25,它应该返回0,因为它是第一个价格存在之前的状态。

For date 2010-10-26 it should return 24,99 对于日期2010-10-26,它应返回24,99

For date 2010-11-10 and 2010-11-24 it should return 27.99 对于日期2010-11-10和2010-11-24,应返回27.99

For date 2010-11-25 it should return 26.51 对于日期2010-11-25,应返回26.51

And so on. 等等。

How can this best be achieved? 如何最好地实现?

This should point you in the right direction... 这应该为您指明正确的方向...

Judging from your sample data, you're looking for the most recent record either before or on a specific date. 从样本数据来看,您正在寻找特定日期之前或特定日期的最新记录。

You're only interested in a single record, so you should limit your selection to the first record. 您只对单个记录感兴趣,因此应将选择限制为第一条记录。

You can simplify the before or on part. 您可以简化之前或部分内容。 Anything 'before or on today' is the same as anything 'before tomorrow'. “今天之前或之上”与“明天之前”相同。 So you'll be looking for anything 'before the input date plus one day '. 因此,您将在“输入日期加一天之前”查找任何内容。 This simplifies the query, because you won't have to worry about the exact time of day. 这简化了查询,因为您不必担心一天中的确切时间。

Finally, to make sure you get the most recent record, you'll have to order the records. 最后,要确保获得最新记录,您必须订购记录。

In pseudo-code it would be: 用伪代码将是:

select the first record
from skucompetition
where creationdate is before (input date + one day)
order by creationdate so most recent record comes first

You should handle the special case of returning 0 in your application. 您应该处理在应用程序中返回0的特殊情况。 If you have constructed the query correctly, it won't match any records for input dates before the first date. 如果您正确地构造了查询,则该查询将与第一个日期之前的输入日期的任何记录都不匹配。 That's when your application should handle the 'return 0' case. 那是您的应用程序应处理“返回0”的情况。

I would create a query that: 我将创建一个查询:

  • selects all records with the creationdate on or later than the date you're looking at 选择所有创建日期在您查找日期或之后的记录
  • order those from the earliest to the latest 从最早到最新订购
  • and then use a "limit" to only return the first record 然后使用“限制”仅返回第一条记录

The exact syntax will of course depend on which RDBMS you use 当然,确切的语法取决于您使用的RDBMS

SELECT price FROM skucompetition
WHERE sku = "<sku>" AND DATE(creationdate) <= "<date>"
ORDER BY date DESC LIMIT 1

This will select the first record with the right SKU that is not older than "date". 这将选择正确的SKU不早于“日期”的第一条记录。 If there's none, NULL will be returned. 如果不存在,将返回NULL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM