I have two tables, one that stores the current price, and one that stores the historical price of items. I want to create a query that pulls the current price, and the difference between the current price and the most recent historical price.
In the historical table, I have the start and end times of the price, so I can just select the most recent price, but how do I pull it all together in one query? Or do I have to do a subquery?
select p.current_price,
h.historical_price
h.historical_time
from price p
inner join price_history h
on p.id = h.id
where max(h.historical_time)
This obviously doesn't work, but that is what I'm trying to accomplish.
This gives me the current and historical price. But I want to make sure I have the most RECENT price. How would I do this?