1. 程式人生 > >PostgreSQL-14-異常值處理

PostgreSQL-14-異常值處理

where primary out eat bsp post HERE key mar

-- 查看異常值
CREATE TABLE outerdata(id int PRIMARY KEY,value numeric);
\COPY outerdata FROM ‘C:\Users\iHJX_Alienware\Desktop\異常值.csv‘ WITH CSV HEADER; -- 加載數據


SELECT AVG(value),STDDEV(value) FROM outerdata; -- 查看均值、標準差
ALTER TABLE outerdata ADD u numeric;
UPDATE outerdata SET u = (SELECT AVG(value) FROM outerdata); -- 添加列,計算均值

ALTER TABLE outerdata ADD std numeric;
UPDATE outerdata SET std = (SELECT STDDEV(value) FROM outerdata); -- 添加列,計算標準差
ALTER TABLE outerdata ADD sgm numeric;
UPDATE outerdata SET sgm = ABS(value - u); -- 添加列,計算value與均值差
SELECT * FROM outerdata WHERE sgm > 3*std; -- 篩選出異常值

-- 通過單個WHERE做篩選
SELECT * FROM outerdata

WHERE ABS(value - (SELECT AVG(value) FROM outerdata)) > (SELECT STDDEV(value) FROM outerdata)*3;


-- 刪除異常值
DELETE FROM outerdata
WHERE ABS(value - (SELECT AVG(value) FROM outerdata)) > (SELECT STDDEV(value) FROM outerdata)*3;

PostgreSQL-14-異常值處理