Skip to content

Instantly share code, notes, and snippets.

@pontusab
Created May 5, 2024 18:48
Show Gist options
  • Save pontusab/49ff33c54d87ec4da5eeb53f78011513 to your computer and use it in GitHub Desktop.
Save pontusab/49ff33c54d87ec4da5eeb53f78011513 to your computer and use it in GitHub Desktop.
create or replace function get_profit (
team_id uuid,
date_from date,
date_to date,
currency text
) returns table (
date timestamp with time zone,
value numeric
) language plpgsql as $$
begin
return query
select
date_trunc('month', month_series) as date,
coalesce(sum(amount), 0) as value
from
generate_series(
date_from::date,
date_to::date,
interval '1 month'
) as month_series
left join transactions as t on date_trunc('month', t.date) = date_trunc('month', month_series)
and t.team_id = get_profit.team_id
and t.category != 'transfer'
and t.status = 'posted'
and t.currency = get_profit.currency
group by
date_trunc('month', month_series)
order by
date_trunc('month', month_series);
end;
$$;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment