home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / database / sybase / 329 < prev    next >
Encoding:
Text File  |  1992-11-17  |  4.6 KB  |  98 lines

  1. Path: sparky!uunet!charon.amdahl.com!pacbell.com!ames!sun-barr!cs.utexas.edu!rutgers!news.columbia.edu!lamont!msolda
  2. From: msolda@lamont.ldgo.columbia.edu (M Solda)
  3. Newsgroups: comp.databases.sybase
  4. Subject: Re: multiple EXISTS subqueries under Sybase 4.8
  5. Keywords: EXISTS, 4.8, sun4, sunos 4.1.1
  6. Message-ID: <1992Nov16.184540.11698@lamont.ldgo.columbia.edu>
  7. Date: 16 Nov 92 18:45:40 GMT
  8. References: <1992Nov14.174847.29576@lamont.ldgo.columbia.edu> <1992Nov15.144258.18635@panix.com> <1992Nov15.225853.2269@lamont.ldgo.columbia.edu> <1e8fj7INN1sg@gazette.bcm.tmc.edu>
  9. Sender: news@lamont.ldgo.columbia.edu
  10. Reply-To: msolda@lamont.ldgo.columbia.edu
  11. Organization: Lamont-Doherty Geological Observatory
  12. Lines: 84
  13.  
  14. In article <1e8fj7INN1sg@gazette.bcm.tmc.edu>, mparsons@fleming.csc.bcm.tmc.edu (Mark Parsons) writes:
  15. > In article <1992Nov15.225853.2269@lamont.ldgo.columbia.edu>, msolda@lamont.ldgo.columbia.edu (M Solda) writes:
  16. > |> 
  17. > |> just to follow up, after posting my original article, i solved the problem by
  18. > |> using a UNION operator to join the results into to identical queries, except 
  19. > |> of course, for the EXISTS clauses.
  20. > |> 
  21. > |> i did try "transforming the OR into an AND", but as Dave points out, this would
  22. > |> not work in all cases and this was such a case.
  23. > |> 
  24. > |> anybody know if this is a reported bug?
  25. > |> 
  26. > Ummmmmmm, if you're still working on it, could you post the example
  27. > code?  I'm not quite following this.
  28.  
  29. okay, here it is:
  30.  
  31. /******************************************************************************/
  32. /*                                                                            */
  33. /*  SQL query to extract data about employees in the available titles         */
  34. /*                                                                            */
  35. /*  NOTE:  the EXISTS and NOT EXISTS query seem not to be able to be joined   */
  36. /*         in a single where and thus the first query finds those employees   */
  37. /*         that have only been employeed once, while the second query finds   */
  38. /*         those employees who have been employeed more than once.  the       */
  39. /*         results are UNIONED together into a single query result.           */
  40. /*                                                                            */
  41. /******************************************************************************/
  42.  
  43. /* for those who have been employeed only once */
  44.  
  45. select last_name, first_name, salary, title
  46. from employee_id i1, salary_history s1, employee_status e1
  47. where not exists (select *
  48.                   from employee_status
  49.                   where status_begin < '1/1/0'
  50.                      and ss_num = s1.ss_num)
  51.    and salary_scope_begin = (select max(salary_scope_begin)
  52.                              from salary_history
  53.                              where ss_num = s1.ss_num
  54.                                 and salary_scope_begin <= '1/1/0')
  55.    and employee_id.ss_num = salary_history.ss_num
  56.    and employee_id.ss_num = employee_status.ss_num
  57.    and status_begin is NULL
  58.    and title_effective_date = (select max(title_effective_date)
  59.                                from employee_status
  60.                                where title_effective_date <= '1/1/0'
  61.                                   and ss_num = s1.ss_num)
  62.  
  63. /* use the UNION operator to combine the two selects into a single result */
  64.  
  65. union
  66.  
  67. /* for those who have been employeed more than once */
  68.  
  69. select last_name, first_name, salary, title
  70. from employee_id i1, salary_history s1, employee_status e1
  71. where exists (select *
  72.               from salary_history
  73.               where ss_num = s1.ss_num
  74.                  and comment = 'Employment'
  75.                  and salary_scope_begin <= '1/1/0'
  76.                  and salary_scope_begin > (select max(status_begin)
  77.                                            from employee_status
  78.                                            where status_begin < '1/1/0'
  79.                                               and ss_num = s1.ss_num))
  80.    and salary_scope_begin = (select max(salary_scope_begin)
  81.                              from salary_history
  82.                              where ss_num = s1.ss_num
  83.                                 and salary_scope_begin <= '1/1/0')
  84.    and employee_id.ss_num = salary_history.ss_num
  85.    and employee_id.ss_num = employee_status.ss_num
  86.    and status_begin is NULL
  87.    and title_effective_date = (select max(title_effective_date)
  88.                                from employee_status
  89.                                where title_effective_date <= '1/1/0'
  90.                                   and ss_num = s1.ss_num)
  91. order by title, last_name
  92. compute avg(salary), count(title) by title
  93. compute avg(salary), count(title)
  94.  
  95. msolda
  96.