数据库为oracle
有个表分别是:table1,table2,table3,table4他们通过key1,key2,key3,key4连接起来table1               
key1     col1        
1 Name1       
2 Name2                         
3 Name3       
4 Name4                         
5 Name5           
 table2    
       
  key2   col2     
  1      10      
  3       8        
  5       20    table3
  key3    col3
  2        20
  2        30
  3        13
  4        10
  4        15
  4        15
  
table4
key4    col4
1        2
1        3
2        10
2        10
2        10
4        20
4        10
                         
得到的结果是:Result table1.Key1    table2.col2      table3.col3       table4.col4
1              10                0                 5
2              0                 50                30
3              8                 13                0
4              0                 40                30
5              20                0                 0        按着主键对table2,table3,table4汇总,然后与table1连接有哪位能帮助写一下,多谢了。

解决方案 »

  1.   

    select t1.key1,nvl(t2.col2,0),nvl(t3.col3,0),nvl(t4.col4,0)
    from table1 t1,
    (select key2,sum(col2) col2 from table2 group by key2)t2,
    (select key3,sum(col3) col3 from table3 group by key3)t3,
    (select key4,sum(col4) col4 from table4 group by key4)t4
    where t1.key1=t2.key2(+) and  t1.key1=t3.key3(+) and t1.key1=t4.key4(+)
      

  2.   

    select t1.Key1,t2.col2,t3.col3,t4.col4 from table1 t1,table2 t2,table3 t3 where t1.Key1=t2.Key2 and t1.Key1=t3.Key3 and t1.Key1=t4.Key4楼主试试看
      

  3.   


    这个应该不行。table1.Key1    table2.col2      table3.col3      table4.col4 
    1              10                0                5 
    2              0                50                30 
    3              8                13                0 
    4              0                40                30 
    5              20                0                0      首先肯定要外连接的其次table3.col3 是sum信息 group by key3的table4.col4 也是如此。1楼的是可以的。不过你最好把你的要求和表结构描述的清楚一些,估计1楼是完全根据你的结果来猜你的用意的。由此足可以看到1楼的大侠之丰富经验哟。