use insert on conflict
This commit is contained in:
@ -36,9 +36,10 @@ create table pv_stats_t (
|
|||||||
id serial not null primary key,
|
id serial not null primary key,
|
||||||
"date" date not null,
|
"date" date not null,
|
||||||
dateType varchar(5) not null,
|
dateType varchar(5) not null,
|
||||||
first double precision not null,
|
first numeric(10,2) not null default 0,
|
||||||
total double precision default 0
|
total numeric(10,2) not null default 0
|
||||||
);
|
);
|
||||||
|
alter table pv_stats_t add constraint ddT_uk unique ("date", dateType);
|
||||||
|
|
||||||
grant insert, select, update on pv_stats_t to nodered;
|
grant insert, select, update on pv_stats_t to nodered;
|
||||||
grant select, update on pv_stats_t_id_seq to nodered;
|
grant select, update on pv_stats_t_id_seq to nodered;
|
||||||
@ -54,12 +55,9 @@ declare
|
|||||||
begin
|
begin
|
||||||
foreach v_dateType in array v_dateTypes
|
foreach v_dateType in array v_dateTypes
|
||||||
loop
|
loop
|
||||||
select id from pv_stats_t into v_stat_id where "date" = date_trunc(v_dateType, NEW.time::date) and dateType = v_dateType;
|
insert into pv_stats_t("date", dateType, first, total)
|
||||||
if not found then
|
values (date_trunc(v_dateType, NEW.time::date), v_dateType, NEW.exportEnergyActive, 0)
|
||||||
insert into pv_stats_t ("date", dateType, first) values (date_trunc(v_dateType, NEW.time::date), v_dateType, NEW.exportEnergyActive);
|
on conflict on constraint ddT_uk do update set total = NEW.exportEnergyActive - excluded.first;
|
||||||
else
|
|
||||||
update pv_stats_t set total = NEW.exportEnergyActive - first where id = v_stat_id;
|
|
||||||
end if;
|
|
||||||
end loop;
|
end loop;
|
||||||
|
|
||||||
return NEW;
|
return NEW;
|
||||||
@ -71,3 +69,9 @@ create trigger pv_stats_trig
|
|||||||
for each row
|
for each row
|
||||||
execute function pv_stats_func();
|
execute function pv_stats_func();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
insert into pv_stats_t("date", dateType, first, total) values ('2023-12-12', 'day', 12, 0)
|
||||||
|
on conflict on constraint ddT_uk do update set total = 15 - excluded.first;
|
||||||
|
|
||||||
|
;
|
||||||
|
@ -5,3 +5,6 @@ select time_bucket('1 day', time) as interval,
|
|||||||
group by interval
|
group by interval
|
||||||
order by interval;
|
order by interval;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
-- daily stats of current month
|
||||||
|
Reference in New Issue
Block a user