We have a script that runs once per day to update prices for an associated system by looking up new product prices by date. Ex:
ID | product_id | date | price
------------------------------------
1 | 1 | 2014-03-01 | 199
2 | 1 | 2014-04-01 | 299
3 | 2 | 2014-03-11 | 150
4 | 2 | 2014-04-17 | 172
So, our script runs on April 1, and performs a SELECT * FROM table WHERE date='2014-04-01';
and then uses that product_id column to update the other system with the new price. This all works perfectly fine. We've got a problem now though where if the system doesn't run one day, or the user puts in a date in the past, the system will never update with the new pricing.
So the question is: What would the query look like that selects the max(date) for each distinct product_id where date <= today?
I've tried various methods of distinct() and group by's, but can't ever get the result I'm looking for. That result would be that when the script runs on April 2, it sees that the max date < today is April 01, so grabs those prices.