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

  1. Path: sparky!uunet!cs.utexas.edu!sun-barr!news2me.ebay.sun.com!exodus.Eng.Sun.COM!sun!amdcad!weitek!pyramid!infmx!aland
  2. From: aland@informix.com (Colonel Panic)
  3. Newsgroups: comp.databases.informix
  4. Subject: Re: conditional select sum() based on field question
  5. Keywords: select sum
  6. Message-ID: <1992Aug15.030344.15687@informix.com>
  7. Date: 15 Aug 92 03:03:44 GMT
  8. References: <297@praeda.UUCP.UUCP>
  9. Sender: news@informix.com (Usenet News)
  10. Organization: The Helen Keller Bondurant High Performance Driving School for the Blind
  11. Lines: 43
  12.  
  13. In article <297@praeda.UUCP.UUCP> pete@praeda.UUCP.UUCP (Pete Frehner) writes:
  14. >I could do this in two select statements and the difference would be the
  15. >net amount, but this requires that the data be read twice (once for each
  16. >select statment).  
  17. >
  18. >   select sum(amount) into total_credits
  19. >       from orders where order_type = "CREDIT"
  20. >
  21. >   select sum(amount) into total_normal_orders 
  22. >       from orders where order_type = "REG"
  23. >   let net_amount = total_normal_orders - total_credits 
  24. >
  25. >Is there a way that this could be done in one select statment??  Two select
  26. >statments is no big deal for a small database, but this database contains
  27. >hundreds of thousands of orders.  If the data could be read only once it would
  28. >pick up performance quite a bit.
  29.  
  30. This does the *important* work in one select:
  31.  
  32. select order_type, sum(amount) sum_amount from orders
  33.   where order_type in ("CREDIT", "REG")
  34.   group by order_type
  35.   into temp tempsum
  36.  
  37. The queries to populate the variables are really quick, since they are dealing
  38. with a two-row summarized temp table at this point:
  39.  
  40. select sum_amount into total_credits from tempsum where order_type = "CREDIT"
  41.  
  42. select sum_amount into total_normal_orders from tempsum where order_type="REG"
  43.  
  44. let net_amount = total_normal_orders - total_credits 
  45.  
  46. >Pete Frehner                 Voice (519) 673-3350
  47. >Praeda Management Systems                Fax   (519) 667-1968
  48. >371 Dufferin Ave.                        uucp: .....uunet!praeda!pfrehner
  49. >London, Ontario CANADA N6B 1Z5           
  50.  
  51.  
  52. --
  53. Alan Denney      aland@informix.com      {pyramid|uunet}!infmx!aland
  54.  
  55.   Smith and Wesson: the Ultimate Point-and-Click User Interface.
  56.