I have a table numberoffirms like this:
--------------------------------------------
| id | region | month | year | value |
--------------------------------------------
| 1| 1| 1| 1| 1024001|
| 2| 1| 7| 25| 33542|
| 3| 1| 3| 26| 10000000|
| 4| 1| 5| 26| 1995|
| 5| 2| 4| 16| 123456|
My problem is that I need to get the highest value per region based on highest month and year. For example, from the table above, i should get the value 1995 because in region 1, the max year is 26 and in that year, the max month is 5.
Now when i run this SQL query which i made, it does not get the right value.
SELECT region, MAX(month) month, year, value FROM `numberoffirms` WHERE year IN (SELECT MAX(year) year FROM `numberoffirms` GROUP BY region) GROUP BY region
The value it gets is this
| region | month | year | value |
------------------------------------
| 1| 5| 26| 10000000 |
which is clearly wrong since the value i should get is 1995, though it got the region, month, and year correct.
What am i doing wrong with my SQL query code here?