【发布时间】:2018-07-02 16:20:27
【问题描述】:
我有这个问题
;WITH cte AS
(
SELECT
*,
DATEPART(WEEKDAY, Dt) AS WeekDay,
PERCENTILE_CONT(0.75) WITHIN GROUP (ORDER BY SaleCount) OVER (PARTITION BY CustomerType, [CustomerName], ItemRelation, DocumentNum, DocumentYear) AS PERCENTILE,
AVG(SaleCount) OVER (PARTITION BY CustomerType, [CustomerName], ItemRelation, DocumentNum, DocumentYear, DATEPART(WEEKDAY, Dt), IsPromo) AS AVG_WeekDay
FROM
promo_data_copy
)
UPDATE a
SET SaleCount = cte.AVG_WeekDay
FROM CTE
JOIN promo_data_copy a ON a.Dt = cte.dt
AND a.ItemRelation = cte.ItemRelation
AND a.CustomerName = cte.CustomerName
AND a.DocumentNum = cte.DocumentNum
AND a.DocumentYear = cte.DocumentYear
AND a.CustomerType = cte.CustomerType
AND a.ispromo = cte.ispromo
WHERE
CTE.PERCENTILE < CTE.SaleCount
AND DATEPART(WEEKDAY, CTE.Dt) < 7
AND CTE.ispromo = 0 ;
这里有字符串
avg(SaleCount) over (Partition by CustomerType, [CustomerName], ItemRelation, DocumentNum, DocumentYear,datePart(WEEKDAY,Dt), IsPromo) as AVG_WeekDay
From promo_data_copy)
如何计算中位数而不是 avg()?此查询用平均值替换异常值。
我需要用中位数替换它,所以上面的字符串必须代替avg()。
包含中位数(但在 T-SQL 中没有像 Excel 中那样的中位数函数)
有人可以帮忙吗?
【问题讨论】:
-
@joe ,不,它不是重复的,请查看我的帖子,我编辑了它
标签: sql sql-server sql-server-2012 ssms