How to unify aggregating function with Postgres per different group by my_data table is like this (one row example):

 total_cost | total_exp | ind1 | ind2 | ind3 | flag
------------+-----------+------+------+------+------
 1000       | 2000      | A    | A1   | A2   | F


And I have a function like this:

CREATE OR REPLACE FUNCTION create_my_aggregation() RETURNS VOID AS $$
        DECLARE
            import_date date;
        BEGIN
            FOR import_date IN
            SELECT generate_series('2005-01-01'::date, '2015-04-01'::date, '1 month'::Interval)
            LOOP
                insert into my_timeseries

                       select
--                       ind1 as ind,
--                       ind2 as ind,
                       ind3 as ind,
                       import_date,

                       -- size

                       array_agg(total_cost) filter(where flag = 'F' and total_exp &gt; 0) as f_cost_a,
                       array_agg(total_cost) filter(where flag = 'S' and total_exp &gt; 50) as s_cost_a,
                       array_agg(total_cost) filter(where flag = 'D' and total_exp &gt; 100) as d_cost_a,
                       array_agg(total_cost) filter(where flag = 'T' and total_exp &gt; 20) as t_cost_a,

                       from my_data
                       where date_trunc('month', date_of_transfer) = date_trunc('month', import_date)
--                       group by ind1, import_date;
--                       group by ind2, import_date;
                       group by ind3, import_date;
            END LOOP;
        END
$$ LANGUAGE plpgsql;


The expected outcome is an array aggregation with the ind1,ind2,ind3 renamed to ind and the relative aggregations.

The problem with that functions is that I have to switch (and run 3 times) select ind1 as ind....group by ind1,import_date.

Is there a way to unify it in one go?
Use grouping sets:

select
  coalesce(ind1, ind2, ind3) as ind,
  ...
group by grouping sets (
  (ind1, import_date),
  (ind2, import_date),
  (ind3, import_date));


But its was introduced only in 9.5 version.