Ask a Question related to IBM DB2, Design and Development.

  1. #1

    Default sub select vs. join

    Can anyone help convert the following sub-select into a join of some
    kind that will return the results described below?

    SELECT A.RST
    FROM A
    WHERE A.PROC = 'AAAA'
    AND A.RST NOT IN
    (
    SELECT B.RST
    FROM B
    WHERE A. PROC = B.PROC
    AND B.USERID = 'PGM'
    )

    A.RST can be 'O', 'P', 'Q', or 'R'.
    B.RST is 'Q' or 'R'.
    Valid result is 'O' and 'P'. PROC and RST are the only 'joinable'
    columns in A and B tables

    Thanks
    blake Guest

  2. Similar Questions and Discussions

    1. Select, Join & field values - 2 tables
      I had originally posted this in alt.comp.databases.mysql with no replies (I thought I had posted it here, my bad). Hopefully one of you bright...
    2. Sub-Select vs. Join
      Hi All, I have two tables. One table holds a list of states names. The other table holds the statesID the user slected along with there...
    3. Union or Join or Nested Select - Can't Remember
      Its been along time since I have had to write tsql from the hip so any help would be greatly valued. I have a table that contains Country, State,...
    4. trying to update a table after making a join select query on two tables
      here is my problem distilled down I need to be able to change the title of a book associated with an author there are 2 tables "author" and "books"...
    5. SELECT from multiple tables (not join though)
      Hi all, I have another question, I hope it isn't too basic. ^.^ I want to do a select from multiple tables but not join them. What I am trying...
  3. #2

    Default Re: sub select vs. join

    On 30 Jul 2003, blake wrote:
    > Can anyone help convert the following sub-select into a join of some
    > kind that will return the results described below?
    >
    > SELECT A.RST
    > FROM A
    > WHERE A.PROC = 'AAAA'
    > AND A.RST NOT IN
    > (
    > SELECT B.RST
    > FROM B
    > WHERE A. PROC = B.PROC
    > AND B.USERID = 'PGM'
    > )
    >
    > A.RST can be 'O', 'P', 'Q', or 'R'.
    > B.RST is 'Q' or 'R'.
    > Valid result is 'O' and 'P'. PROC and RST are the only 'joinable'
    > columns in A and B tables

    If you have to join using the column RST then you have no choice
    but to do a correlated subselect.







    -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
    [url]http://www.newsfeeds.com[/url] - The #1 Newsgroup Service in the World!
    -----== Over 80,000 Newsgroups - 16 Different Servers! =-----
    Ian D. Bjorhovde Guest

  4. #3

    Default Re: sub select vs. join

    [email]blake_samples@labcorp.com[/email] (blake) wrote in message:
    > Can anyone help convert the following sub-select into a join of some
    > kind that will return the results described below?
    >
    > SELECT A.RST
    > FROM A
    > WHERE A.PROC = 'AAAA'
    > AND A.RST NOT IN
    > (
    > SELECT B.RST
    > FROM B
    > WHERE A. PROC = B.PROC
    > AND B.USERID = 'PGM'
    > )
    >
    > A.RST can be 'O', 'P', 'Q', or 'R'.
    > B.RST is 'Q' or 'R'.
    > Valid result is 'O' and 'P'. PROC and RST are the only 'joinable'
    > columns in A and B tables
    >
    Example1:
    I could't understand why you want to use join.
    Anyway here is an example, but only for my pleasure. This query must
    have performance problem, and not so straightforward.
    SELECT A.RST
    FROM A
    LEFT OUTER JOIN
    B
    ON A.PROC = B.PROC
    AND A.RST = B.RST
    AND B.USERID = 'PGM'
    WHERE A.PROC = 'AAAA'
    AND B.RST IS NULL
    ;

    Example2:
    You can use EXISTS predicate instead of IN predicate.
    SELECT A.RST
    FROM A
    WHERE A.PROC = 'AAAA'
    AND NOT EXISTS
    (
    SELECT *
    FROM B
    WHERE A.PROC = B.PROC
    AND A.RST = B.RST
    AND B.USERID = 'PGM'
    )
    ;
    Tokunaga T. Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139