Need help with postgres function

I want to sum the margin and sales quantity of my items and group them by its currency. I have created this function:

CREATE OR REPLACE FUNCTION get_sales_overview(design_id_input varchar) 
RETURNS TABLE(
  total_margin numeric,
  total_sales numeric,
  margin_currency varchar
) 
AS $$ 
  BEGIN
    RETURN QUERY 
    SELECT 
      sum(margin(stored as numeric)) as total_margin,
      sum(sold_quantity (stored as int4)) as total_sales,
      sales_items.margin_currency (stored as varchar)
    FROM sales_items
    WHERE sales_items.design_id = design_id_input
    GROUP BY
      sales_items.margin_currency;
  END;
$$ language plpgsql;


when I call

select * from get_sales_overview('B08YB5SBYX');


I get an error :
Failed to run sql query: structure of query does not match function result type


Where am I returning the wrong type?
Was this page helpful?