SQL Server Group By Very Slow unless using temp tables
Sounds like a bad plan to me - and possibly around statistics.

When you insert the data into a temp table, you can get statistics auto created on it.

If your statistics think there aren't many records for that combination of stock and year, it could choose a really bad plan. Try updating your statistics and seeing how it goes. Or put a better indexing strategy together, to help the join as well as the filter and grouping.

You could also post the sqlplans...