title: weighted standard deviation in sql server without aggregation error
body: <p>Redoing the weighted mean (which is already in another column) in working out the weighted-Sum-Of-Squared-Deviations, results in the error "Cannot perform an aggregate function on an expression containing an aggregate or a subquery."</p>
  
  <pre><code>SQRT(SUM(wt.value*SQUARE(out1.value-(SUM(ISNULL(out1.value,0)*wt.value)/SUM(wt.value))))
      / (((COUNT(wt.value)-1)*SUM(wt.value))/(COUNT(wt.value)))) as wsdevInc0
  </code></pre>
  
  <p>Can you suggest how to calculate the WEIGHTED STANDARD DEVIATION in SQL, short of adding another level of 'SELECT' to the query hierarchy (and probably duplicating the selection of data)?</p>