oracle sql查询使用现有查询加入表

时间:2012-06-20 03:27:55

标签: sql oracle

我有以下四个表格,其中包含以下结构

表A

  ColA1  ColA2  ColA3    ColA4  ColA5
-----------------------------------------
  AA     100      CC     DD       EE

表B

  ColB1  ColB2  ColB3    ColB4   ColB5
 -------------------------------------------
 AA      100     40452   A9       CDE

当这些two tables加入时如下:

 Select colA1,ColA2, ColA3, ColA4, ColB3,ColB4, ColB5
   from table A
        Left outer join 
            (select ColB3, ColB4, ColB5
              from table B
              where colB3 = (select max(colB3) from table B
            )
          on (colA1 = colB1 and ColA2 = col B2);

现在我必须使用table C

加入下一个table B

表C 结构

 ColD1   ColD2   ColD3
 --------------------------------  
  Desc1 A9   Executive
  Desc1 A7   Engineer

我有ColD2 and colB4之类的公共列来获取Col D3

如何加入existing query + join between表b和表c?

1 个答案:

答案 0 :(得分:0)

未经测试,但它会在下面给出

 SELECT colA1,ColA2, ColA3, ColA4, ColB3,ColB4, ColB5,ColD3
   FROM table A
        LEFT OUTER JOIN 
            (SELECT ColB3, ColB4, ColB5
              FROM table B
              WHERE colB3 = (SELECT MAX(colB3) FROM table B
            )
          ON (colA1 = colB1 AND ColA2 = col B2)
        LEFT OUTER JOIN TABLE C
          ON (colB4=colD2);