当两个查询针对用户独立运行时,我得到结果300和0但是在我的函数中我没有返回任何行。
我正在从SQL Server迁移到PostgresSQL,所以有些事情对我来说有些陌生!
create or replace function getsummary(userid int)
returns table (assetTotal numeric, liabilityTotal numeric)
as $$
BEGIN
SELECT sum(t.credit) - sum(t.debit)
into assetTotal
from transactions t
join user_institutes i on t.user_institute_id = i.id
where i.account_type in (1,3,4,5)
and i.user_id = userid;
select sum(t.credit) - sum(t.debit)
into liabilityTotal
from transactions t
join user_institutes i on t.user_institute_id = i.id
where i.account_type in (2,8,10)
and i.user_id = userid;
END
$$ language plpgsql;
select * from getsummary(1)
非常感谢
答案 0 :(得分:1)
没有深入查看您的查询,只是指出缺少部分。尝试:
create or replace function getsummary(userid int)
returns table (assetTotal numeric, liabilityTotal numeric)
as $$
DECLARE
_assetTotal numeric;
_liabilityTotal numeric;
BEGIN
SELECT sum(t.credit) - sum(t.debit)
into _assetTotal
from transactions t
join user_institutes i on t.user_institute_id = i.id
where i.account_type in (1,3,4,5)
and i.user_id = userid;
select sum(t.credit) - sum(t.debit)
into _liabilityTotal
from transactions t
join user_institutes i on t.user_institute_id = i.id
where i.account_type in (2,8,10)
and i.user_id = userid;
return query select _assetTotal, _liabilityTotal;
END
$$ language plpgsql;