中位数计算不正确
Median worked out incorrectly
我已经尝试了所有可能的方法来计算特定列的中值,但一直得到与 Excel 不同的值。
我正在研究 SQL2008R2
这是我迄今为止尝试过的方法:
SELECT Entity,
BillPeriod,
Cost,
PatientDays,
0 AS Median
FROM GroupCTE
UNION
SELECT Entity,
BillPeriod,
Cost,
PatientDays,
0 AS Median
FROM RegionCTE
UNION
SELECT Entity,
BillPeriod,
Cost,
PatientDays,
0 AS Median
FROM InstitutionCTE
Union
SELECT 'Median' as Entity,
BillPeriod,
0 as Cost,
0 as PatientDays,
(((
(SELECT MAX(Cost) FROM
(SELECT TOP 50 PERCENT Cost FROM InstitutionCTE WHERE Cost <> 0 ORDER BY Cost ASC) AS BottomHalf)
+
(SELECT MIN(Cost) FROM
(SELECT TOP 50 PERCENT Cost FROM InstitutionCTE WHERE Cost <> 0 ORDER BY Cost DESC) AS TopHalf)
)) / 2) AS Median
我是不是做错了什么?
我试过了:Function to Calculate Median in Sql Server
谢谢
所以我设法解决了这个问题。
问题是,出于某种原因,进行中值计算给出的结果与 Excels 中值函数的结果不同。
我创建了一个 TEMP table 并导入了 UNION 的结果,然后使用我的计算更新了 Median 列:
INSERT INTO #Temp
SELECT Entity,
BillPeriod,
((Cost) / (PatientDays)) * 100 AS Cost,
PatientDays,
0 AS Median
FROM GroupCTE
UNION
SELECT Entity,
BillPeriod,
((Cost) / (PatientDays)) * 100 AS Cost,
PatientDays,
0 AS Median
FROM RegionCTE
UNION
SELECT Entity,
BillPeriod,
((Cost) / (PatientDays)) * 100 AS Cost,
PatientDays,
0 AS Median
FROM InstitutionCTE
UNION
SELECT 'Median' as Entity,
BillPeriod,
0 AS Cost,
0 AS PatientDays,
0 AS Median
FROM InstitutionCTE
GROUP BY Entity, BillPeriod, PatientDays
UPDATE #Temp
SET Median = ((
((SELECT MAX(Cost) FROM
(SELECT TOP 50 PERCENT Cost FROM #Temp WHERE Cost <> 0 ORDER BY Cost ASC) AS BottomHalf)
+
(SELECT MIN(Cost) FROM
(SELECT TOP 50 PERCENT Cost FROM #Temp WHERE Cost <> 0 ORDER BY Cost DESC) AS TopHalf)
)) / 2)
SELECT * FROM #Temp
DROP TABLE #Temp
我已经尝试了所有可能的方法来计算特定列的中值,但一直得到与 Excel 不同的值。
我正在研究 SQL2008R2
这是我迄今为止尝试过的方法:
SELECT Entity,
BillPeriod,
Cost,
PatientDays,
0 AS Median
FROM GroupCTE
UNION
SELECT Entity,
BillPeriod,
Cost,
PatientDays,
0 AS Median
FROM RegionCTE
UNION
SELECT Entity,
BillPeriod,
Cost,
PatientDays,
0 AS Median
FROM InstitutionCTE
Union
SELECT 'Median' as Entity,
BillPeriod,
0 as Cost,
0 as PatientDays,
(((
(SELECT MAX(Cost) FROM
(SELECT TOP 50 PERCENT Cost FROM InstitutionCTE WHERE Cost <> 0 ORDER BY Cost ASC) AS BottomHalf)
+
(SELECT MIN(Cost) FROM
(SELECT TOP 50 PERCENT Cost FROM InstitutionCTE WHERE Cost <> 0 ORDER BY Cost DESC) AS TopHalf)
)) / 2) AS Median
我是不是做错了什么?
我试过了:Function to Calculate Median in Sql Server
谢谢
所以我设法解决了这个问题。 问题是,出于某种原因,进行中值计算给出的结果与 Excels 中值函数的结果不同。
我创建了一个 TEMP table 并导入了 UNION 的结果,然后使用我的计算更新了 Median 列:
INSERT INTO #Temp
SELECT Entity,
BillPeriod,
((Cost) / (PatientDays)) * 100 AS Cost,
PatientDays,
0 AS Median
FROM GroupCTE
UNION
SELECT Entity,
BillPeriod,
((Cost) / (PatientDays)) * 100 AS Cost,
PatientDays,
0 AS Median
FROM RegionCTE
UNION
SELECT Entity,
BillPeriod,
((Cost) / (PatientDays)) * 100 AS Cost,
PatientDays,
0 AS Median
FROM InstitutionCTE
UNION
SELECT 'Median' as Entity,
BillPeriod,
0 AS Cost,
0 AS PatientDays,
0 AS Median
FROM InstitutionCTE
GROUP BY Entity, BillPeriod, PatientDays
UPDATE #Temp
SET Median = ((
((SELECT MAX(Cost) FROM
(SELECT TOP 50 PERCENT Cost FROM #Temp WHERE Cost <> 0 ORDER BY Cost ASC) AS BottomHalf)
+
(SELECT MIN(Cost) FROM
(SELECT TOP 50 PERCENT Cost FROM #Temp WHERE Cost <> 0 ORDER BY Cost DESC) AS TopHalf)
)) / 2)
SELECT * FROM #Temp
DROP TABLE #Temp