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

  1. Path: sparky!uunet!usc!cs.utexas.edu!sun-barr!west.West.Sun.COM!news2me.ebay.sun.com!exodus.Eng.Sun.COM!sun!amdcad!weitek!pyramid!infmx!news
  2. From: cortesi@informix.com (David Cortesi)
  3. Newsgroups: comp.databases.informix
  4. Subject: Re: conditional select sum() based on field question
  5. Message-ID: <1992Aug13.201848.25430@informix.com>
  6. Date: 13 Aug 92 20:18:48 GMT
  7. References: <1992Aug13.003128.8803@informix.com>
  8. Sender: news@informix.com (Usenet News)
  9. Reply-To: cortesi@informix.com
  10. Organization: Informix Software, Inc.
  11. Lines: 53
  12.  
  13. > In article <297@praeda.UUCP.UUCP> pete@praeda.UUCP.UUCP (Pete Frehner) asked:
  14. > >
  15. > >I have a question regarding selecting a sum from a table of amounts and
  16. > >depending upon the type of record, either adding or subtracting the amount
  17. > >from the sum...I could do this in two select statements...  
  18. > >   select sum(amount) into total_credits
  19. > >       from orders where order_type = "CREDIT"
  20. > >   select sum(amount) into total_normal_orders 
  21. > >       from orders where order_type = "REG"
  22. > >   let net_amount = total_normal_orders - total_credits 
  23. > >
  24. > >Is there a way that this could be done in one select statment?
  25.  
  26. In article <1992Aug13.003128.8803@informix.com> proberts@informix.com
  27. (Paul Roberts) supplied a very clever answer:
  28. > create a temp table called "fudge" and put exactly
  29. > two rows into it, like so:
  30. >      char(6)       smallint 
  31. >     order_type     f_factor
  32. >     ----------     --------
  33. >       REG             1
  34. >       CREDIT         -1
  35. > and then do this select:
  36. > select sum(orders.amount * fudge.f_factor)
  37. >   from orders, fudge
  38. >  where fudge.order_type = orders.order_type
  39.  
  40. This is exceedingly clever, but what strikes me is how it
  41. generalizes to the application of any kind of "weight" against
  42. any set of categorized "samples."
  43.  
  44. In this case the "samples" were money; there were just two 
  45. "categories," and the "weights" were +/- 1 -- but that is
  46. not inherent.  This is a general technique that can apply
  47. arbitrary weights based on an arbitrary number of categories.
  48.  
  49. For one other commercial example, let the weights be a
  50. series of numbers between 0 and 1 representing state
  51. sales tax percentages, and let the categories be names of
  52. states. Now you can sum the sales tax due on a column of
  53. retail prices with one select.  Or, multiply by 1+weight
  54. to sum the prices including tax. 
  55.  
  56. Or let the amounts be shipping weights; let the categories
  57. be delivery zones; let the weights be shipping charge
  58. per-pound multipliers.
  59.  
  60. And of course the "samples" and "weights" could actually be
  61. samples and weights in a statistical sense.
  62.