简体   繁体   English

在给定日期的有效价格

[英]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.

相关问题 我如何加载单个价格,该价格在给定日期从数据到字段有效的情况下在给定日期有效 - How can I load a single price, that's valid on a given date when the data has valid from and to columns 检查日期在给定时间内是否有效 - Check if a date is valid with in a given period 确保“ expiration_date-X”落在有效的“ date_of_price”上(如果不是,请使用下一个有效的date_of_price) - making sure “expiration_date - X” falls on a valid “date_of_price” (if not, use the next valid date_of_price) 查找给定日期的有效时间段 - Find valid time period for given date SQL 查询以获取给定日期的有效数据 - SQL Query to Get Valid Data on Given Date 给定两个日期范围折扣表和产品价格,计算日期范围最终价格 - Given two date ranged discount tables and product price, calculate date ranged final price 在给定标准费率和季节性日期范围的情况下计算价格 - Calculate price given standard rates and seasonal date ranges 日期范围的MIN或MAX值 - 根据产品ID,价格和日期范围确定给定日期范围的最低价格 - MIN or MAX Value for Date Ranges - Determining lowest price for a given date range based on Product ID, Price, and Date Ranges BigQuery SQL:给定字符串输入。 如何确认它是有效日期 - BigQuery SQL: Given string input. how to confirm it is a valid date MySQL日期范围价格 - Mysql date range price
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM