• I'm not sure why you have two separate subqueries to calculate the two standard deviations. It looks to me like it could be one.

    I would also change the whole thing to a CTE. It should be much faster that way.