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

  1. Path: sparky!uunet!cs.utexas.edu!uwm.edu!ogicse!das-news.harvard.edu!das!bochner
  2. From: bochner@das.harvard.edu (Harry Bochner)
  3. Newsgroups: comp.databases.informix
  4. Subject: Re: conditional select sum() based on field question
  5. Keywords: select sum
  6. Message-ID: <1992Aug13.180645.6534@das.harvard.edu>
  7. Date: 13 Aug 92 18:06:45 GMT
  8. Article-I.D.: das.1992Aug13.180645.6534
  9. References: <297@praeda.UUCP.UUCP>
  10. Sender: usenet@das.harvard.edu (Network News)
  11. Organization: Aiken Computation Lab, Harvard University
  12. Lines: 28
  13.  
  14. In article <297@praeda.UUCP.UUCP>, pete@praeda.UUCP.UUCP (Pete Frehner) writes:
  15. |>    select sum(amount) into total_credits
  16. |>        from orders where order_type = "CREDIT"
  17. |>    select sum(amount) into total_normal_orders 
  18. |>        from orders where order_type = "REG"
  19. |>    let net_amount = total_normal_orders - total_credits 
  20. |> Is there a way that this could be done in one select statment??
  21.  
  22. Paul Roberts has already posted a good solution for this particular case, where
  23. we want the difference of the two totals. I'd just like to contribute an idea for
  24. a more general solution, usable if you really need the two separate figures:
  25.  
  26. select order_type, sum(amount) from orders
  27. group by order_type
  28. into temp totals
  29.  
  30. select amount into total_credits from totals
  31. where order_type = "CREDIT"
  32.  
  33. select amount into total_normal_orders from totals
  34. where order_type = "REG"
  35.  
  36. This way the large order table only has to be scanned once. Selecting the
  37. individual totals should be fast, as long as the number of order_types is small.
  38.  
  39. -- 
  40. Harry Bochner
  41. bochner@das.harvard.edu
  42.