Join subquery by year and month
There is one problem with aliases usability in different parts of a query - aliases defined in the SELECT clause are only available in GROUP BY, ORDER BY and HAVING, not in WHERE or JOIN .. ON, so as you do not add the table specificator to the second stat_year, it probably takes the same column and the = is trivially true.

You can check that with EXPLAIN EXTENDED &lt;query&gt;; SHOW WARNINGS; which will tell you how MySQL "sees" your query (canonizing all column references with db and table names).

You need to replace it by the actual expression - ON job.stat_year = YEAR(td.db_date) - the one in the GROUP BY can stay the same as they will be equivalent after the join is done.