I created a function to generate the previous month's sales report based on the date. The function works fine, the only problem is the column name is fixed. I would like the column name to be changed dynamically based on the parameter run_date.
For example, if the run_date is '12-09-2021', the column name should be Nov2021Netsales
create or replace FUNCTION public.salesreport(run_date date)
returns TABLE (region numeric, dist numeric, repid character varying, Aug2021Netsales decimal)
AS
$$
select region,dist,repid,sales as Aug2021Netsales
from billing
where record_created_date between (date_trunc('month', run_date) - INTERVAL '1 months')::date and (date_trunc('month', run_date )::date - 1)
group by region,dist,repid,Aug2021Netsales
$$
LANGUAGE sql;
Thank you.