You may want to try this code to get count , sum or total.
SELECT COUNT(*) INTO :#VAR
WHERE CLAUSE ;
I've been unable to get this to work - thought it was maybe because I'm on 4.6.
I was putting the code in a global logic - seemed an ideal place for it. Turns out it doesn't work there.
Exactly the same code in a funtion works fine.