I create a stock storage system and decided to store each product balance (every time it was updated) in the following table:
+------------+--------------+---------+------+ | Product_id | Warehouse_id | Balance | Date | +------------+--------------+---------+------+
Example:
Staff adds 10 pieces to product_id 123 in store_id 5
+------------+--------------+---------+-------------+ | Product_id | Warehouse_id | Balance | Date | +------------+--------------+---------+-------------+ | 123 | 5 | 10 | 2013-09-16 | +------------+--------------+---------+-------------+
The character then adds 3 pieces to product 234 in store_id 5 and 5 pieces to 123 in store_id 5,
+------------+--------------+---------+-------------+ | Product_id | Warehouse_id | Balance | Date | +------------+--------------+---------+-------------+ | 123 | 5 | 10 | 2013-09-16 | | 234 | 5 | 3 | 2013-09-18 | | 123 | 5 | 15 | 2013-09-21 | +------------+--------------+---------+-------------+
* Note the date column
Now let me add some more lines
+------------+--------------+---------+-------------+ | Product_id | Warehouse_id | Balance | Date | +------------+--------------+---------+-------------+ | 123 | 5 | 10 | 2013-09-16 | | 234 | 5 | 3 | 2013-09-18 | | 123 | 5 | 15 | 2013-09-21 | | 123 | 5 | 18 | 2013-09-24 | | 234 | 5 | 10 | 2013-09-26 | | 123 | 5 | 22 | 2013-09-29 | +------------+--------------+---------+-------------+
How can I launch a request that will allow me to get the balance of all products as of September 25, 2013?
This means that I need the following result:
+------------+--------------+---------+-------------+ | Product_id | Warehouse_id | Balance | Date | +------------+--------------+---------+-------------+ | 234 | 5 | 3 | 2013-09-18 | | 123 | 5 | 18 | 2013-09-24 | +------------+--------------+---------+-------------+
In short, I need the last line (by date) for product_id.
Any help would be greatly appreciated!
source share