sql server - Interquartile Range - Lower, Upper and Median -
i'm trying work out interquartile range based on array of numbers can length e.g.
1, 1, 5, 6, 7, 8, 2, 4, 7, 9, 9, 9, 9
the values need work out interquartile range are:
- upper quartile
- median
- lower quartile
if dump above array of numbers microsoft excel (columns a:m), can use following formulas:
=quartile.inc(a1:m1,1)
=quartile.inc(a1:m1,2)
=quartile.inc(a1:m1,3)
to answers of:
- 4
- 7
- 9
i need work out these 3 values in either sql server or vb.net. can array values in format or object in either of these languages, can't find functions exist quartile.inc
function excel has.
does know how achieved in either sql server or vb.net?
there might easier way, quartiles, can use ntile (transact-sql)
distributes rows in ordered partition specified number of groups. groups numbered, starting @ one. each row, ntile returns number of group row belongs.
so data:
select 1 val #temp union select 1 union select 5 union select 6 union select 7 union select 8 union select 2 union select 4 union select 7 union select 9 union select 9 union select 9 union select 9 -- ntile(4) specifies require 4 partitions (quartiles) select ntile(4) on ( order val ) quartile , val #tempquartiles #temp select * #tempquartiles drop table #temp drop table #tempquartiles
this produce:
quartile val 1 1 1 1 1 2 1 4 2 5 2 6 2 7 3 7 3 8 3 9 4 9 4 9 4 9
from can work out you're after.
so modifying select
can this:
select quartile, max(val) maxval #tempquartiles quartile <= 3 group quartile
to produce:
quartile maxval 1 4 2 7 3 9
Comments
Post a Comment