home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / bit / listserv / sasl / 4013 < prev    next >
Encoding:
Text File  |  1992-09-03  |  3.6 KB  |  96 lines

  1. Comments: Gated by NETNEWS@AUVM.AMERICAN.EDU
  2. Path: sparky!uunet!uvaarpa!darwin.sura.net!paladin.american.edu!auvm!COMPUSERVE.COM!76350.1604
  3. Message-ID: <920903144834_76350.1604_EHJ71-1@CompuServe.COM>
  4. Newsgroups: bit.listserv.sas-l
  5. Date:         Thu, 3 Sep 1992 10:48:34 EDT
  6. Reply-To:     Andy Norton <76350.1604@COMPUSERVE.COM>
  7. Sender:       "SAS(r) Discussion" <SAS-L@UGA.BITNET>
  8. From:         Andy Norton <76350.1604@COMPUSERVE.COM>
  9. Subject:      Re: Finding non-dups via SQL (multiple keys)
  10. Comments: To: SAS-L <SAS-L@AWIIMC12.IMC.UNIVIE.AC.AT>
  11. Lines: 83
  12.  
  13. ----------------------------------------------------------------------
  14. CONTENT:    Information
  15. SUMMARY:   Jim Clark's response to Ray Pass is correct for single
  16.            keys.  Here are solutions for multiple keys.
  17. REL/PLTF:  6.07.01/CMS, 6.04/PC-DOS
  18. E-ADDR:    76350.1604@compuserve.com
  19. NAME:      Andy Norton
  20. ADDRESS:   Trilogy Consulting, 5228 Lovers Lane, Kalamazoo MI 49002
  21. PHONE:     (616) 344-2191
  22. ----------------------------------------------------------------------
  23.  
  24. Ray Pass had asked for a method to identify CASENO's in FILE1 that do
  25. not occur in FILE2.
  26.  
  27. Jim Clark responded with
  28. > title 'Eval Courses NOT IN Class Courses';
  29. > select distinct e.ident, e.ins
  30. >    from work.check e, f.course c
  31. >    where e.ident not in
  32. >     (select c.ident
  33. >      from f.course c)
  34. >    order by e.ident;
  35.  
  36. The general approach is correct and sufficient for Ray's problem.  I
  37. think you could use simply
  38.   select    CHECK.*
  39.   from      WORK.CHECK
  40.   where     CHECK.IDENT not in
  41.             ( select IDENT
  42.               from   F.COURSE)
  43.   order by  IDENT;
  44. to get the same result.  In other words, it is not necessary to join E
  45. with C in order to reference table C in a subquery.
  46.  
  47. The main purpose of my posting, however, is to point out that this
  48. method falls apart when you need to match on two keys instead of one.
  49. Suppose the IDENT's are repeated within different colleges, and you
  50. need to do a combined analysis.  Now the observations are identified
  51. with {COLLEGE, IDENT}.  The IN operator only works for a single
  52. variable, so can't handle this situation.
  53.  
  54. This is a widespread weakness in the design of SQL:  programs that work
  55. with single columns often cannot be extended to handle multiple
  56. columns.  If you write the program to handle multiple keys, it will be
  57. able to handle the single-key situation.
  58.  
  59. For a general solution which works with multiple keys, use
  60.   select    *
  61.   from      WORK.CHECK
  62.   where     not exists
  63.             ( select *
  64.               from   F.COURSE
  65.               where  COLLEGE = CHECK.COLLEGE and
  66.                      IDENT   = CHECK.IDENT)
  67.   order by  COLLEGE, IDENT;
  68.  
  69.   This is fairly intelligible but can be inefficient with the current
  70. optimizer, because the subquery is independently evaluated for each set
  71. of {COLLEGE, IDENT} values.  Defining indexes on COLLEGE and IDENT (in
  72. the COURSE file) could help.
  73.  
  74. It may be possible for the optimizer to automatically convert
  75. subqueries to joins in the future.
  76.  
  77. This alternative may be more efficient (depending on factors such as
  78. how many distinct {COLLEGE, IDENT} sets there are).  I haven't tested
  79. enough to determine exactly what the trade-offs are:
  80.  
  81.   select    CHECK.*
  82.   from      WORK.CHECK,
  83.             ( select COLLEGE, IDENT
  84.               from   WORK.CHECK
  85.               except
  86.               select COLLEGE, IDENT
  87.               from   F.COURSE) as NONDUP
  88.   where     CHECK.COLLEGE = NONDUP.COLLEGE and
  89.             CHECK.IDENT   = NONDUP.IDENT
  90.   order by  COLLEGE, IDENT;
  91.  
  92. Unlike the earlier subquery solution, this program processes all of the
  93. different {COLLEGE, IDENT} pairs together.
  94.  
  95.                     Andy
  96.