I have this function
drop function uptimes(inicio date, fim date);
create or replace function uptimes(inicio date, fim date)
returns table (id integer, entrada timestamp with time zone,
saida timestamp with time zone, erro boolean, uptime interval) as
$$
begin
create temporary table localdata (id_local integer,
entrada_local timestamp with time zone,
saida_local timestamp with time zone, erro_local boolean) on commit drop ;
insert into localdata select * from uptime u
WHERE u.saida>=inicio and u.entrada<=fim order by u.entrada ;
alter table localdata add uptime_local interval;
update localdata set saida_local=fim where saida_local>fim;
update localdata set entrada=inicio where entrada<inicio;
update localdata set uptime_local=saida_local-entrada_local;
return query select * from localdata;
end;
$$
language plpgsql;
That I use to calculate some uptime from a internal system.
However, I receive as output:
uptimes
-----------------------------------------------------------------------------
(1537,"2015-10-12 07:04:15-03","2015-10-14 05:01:56-03",f,"1 day 21:57:41")
And what I need is to split the record into the fields as the output of this, like:
id | entrada | saida |erro |uptime
----+------------------------+------------------------+-----+------------------
1537|"2015-10-12 07:04:15-03"|"2015-10-14 05:01:56-03"|f |"1 day 21:57:41"
select * from uptimes(...);select uptimes ('2015-10-14','2015-11-15), for exempletableso you need to put it into thefromclause.