JOINING 3 Tables Using LEFT OUTER JOIN

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nico3334
    New Member
    • Aug 2007
    • 28

    JOINING 3 Tables Using LEFT OUTER JOIN

    I currently have a query that Joins 2 Tables (Table1 and Table2) using
    LEFT OUTER JOIN. Here is an example of that query:

    SELECT a.supply,
    a.state,
    b.cost
    FROM Table1 a
    LEFT OUTER JOIN Table2 b
    ON a.supply = b.supply
    AND a.state = b.state
    GROUP BY a.supply, a.state, b.cost

    I would like to add additional data to this query by adding a 3rd Table. I made
    this separate query which Joins Table1 and Table3:

    SELECT a.supply,
    FROM Table1 a
    LEFT OUTER JOIN Table3 c
    ON a.supply = c.supply
    GROUP BY a.supply, c.number, c.number2

    My problem is that I don't want to join Tables 2 and 3 because Table3 does not
    have one of the columns that Table1 and 2 use to JOIN. So, I just want to JOIN
    Tables 1 & 3 and keep my 1st query which joins Tables 1 & 2. Is there a way to
    add my 2nd query to my 1st query without messing up the JOIN? If anyone could
    give me some coding examples, I'd greatly appreciate it. Thanks!
  • hariharanmca
    Top Contributor
    • Dec 2006
    • 1977

    #2
    You can Join Table2 and Table3 toe Table1.
    So table 1 have both Table 2,3 key to join then no need to join Table1 to 2 as view1 and table 1 to3 as View2.

    Comment

    • nico3334
      New Member
      • Aug 2007
      • 28

      #3
      Could someone help me with the actual SQL coding? Thanks.

      Comment

      • azimmer
        Recognized Expert New Member
        • Jul 2007
        • 200

        #4
        Originally posted by nico3334
        I currently have a query that Joins 2 Tables (Table1 and Table2) using
        LEFT OUTER JOIN. Here is an example of that query:

        SELECT a.supply,
        a.state,
        b.cost
        FROM Table1 a
        LEFT OUTER JOIN Table2 b
        ON a.supply = b.supply
        AND a.state = b.state
        GROUP BY a.supply, a.state, b.cost

        I would like to add additional data to this query by adding a 3rd Table. I made
        this separate query which Joins Table1 and Table3:

        SELECT a.supply,
        FROM Table1 a
        LEFT OUTER JOIN Table3 c
        ON a.supply = c.supply
        GROUP BY a.supply, c.number, c.number2

        My problem is that I don't want to join Tables 2 and 3 because Table3 does not
        have one of the columns that Table1 and 2 use to JOIN. So, I just want to JOIN
        Tables 1 & 3 and keep my 1st query which joins Tables 1 & 2. Is there a way to
        add my 2nd query to my 1st query without messing up the JOIN? If anyone could
        give me some coding examples, I'd greatly appreciate it. Thanks!
        Code:
        SELECT a.supply, 
               a.state, 
               b.cost
        FROM (Table1 a LEFT OUTER JOIN Table2 b ON a.supply = b.supply AND a.state = b.state) LEFT OUTER JOIN Table3 c ON a.supply = c.supply
        GROUP BY a.supply, a.state, b.cost, c.number, c.number2

        Comment

        Working...