我有一张桌子价格,如:
ID PurchasePriceCalc
0146301 0.002875161
00006L00 0.00396
00087G03 NULL
00001G04 0.0020004
00006S 0.003689818
01580h01 NULL
00082EE00 0.002462687
00038R05 0.002237565
01666R01 0.002666667
我想获取每个PurchasePriceCalc的中位数,然后用PurchasePriceCalc减去结果,为更好地解释该公式应为:(PurchasePriceCalc-Median(PurchasePriceCalc))。
我正在使用以下查询,但无法正常工作:
SELECT ID,PurchasePriceCalc, PurchasePriceCalc - PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY PurchasePriceCalc)
OVER (PARTITION BY ID) AS MediaCalc FROM Prices
任何帮助或帮助将不胜感激!
答案 0 :(得分:2)
我想问题出在OVER (PARTITION BY ID)
。如果ID为UNIQUE,则每个组仅包含一行,这就是为什么所有值都等于0 / NULL的原因。
您应该删除PARTITION BY
部分。
SELECT ID,PurchasePriceCalc,
PurchasePriceCalc - PERCENTILE_CONT(0.5)
WITHIN GROUP(ORDER BY PurchasePriceCalc) OVER () AS MediaCalc
FROM Prices;