2017-08-03 54 views
0

我正在嘗試爲每個主工作職位獲得基本工資的計數,最小值,最大值和一些百分位數(第10,第25,第50,第75,第90)。SQL Server:Min/Max/Percentiles

enter image description here

,我發現了以下錯誤:

Msg 8120, Level 16, State 1, Line 1
Column 'dbo.ps_employee.Base' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

+3

PERCENTILE_DISC它不是聚合函數,如COUNT/MIN/MAX/AVG(列)。此異常由此功能使用的列生成,但不用於分組:e.base。最簡單的解決方案可能是創建一個單獨的查詢來計算百分位數。 –

+0

確實刪除(ORDER BY e.base)更改業務邏輯? –

+0

不,我認爲ORDER BY是PERCENTILE_DISC函數所要求的嗎? – gattoun

回答

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;