home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / database / oracle / 2593 < prev    next >
Encoding:
Internet Message Format  |  1992-12-21  |  2.4 KB

  1. Path: sparky!uunet!dtix!darwin.sura.net!zaphod.mps.ohio-state.edu!cs.utexas.edu!bcm!aio!casivils
  2. From: casivils@lescsse.jsc.nasa.gov (craig sivils)
  3. Newsgroups: comp.databases.oracle
  4. Subject: Re: Explain THIS, please!
  5. Message-ID: <casivils.724894969@node_508ba>
  6. Date: 20 Dec 92 23:42:49 GMT
  7. References: <1gtd91INN7ln@ef2007.efhd.ford.com>
  8. Sender: news@aio.jsc.nasa.gov (USENET News System)
  9. Organization: Lockheed Engineering and Sciences
  10. Lines: 65
  11.  
  12. In <1gtd91INN7ln@ef2007.efhd.ford.com> wwm@ef5003.efhd.ford.com (Bill Meahan) writes:
  13.  
  14. >I'm tearing my hair out at the following:
  15.  
  16.  
  17. >SQL> l
  18. >  1  select '%' Shift,
  19. >  2   p.prefix||'-'||p.base_code||'-'||p.suffix||p.control_code PartNumber,
  20. >  3   s.scrap_code Reason,
  21. >  4   SUM(s.cost) TotalDollars
  22. >  5   FROM cimcmms.scrap_count_history s, acg.part p
  23. >  6   WHERE s.part_key=p.key
  24. >  7    AND s.work_center_code = '75021'
  25. >  8    AND s.production_date BETWEEN '1-Dec-92' AND '13-Dec-92'
  26. >  9    AND s.shift LIKE '%'
  27. > 10    AND s.part_key IN ( SELECT ss.part_key
  28. > 11       FROM cimcmms.scrap_count_history ss
  29. > 12       WHERE ss.work_center_code = '75021'
  30. > 13        AND ss.production_date BETWEEN '1-Dec-92' AND '13-Dec-92'
  31. > 14        AND ss.shift LIKE '%'
  32. > 15        AND ROWID <11
  33. > 16       GROUP BY ss.part_key
  34. > 17       ORDER BY SUM(ss.cost) desc )
  35. > 18   GROUP BY '%',
  36. > 19   p.prefix||'-'||p.base_code||'-'||p.suffix||p.control_code,
  37. > 20   s.scrap_code
  38. > 21   ORDER BY p.prefix||'-'||p.base_code||'-'||p.suffix||p.control_code,
  39. > 22*  SUM(s.cost) desc
  40.  
  41. >SQL> /
  42. >     ORDER BY SUM(ss.cost) desc )
  43. >     *
  44. >ERROR at line 17: 
  45. >ORA-00907: missing right parenthesis 
  46.  
  47. >SQL> spool off
  48.  
  49. >There certainly AREN'T any mismatched parentheses!
  50.  
  51. >Any ideas what is going on?
  52.  
  53. To answer your question: Take the order by clause out of the subquery.  
  54.  
  55. To answer your problem, (the reason you had the order by in, was to try to
  56.                          get the top 10 somethings) Try rewriting the in as
  57.                          a correlated Exists subquery with a nested subquery
  58.                          to check that it is in the top 10 items. 
  59.  
  60. Example.
  61.  
  62. Table animals
  63. ID   number,
  64. Kind char(30),
  65. Name char(30)
  66.  
  67. to select the id for the top 10 names within a kind, use the following query.
  68.  
  69. SELECT ID 
  70. FROM   ANIMALS
  71. WHERE  10 >= ( SELECT COUNT(*)
  72.                FROM   ANIMALS CNT
  73.                WHERE  CNT.KIND  = ANIMALS.KIND
  74.                AND    CNT.NAME <= ANIMALS.NAME )
  75.  
  76.                 Craig
  77.