sql server - How to do arithmetic operations on data fields and aggregate functions in where clause? -


i'm using query return distribution of float field around average:

select  count(*) [count], result    (   select round(result - avg(result) over(), 1) result results) group  result 

this query returns distance between records , mean. now, need filter records out of ±3sd range. thought perhaps achieve changing query this:

select  count(*) [count], result    (   select round(result - avg(result) over(), 1) result results      having abs(result - avg(result)) over() < 3 * stdev(result) over()) group  result 

but sql server giving me 2 errors:

column 'results.result' invalid in having clause because not contained in either aggregate function or group clause.

windowed functions can appear in select or order clauses.

how can achieve i'm looking for? google isn't kind me today :-(

as second error message indicated, windowed functions can appear in select or order clauses - rather including them in having clause, include them in inner select , select on them in outer clause - so:

select  count(*) [count], result     (select round(result - avg(result) over(), 1) result,         abs(result - avg(result) over()) avgdiff,         stdev(result) over() stddev  results) r avgdiff < 3 * stddev group  result 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Socket.connect doesn't throw exception in Android -

iphone - How do I keep MDScrollView from truncating my row headers and making my cells look bad? -