SQL Server:最小/最大/百分位数

时间:2017-08-03 19:21:30

标签: sql-server

我试图获得每个主要职位的基本工资的计数,最小值,最大值和百分位数(第10,25,50,70,9)。

enter image description here

我收到以下错误:

  

Msg 8120,Level 16,State 1,Line 1
  专栏' dbo.ps_employee.Base'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。

2 个答案:

答案 0 :(得分:0)

您必须在GROUP BY部分底部的选择列表中包含所有未汇总的项目。

  

文档:https://docs.microsoft.com/en-us/sql/t-sql/queries/select-group-by-transact-sql

答案 1 :(得分:0)

我能够使用以下查询来获得百分位数。我使用了一个单独的计数/分钟/最大/平均值。

    SELECT DISTINCT mj.title, PERCENTILE_DISC(.1) WITHIN GROUP (ORDER BY e.base) 
OVER (PARTITION BY mj.title) AS '10th', PERCENTILE_DISC(.25) WITHIN GROUP (ORDER
 BY e.base) OVER (PARTITION BY mj.title) AS '25th', PERCENTILE_DISC(.5) WITHIN 
GROUP (ORDER BY e.base) OVER (PARTITION BY mj.title) AS '50th', 
PERCENTILE_DISC(.75) WITHIN GROUP (ORDER BY e.base) OVER (PARTITION BY mj.title) 
AS '75th', PERCENTILE_DISC(.9) WITHIN GROUP (ORDER BY e.base) OVER (PARTITION BY 
mj.title) AS '90th' FROM dbo.ps_employee e FULL OUTER JOIN dbo.ps_jobs j on
 e.title = j.job FULL OUTER JOIN dbo.ps_masterjobs mj 
 ON j.masterID = mj.ID;