[英]Valid price at given date
got a table with dates and prices. 有一张日期和价格表。
Date Price 2012-01-01 25 2012-01-05 12 2012-01-10 10
Is there some kind of function that lets me find what the current price where at '2012-01-07'? 是否有某种功能可以让我找到“2012-01-07”的当前价格? Without me knowing of the other dates.
没有我知道其他日期。
Pseudoquery: select price where currentprice('2012-01-07') 伪查询:选择当前价格的价格('2012-01-07')
Thanks! 谢谢!
MySQL: MySQL的:
select price from your_table
where date <= '2012-01-07'
order by date desc
limit 1
SQL Server: SQL Server:
select top 1 price from your_table
where date <= '2012-01-07'
order by date desc
If you don't have use of ROW_NUMBER(), and want a generic solution, you need to join on a sub-query. 如果您没有使用ROW_NUMBER(),并且想要通用解决方案,则需要加入子查询。
Get the date you want, then get the data for that date. 获取所需的日期,然后获取该日期的数据。
SELECT
*
FROM
yourTable
INNER JOIN
(
SELECT MAX(yourDate) AS maxDate FROM yourTable WHERE yourDate <= @dateParameter
)
AS lookup
ON yourTable.yourDate = lookup.maxDate
select price
from table1 t
where t.date = ( select max(t2.date)
from table1 t2
where t2.date <= '2012-01-07' )
Note this is not the copy&paste answer, as we're not not knowing what is the datatype for your date
column. 请注意,这不是复制和粘贴答案,因为我们不知道您的
date
列的数据类型是什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.