SQLSERVER中PERCENTILE

來源:互聯網
上載者:User

   SQL SERVER中PERCENTILE_CONT和PERCENTILE_DISC

  PERCENTILE_CONT和PERCENTILE_DISC

  看下面一組SQL語句:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 WITH test as (     select N'LeeWhoeeUniversity' as name,10 as score     UNION ALL     select N'LeeWhoeeUniversity',20     UNION ALL     select N'LeeWhoeeUniversity',30     UNION ALL     select N'LeeWhoeeUniversity',40     UNION ALL     select N'LeeWhoeeUniversity',50     UNION ALL     select N'DePaul',60     UNION ALL     select N'DePaul',70     UNION ALL     select N'DePaul',80     UNION ALL     select N'DePaul',90     UNION ALL     select N'DePaul',100 ) select name,score ,PERCENT_RANK() over(partition by name order by score) as per_rnk ,PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY score) over(partition by name) as percont0_5 ,PERCENTILE_CONT(0.6) WITHIN GROUP (ORDER BY score) over(partition by name) as percont0_6 ,PERCENTILE_CONT(0.7) WITHIN GROUP (ORDER BY score) over(partition by name) as percont0_7 ,PERCENTILE_CONT(0.75) WITHIN GROUP (ORDER BY score) over(partition by name) as percont0_75 ,PERCENTILE_DISC(0.5) WITHIN GROUP (ORDER BY score) over(partition by name) as perdist0_5 ,PERCENTILE_DISC(0.6) WITHIN GROUP (ORDER BY score) over(partition by name) as perdist0_6 ,PERCENTILE_DISC(0.7) WITHIN GROUP (ORDER BY score) over(partition by name) as perdist0_7 ,PERCENTILE_DISC(0.75) WITHIN GROUP (ORDER BY score) over(partition by name) as perdist0_75 from test

  運行結果:

  name score per_rnk percont0_5 percont0_6 percont0_7 percont0_75 perdist0_5 perdist0_6 perdist0_7 perdist0_75

  LeeWhoeeUniversity 10 0 30 34 38 40 30 30 40 40

  LeeWhoeeUniversity 20 0.25 30 34 38 40 30 30 40 40

  LeeWhoeeUniversity 30 0.5 30 34 38 40 30 30 40 40

  LeeWhoeeUniversity 40 0.75 30 34 38 40 30 30 40 40

  LeeWhoeeUniversity 50 1 30 34 38 40 30 30 40 40

  DePaul 60 0 80 84 88 90 80 80 90 90

  DePaul 70 0.25 80 84 88 90 80 80 90 90

  DePaul 80 0.5 80 84 88 90 80 80 90 90

  DePaul 90 0.75 80 84 88 90 80 80 90 90

  DePaul 100 1 80 84 88 90 80 80 90 90

  簡單理解,PERCENT_RANK前面介紹過,求出score百分比後,PERCENTILE_CONT和PERCENTILE_DISC就是根據百分比求出對應的score。但不同的是,某一百分比沒有對應的score時,PERCENTILE_CONT會根據百分比的偏差計算出一個新值,此值可能並不存在於score中。PERCENTILE_DISC得出的是偏向於最近百分比對應的score值,因此此值肯定存在於score中。

  如PERCENTILE_CONT(0.6)對應的值為34,偏差值我猜測是這樣計算出的:百分比75%和50%PERCENTILE_CONT計算出的值分別是40和30.那麼等式:(40-30)/(0.75-0.5)=新偏差值/0.6-0.5。PERCENTILE_CONT(0.6)在50%上的偏差值=4。所以,PERCENTILE_CONT(0.6)對應PERCENTILE_CONT(0.5)+4=34。

  再看PERCENTILE_DISC(0.6),直接將最近的0.5計算出的,PERCENTILE_DISC值取過來了。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.