Re: conditional select sum() based on field question
Posted in 1992
Pete Frehner writes:
> [ ... add or subtract according to record type, only scan table once ... ]
> select sum(amount) into total_credits
> from orders where order_type = "CREDIT"
>
> select sum(amount) into total_normal_orders
> from orders where order_type = "REG"
>
> let net_amount = total_normal_orders - total_credits
Call me stupid (go on, I'm waiting :-) but why don't you just
do this ? Is a cursor *much* slower ?
declare a_curs cursor for
select amount, order_type
from orders
where order_type = "CREDIT"
or order_type = "REG"
fetch a_curs into amt, ord
while status = 0
if ord = "CREDIT"
net_amount = net_amount + amt
else
net_amount = net_amount - amt
fetch a_curs into amt, ord
end while
close a_curs
Incidentally, does anyone else miss not having += -= and friends ?
COBOL-fingers, here we come ...
> Pete Frehner Voice (519) 673-3350
> Praeda Management Systems Fax (519) 667-1968
> 371 Dufferin Ave. uucp: .....uunet!praeda!pfrehner
> London, Ontario CANADA N6B 1Z5
Cheers - Tony.
_________________________________________________________________________
Tony Heskett th@bnr.co.uk Phone: (+44) 279 402637
BNR, London Road, Harlow, Essex CM17 9NA Fax: (+44) 279 451434