Вот ваш решенный SQL:
select x.product,x.date,x.state,x.country
from
(select x.product,x.date,x.state,country
from (select product,max(date) date from table1 group by product) t1
join table2 on (left(t1.date,6) = table2.date)
join table1 x on (left(x.date,6) = left(t1.date,6))) x
join (select x.product,max(x.date) date
from (select product,max(date) date from table1 group by product) t1
join table2 on (left(t1.date,6) = table2.date)
join table1 x on (left(x.date,6) = left(t1.date,6))
group by x.product) y
on (x.product = y.product and x.date = y.date);
Пример:
mysql> create table table1(product varchar(20),date varchar(8), state varchar(20));
Query OK, 0 rows affected (0.41 sec)
mysql> insert into table1 values
-> ('A','20080107','NY'),
-> ('A','20080131','TX'),
-> ('B','20100212','CT'),
-> ('B','20100226','MT'),
-> ('C','20150312','HG'),
-> ('C','20140425','UP');
Query OK, 6 rows affected (0.13 sec)
Records: 6 Duplicates: 0 Warnings: 0
mysql>
mysql> create table table2(Product varchar(20),Date varchar(8),Country varchar(20));
Query OK, 0 rows affected (0.33 sec)
mysql> insert into table2 values
-> ('A','200801','USA'),
-> ('C','201503','AUS'),
-> ('B','201002','UK'),
-> ('B','201704','FIN'),
-> ('C','200605','IRE'),
-> ('A','200805','CAN');
Query OK, 6 rows affected (0.13 sec)
Records: 6 Duplicates: 0 Warnings: 0
mysql> select x.product,x.date,x.state,x.country
-> from
-> (select x.product,x.date,x.state,country
-> from (select product,max(date) date from table1 group by product) t1
-> join table2 on (left(t1.date,6) = table2.date)
-> join table1 x on (left(x.date,6) = left(t1.date,6))) x
-> join (select x.product,max(x.date) date
-> from (select product,max(date) date from table1 group by product) t1
-> join table2 on (left(t1.date,6) = table2.date)
-> join table1 x on (left(x.date,6) = left(t1.date,6))
-> group by x.product) y
-> on (x.product = y.product and x.date = y.date);
+---------+----------+-------+---------+
| product | date | state | country |
+---------+----------+-------+---------+
| A | 20080131 | TX | USA |
| B | 20100226 | MT | UK |
| C | 20150312 | HG | AUS |
+---------+----------+-------+---------+
3 rows in set (0.01 sec)
Демо
http://www.sqlfiddle.com/#!9/6ef0e5/11