发布网友 发布时间:2022-04-08 09:26
共2个回答
懂视网 时间:2022-04-08 13:47
PostgreSQL计算平均值的sql类似如下:
with s as (select cast(load_15 as float) from asset_serverstatus where sid_id=10 order by id desc limit 10) select avg(load_15) from s
CREATE OR REPLACE FUNCTION fn_status_loadavg_insert() RETURNS trigger AS $BODY$ BEGIN update asset_serverstatus set load_avg=( with s as (select cast(load_15 as float) from asset_serverstatus where sid_id=new.sid_id order by id desc limit 10) select avg(load_15) from s ) where id=new.id; return new; END; $BODY$ LANGUAGE plpgsql VOLATILE COST 100;
用pgsql写了个触发器的函数,基本思路其实就是更新。
CREATE TRIGGER trg_status_loadavg_insert AFTER INSERT ON asset_serverstatus FOR EACH ROW EXECUTE PROCEDURE fn_status_loadavg_insert();
这样PostgreSQL数据库中根据某条件,取某字段的平均值,然后插入到另外一个字段的实现就完成了。
记录下。
热心网友 时间:2022-04-08 10:55
SELECT