hey guys, sql noob here. need a function to sum a ...
# sql
m
hey guys, sql noob here. need a function to sum a column
amount
of all rows in a table with specific columns (account and tokenId) my try looks like
Copy code
create or replace function total_token_offers_by_account(tokenId varchar, account varchar) returns int8 as $$
  select count(amount) from "Offers" where owner = account and tokenId = tokenId
$$ language sql
when I call it via
Copy code
select total_token_offers_by_account("1", "0x...")
it always throws like
column "1" does not exist
. Well it should look for column
tokenId
and then search for the value
"1"
.
s
I've had this issue before, Postgres doesn't like when you name columns and parameters the same thing. Another thing to note is that its Postgres convention to use underscores in column names instead of camelCase.
tokenId
token_id
Copy code
sql
create or replace function total_token_offers_by_account(id varchar, account varchar) returns int8 as $$
  select count(amount) from "Offers" where owner = account and tokenId = id
$$ language sql
m
ah its a naming thing, alright I will try, thanks a lot!
s
yeah its replacing the column name with the value you passed in since the column name and variable name are the same
m
same problem with
Copy code
create or replace function total_token_offers_by_account(token_id varchar, account varchar) returns int8 as $$
  select count(amount) from "Offers" where owner = account and "tokenId" = token_id
$$ language sql
will try to change the column name too
s
Yeah I'd suggest changing the column name, there is a way to do it will allowing camelCase but I can't remember it right now
m
mhm I'd have to change a lot of columns.. 😄 shouldn't it work if I put the column in parenthesis?
s
yes that should work
What error are you getting now?
m
still same error
wtf the error was calling the function with double quotes
"
instead of single quotes
'
s
I don’t see any quotes inside of you select call
m
I called it like
Copy code
select total_token_offers_by_account("1", "0x...")
but it must be
Copy code
select total_token_offers_by_account('1', '0x...')