home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / database / informix / 1718 < prev    next >
Encoding:
Internet Message Format  |  1992-08-13  |  1.8 KB

  1. Path: sparky!uunet!caen!sol.ctr.columbia.edu!emory!bnr.co.uk
  2. From: A.Heskett@bnr.co.uk (Tony Heskett)
  3. Newsgroups: comp.databases.informix
  4. Subject: Re: conditional select sum() based on field question
  5. Message-ID: <9305@emory.mathcs.emory.edu>
  6. Date: 13 Aug 92 19:48:08 GMT
  7. Sender: walt@mathcs.emory.edu
  8. Reply-To: A.Heskett@bnr.co.uk (Tony Heskett)
  9. Lines: 49
  10. X-Informix-List-ID: <list.1377>
  11.  
  12.  
  13. Pete Frehner writes:
  14.  
  15. > [ ... add or subtract according to record type, only scan table once ... ]
  16.  
  17. >    select sum(amount) into total_credits
  18. >        from orders where order_type = "CREDIT"
  19. >    select sum(amount) into total_normal_orders 
  20. >        from orders where order_type = "REG"
  21. >    let net_amount = total_normal_orders - total_credits 
  22.  
  23. Call me stupid (go on, I'm waiting  :-)  but why don't you just
  24. do this ?    Is a cursor *much* slower ?
  25.  
  26.       declare a_curs cursor for
  27.          select amount, order_type
  28.             from orders
  29.             where order_type = "CREDIT"
  30.                or order_type = "REG"
  31.     
  32.       fetch a_curs into amt, ord
  33.     
  34.       while status = 0
  35.      
  36.          if ord = "CREDIT"
  37.             net_amount = net_amount + amt
  38.          else
  39.             net_amount = net_amount - amt
  40.     
  41.          fetch a_curs into amt, ord
  42.     
  43.       end while
  44.     
  45.       close a_curs
  46.  
  47. Incidentally, does anyone else miss not having += -= and friends ?
  48. COBOL-fingers, here we come ...
  49.  
  50. > Pete Frehner                 Voice (519) 673-3350
  51. > Praeda Management Systems                Fax   (519) 667-1968
  52. > 371 Dufferin Ave.                        uucp: .....uunet!praeda!pfrehner
  53. > London, Ontario CANADA N6B 1Z5           
  54.  
  55. Cheers - Tony.
  56. _________________________________________________________________________
  57. Tony Heskett                 th@bnr.co.uk       Phone: (+44) 279 402637
  58. BNR, London Road, Harlow, Essex  CM17 9NA       Fax:   (+44) 279 451434
  59.