Bogo
Bogo

Reputation: 688

How to get the summ of all values

I have this piece of code PL/SQL code in a procedure

for rec in(select t.val, t.cat from table t where a=1)
Loop
v:=(rec.val*rec.cat)/2;
end Loop;

How I can get the sum of all 'v' values?

Upvotes: 0

Views: 79

Answers (2)

Adrian Nasui
Adrian Nasui

Reputation: 1095

This will create the sum in another variable, named tempSum

DECLARE
tempSum number (6);

tempSum := 0;

for rec in(select t.val, t.cat from table t where a=1)
Loop
v:=(rec.val*rec.cat)/2;
tempSum := tempSum + V;
end Loop;

Upvotes: 1

Sanders the Softwarer
Sanders the Softwarer

Reputation: 2496

Best way:

select sum(t.val * t.cat / 2) into l_val_cat_sum from table t where a = 1;

Upvotes: 0

Related Questions