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

  1. Path: sparky!uunet!gatech!darwin.sura.net!dtix!mimsy!nocusuhs!mgr!perez
  2. From: perez@mgr.hjf.org (Charles Perez)
  3. Newsgroups: comp.databases.informix
  4. Subject: Re: conditional select sum() based on field question
  5. Keywords: select sum
  6. Message-ID: <1992Aug14.190551.27767@nocusuhs.nnmc.navy.mil>
  7. Date: 14 Aug 92 19:05:51 GMT
  8. References: <297@praeda.UUCP.UUCP>
  9. Sender: news@nocusuhs.nnmc.navy.mil (Netnews Admin Account)
  10. Organization: HJF AIDS Research Project, Washington DC
  11. Lines: 63
  12. Nntp-Posting-Host: 131.158.26.52
  13.  
  14. In article <297@praeda.UUCP.UUCP> pete@praeda.UUCP.UUCP (Pete Frehner) writes:
  15. >
  16. >I have a question regarding selecting a sum from a table of amounts and
  17. >depending upon the type of record, either adding or subtracting the amount
  18. >from the sum.  We are using Informix 4.0 ISQL and R4GL.
  19. >
  20. >I could do this in two select statements and the difference would be the
  21. >net amount, but this requires that the data be read twice (once for each
  22. >select statment).  
  23. >
  24. >
  25. >   select sum(amount) into total_credits
  26. >       from orders where order_type = "CREDIT"
  27. >
  28. >   select sum(amount) into total_normal_orders 
  29. >       from orders where order_type = "REG"
  30. >
  31. >   let net_amount = total_normal_orders - total_credits 
  32. >
  33. >
  34. >Is there a way that this could be done in one select statment??  Two select
  35. >statments is no big deal for a small database, but this database contains
  36. >hundreds of thousands of orders.  If the data could be read only once it would
  37. >pick up performance quite a bit.
  38.  
  39. I would do it this way:
  40.  
  41.   DEFINE
  42.     rorders RECORD
  43.       type LIKE orders.order_type,
  44.       amount LIKE orders.amount
  45.     END RECORD
  46.  
  47.   DECLARE corders CURSOR FOR
  48.   SELECT order_type, sum(amount)
  49.   FROM orders
  50.   WHERE
  51.     (order_type = "CREDIT")
  52.     or (order_type = "REG")
  53.   GROUP BY order_type
  54.  
  55.   FOREACH corders INTO rorders.*
  56.     IF (rorders.order_type = "CREDIT") THEN
  57.       LET total_credits = rorders.amount
  58.     ELSE
  59.       LET total_normal_orders = rorders.amount
  60.     END IF
  61.   END FOREACH
  62.  
  63.   LET net_amount = total_normal_orders - total_credits
  64.  
  65. This gives you exactly one SQL query to contend with, and the cursor
  66. shouldn't slow you down. Make sure there is an index primarily on the
  67. order_type field, or it will still crawl!
  68.  
  69. Hope this helps.
  70.  
  71.  
  72. -- 
  73. ==========================================================================
  74. That which is necessary is never evil. If an evil seems necessary, look
  75. to your context; that's where its root lies.  - Charles Perez
  76. ==========================================================================
  77.