Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Valid price at given date

Tags:

date

sql

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'? Without me knowing of the other dates.

Pseudoquery: select price where currentprice('2012-01-07')

Thanks!

like image 324
freand Avatar asked Oct 21 '25 07:10

freand


1 Answers

MySQL:

select price from your_table 
where date <= '2012-01-07'
order by date desc
limit 1

SQL Server:

select top 1 price from your_table 
where date <= '2012-01-07'
order by date desc
like image 184
juergen d Avatar answered Oct 23 '25 22:10

juergen d



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!