I need to make the sum of all the differences of the intervals that fulfill the condition of the where. There is only one, however, if there is no end_date on this line, I need to pick up the current schedule and do the calculation with the current time instead of end_date.
CREATE OR REPLACE FUNCTION total_paused_time (v_id Integer)
RETURNS Interval
as $$
DECLARE @aux integer
select sum(@aux),
case when end_date = null then @aux := now() - start_date
else then @aux := end_date - start_date
end
from called_pause where called_id = v_id and internal = false;
$$ LANGUAGE SQL;