PostgreSQL-14-异常值处理
-- 查看异常值
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;