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
Post a Comment