mam funkcję, która wpisuje dane to tabeli wyniki_godzinne:
create or replace function wypelnij_tabele() returns trigger as
$BODY$
declare
i TIME;
begin
IF (TG_OP = 'INSERT') THEN
for i in select distinct date_trunc('hour', time) from baza
LOOP
insert into wyniki_godzinne select trader_id , sum(net_pl), (select count(net_pl) where net_pl < 0), (select count(net_pl) where net_pl > 0), event_date, i from baza b
where time between i and (i + interval '59 minutes 59 seconds 999 millisecond')
and not exists (select 1 from wyniki_dzienne w
where w.trader_id = b.trader_id
and w.event_date = b.event_date)
group by trader_id, event_date;
end LOOP;
END IF;
RETURN NULL;
end;
$BODY$
language 'plpgsql';
chcę uzyskać sumę wyników wystąpienia wartości ujemnych oraz dodatnich w kolumnie net_pl:
(select count(net_pl) where net_pl < 0), (select count(net_pl) where net_pl > 0)
nie wiem czy w dobrą stronę kombinuję, prosiłbym o jakieś podpowiedzi?