Muezz
04/08/2022, 8:09 PMdart
create or replace function calculate_current_balance(acc_name text)
returns numeric as $$
declare
debit_total numeric;
credit_total numeric;
begin
select SUM(amount)
into debit_total
from transactions
where transactions."debitAccount" = acc_name;
select SUM(amount)
into credit_total
from transactions
where transactions."creditAccount" = acc_name;
if debit_total=null
then
debit_total=0;
end if;
if credit_total=null
then
credit_total=0;
end if;
return credit_total-debit_total;
end;
$$ language plpgsql
When there is no row containing the acc_name
in any one of the two queries, it returns null
. To get rid of that, I added these if statements but I am still getting null
. Any idea how I can fix this?garyaustin
04/08/2022, 8:41 PMMuezz
04/08/2022, 8:44 PMMuezz
04/08/2022, 8:44 PM=
with is
and that gives the correct output.Muezz
04/08/2022, 8:44 PM