我需要計算查詢中幾個字段的中位數,並將其按列中的一個進行分組。有沒有一種方法可以在SQL Server 2008 R2中輕鬆計算中值?我在計算2008 R2時遇到了麻煩。在SQL Server 2008 R2中計算中位數
Table structure:
PatientName (need to calculate count group by PatientType)
PatientType (should be used to group the query by),
minutes1,
minutes2,
minutes3,
minutes4,
minutes5
End Result:
PatientCount (Group by PatientType),
Median For minutes1 (Group by PatientType),
Median For minutes2 (Group by PatientType),
Median For minutes3 (Group by PatientType),
Median For minutes4 (Group by PatientType),
Median For minutes5 (Group by PatientType)
很容易,沒有。但這是可以完成的。您可以使用PERCENTILE_DISC,但直到2012年纔可以使用。即便如此,我也不確定這是否是真正的中位數,還是使用偶數集中最接近的較小值。例如,給定值1,3,5,7的中位數是4,但是3會足夠接近你嗎? –
在過去,我使用了PERCENTILE_DISC或PERCENTILE_CONT,但它們僅在2012年或以上時纔有用。另外,我需要一個真正的中位數。 – NonProgrammer