li.的博客
|
英文单词:MEDIAN
WITH OrdersRN AS
(
SELECT EmployeeID, Value,
ROW_NUMBER() OVER(PARTITION BY EmployeeID ORDER BY Value) AS RowNum,
COUNT(*) OVER(PARTITION BY EmployeeID) AS Cnt
FROM dbo.VOrders
)
SELECT EmployeeID, AVG(Value) AS Median
FROM OrdersRN
WHERE RowNum IN((Cnt + 1) / 2, (Cnt + 2) / 2)
GROUP BY EmployeeID;
---------------------------------------------------------------------------
说明:以上语句经测试后基本能满足需求,但要注意数据不能有重复,有重复数据排序时会有错误或漏数的情况。