CREATE or replace FUNCTION budget_function(num integer)
RETURNS TABLE(budget bigint, dub_date text)
LANGUAGE plpgsql
AS $function$
begin
return query
SELECT SUM(t1.budget) budget,
string_agg(TO_CHAR("bud_date",'YYYY-MM-DD'),'-') date
FROM (
SELECT *, ROW_NUMBER() OVER(ORDER BY "bud_date") rn
FROM budget_table
) t1
GROUP BY (rn - 1)/num
ORDER BY (rn - 1)/num;
end
$function$
;