Hello please I need help. I'm getting an error whe...
# sql
o
Hello please I need help. I'm getting an error when trying to return a custom record from this query.
Copy code
sql
  select type, total 
  from aggregate_business_transaction('b579f1b2-4c20-4d54-a3aa-4728b2ca32da') 
        as (type varchar, total int8)
This is the content of my SQL function.
Copy code
sql
create or replace function aggregate_business_transaction(business_id uuid)
returns table(
  type varchar, 
  total int8
)
as $$
BEGIN
  RETURN QUERY SELECT
    type,
    SUM(amount_in_base) as total
  FROM transactions
  WHERE "business_id" = business_id
  GROUP BY type;
END;
$$ language plpgsql
g
What is your error? Try select * from function(..); also
o
column reference "type" is ambiguous
@User
So i changed that to this:
Copy code
sql

create or replace function aggregate_business_transaction(business_col_id uuid)
returns table(
  transaction_type varchar, 
  total int8
)
as $$
BEGIN
  RETURN QUERY SELECT
    type as transaction_type,
    SUM(amount_in_base) as total
  FROM transactions
  WHERE "business_id" = business_col_id
  GROUP BY transaction_type;
END;
$$ language plpgsql
Now! i'm getting this error @User :
structure of query does not match function result type
g
I'm not good enough with postgres to just look and see the issue. But it is complaining your query types are not matching the two return values. What is the type of amount_in_base? Is it int8? total will be what ever type amount_in_base is.
o
amount_in_base is
int8
transaction_type is
varchar
@User
g
So if it were me muddling thru it I would run just the select in sql editor and see if the result is what I expect. Records with two columns named transaction_type and total. Basically you are dealing with postgres so your world of info is far beyond just the supabase discord. There are 1/2 dozen or so SQL gurus who wander around this user channel otherwise.
o
I could show you the returned record tho.
Please see record
@User
Thanks all wise @User .
I've figured it out.
g
I would not expect total to be in quotes...
@User what was it?
o
I used
numeric
instead of
int8
or any other data type.
This one works:
Copy code
sql
create or replace function aggregate_business_transaction(business_col_id uuid)
returns table(
  transaction_type varchar, 
  total numeric
)
as $$
BEGIN
  RETURN QUERY SELECT
    type as transaction_type,
    SUM(amount_in_base) as total
  FROM transactions
  WHERE "business_id" = business_col_id
  GROUP BY transaction_type;
END;
$$ language plpgsql
g
yeah that is a bit different than int8.... carry on
o
Thanks @User