dept_stats is slow to compute. Save its current result into a real table dept_stats_cache.
dept_stats
dept_stats_cache
Each hint gives a little more help. Try on your own first.
Run your code to see the output here.