rollback upsert experiment, not working properly
This commit is contained in:
@ -49,14 +49,26 @@ create or replace function pv_stats_func ()
|
|||||||
language plpgsql
|
language plpgsql
|
||||||
as $$
|
as $$
|
||||||
declare
|
declare
|
||||||
|
v_stat_id pv_stats_t.id%TYPE;
|
||||||
v_dateTypes varchar[] := array['day', 'month', 'year'];
|
v_dateTypes varchar[] := array['day', 'month', 'year'];
|
||||||
v_dateType varchar;
|
v_dateType varchar;
|
||||||
begin
|
begin
|
||||||
foreach v_dateType in array v_dateTypes
|
foreach v_dateType in array v_dateTypes
|
||||||
loop
|
loop
|
||||||
insert into pv_stats_t("date", dateType, first, total)
|
select id
|
||||||
values (date_trunc(v_dateType, NEW.time::date), v_dateType, NEW.exportEnergyActive, 0)
|
from pv_stats_t
|
||||||
on conflict on constraint ddT_uk do update set total = NEW.exportEnergyActive - excluded.first;
|
into v_stat_id
|
||||||
|
where "date" = date_trunc(v_dateType, NEW.time::date) and
|
||||||
|
dateType = v_dateType;
|
||||||
|
if not found then
|
||||||
|
insert into pv_stats_t ("date", dateType, first)
|
||||||
|
values (date_trunc(v_dateType, NEW.time::date), v_dateType, NEW.exportEnergyActive);
|
||||||
|
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;
|
||||||
@ -70,7 +82,7 @@ create trigger pv_stats_trig
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
insert into pv_stats_t("date", dateType, first, total) values ('2023-12-12', 'day', 12, 0)
|
insert into pv_stats_t("date", dateType, first, total) values (date_trunc('month', now()), 'month', 0.01, 0)
|
||||||
on conflict on constraint ddT_uk do update set total = 15 - excluded.first;
|
on conflict on constraint ddT_uk do update set total = 3.26 - excluded.first;
|
||||||
|
|
||||||
;
|
;
|
||||||
|
Reference in New Issue
Block a user