2017-12-07 2 views
-4

を記録:SQL Serverの:トップの平均値(n)とボトムを取得する方法は、(n)は、私はスコアのテーブルを持っている

ALLの平均、上位15%とボトム15%などを取得する方法
Student ¦ Score 
    A  ¦ 23.5 
    B  ¦ 34.9 

これは:

Average ¦ top15Avg ¦ bottom15Avg 

ありがとう。

+0

これまでに試したこと、適切な入力と正しい出力を示してください。あなたが少なくとも15%の上位を言っているときには、より多くの行を提供して有意義な例にする必要があります。 – sia

答えて

0

まず、15%となる行の数を計算します。 次に、このような選択と平均を行うことができます。

declare @pct int, @all float, @top float, @bottom float 
select @pct = 0.15 * COUNT(*) from Students 

select @all = AVG(score) from Students 
select @top = AVG(score) from (select top (@pct) score from Students order by score desc) 
select @bottom = AVG(score) as 'bottom15Avg' from (select top (@pct) score from Students order by score asc) 
select @all as 'Average', @top as 'top15Avg', @bottom as 'bottom15Avg' 
関連する問題