Display last date grouped by warehouse and product I have a table called daily_stock with the following fields:

+-----------+----------+------------+----------+
| warehouse | product  | date       | stock    |
+-----------+----------+------------+----------+
| 01        | A        | 2016/04/01 | 100      |
| 01        | A        | 2016/04/05 | 80       |
| 01        | A        | 2016/04/18 | 35       |
| 02        | A        | 2016/04/02 | 1        |
| 02        | A        | 2016/04/06 | 10       |
| 02        | A        | 2016/04/16 | 18       |
| 01        | B        | 2016/04/03 | 25       |
| 01        | B        | 2016/04/11 | 66       |
| 01        | B        | 2016/04/29 | 45       |
| 02        | B        | 2016/04/05 | 19       |
| 02        | B        | 2016/04/19 | 1        |
+-----------+----------+------------+----------+

Now I need to display products last date with stock in all warehouses.

The result I am trying to get is:

+----------+----------+------------+----------+
| 01       | A        | 2016/04/18 | 35       |
| 02       | A        | 2016/04/16 | 18       |
| 01       | B        | 2016/04/29 | 45       |
| 02       | B        | 2016/04/19 | 1        |
+----------+----------+------------+----------+
Does this work for you? I am testing in MySQL.

SELECT LatestStock.*
FROM daily_stock as LatestStock
JOIN (  
    SELECT warehouse, product, MAX([date]) as MAXdate
    FROM daily_stock 
    GROUP BY warehouse, product
    ) as LatestDate on LatestStock.warehouse = LatestDate.warehouse AND LatestStock.product = LatestDate.product AND LatestStock.date = LatestDate.MAXdate
ORDER BY LatestStock.product, LatestStock.warehouse