写错了,更改为:
 
   
table1  
name        sl  
----        ---  
aaa          3  
bbb          2  
ccc          5  
aaa          2  
fff          7  
bbb          1  
 
table2:  
name        sl  
-----      ----  
aaa          1  
fff          6  
aaa          1
 
怎样得出这样的结果:  
 
name        sl_1             sl_2       sl3  
-----      ----               ----     ----  
aaa            5              2          3  
bbb            3              0          3  
ccc            5              0          5  
fff            7              6          1  
 
 

解决方案 »

  1.   

    写错了,更改为:
     
    table1  
    name        sl  
    ----        ---  
    aaa          3  
    bbb          2  
    ccc          5  
    aaa          2  
    fff          7  
    bbb          1  
     
    table2:  
    name        sl  
    -----      ----  
    aaa          1  
    fff          6  
    aaa          1
     
    怎样得出这样的结果:  
     
    name        sl_1             sl_2       sl3  
    -----      ----               ----     ----  
    aaa            5              2          3  
    bbb            3              0          3  
    ccc            5              0          5  
    fff            7              6          1  
     
     
      

  2.   

    select name,s1_1,s1_2,(s1_1-s1_2) sl3 from 
    (select name,sum(s1) s1_1,(select sum(s1) from table2 where name=table1.name) sl_2 from table1 group by name)
      

  3.   

    to: beckhambobo(beckham) 
    我实际的两张表是这样的:
    A表数据:
    品牌     型号     规格     数量     单价      合计
    10001 10003 100010 3 11300 33900
    10001 10003 100010 1 11200 11200
    10007 10019 100042 1 1 1
    10007 10023 100046 11 23 253
    10001 10003 100018 2 10200 20400
    10001 10003 100027 4 11500 46000B表数据
    品牌     型号     规格     数量     单价      合计
    10001 10003 100010 1 12000 12000
    10001 10003 100010 1 12500 12500
    10001 10003 100010 1 12000 12000
    10007 10019 100042 1 3 3
    10007 10023 100046 2 30 60要得出的结果是:
    品牌     型号     规格     A数量    B数量   库存数量(A数量 - B数量)
    10001 10003 100010 4 3 1
    10001 10003 100018 2         0       2
      

  4.   

    select 品牌,型号,规格,s1_1,s1_2,(s1_1-s1_2) sl3 from 
    (select 品牌,型号,规格,sum(数量) s1_1,(select sum(数量) from table2 where 品牌=table1.品牌 and 型号=table1.型号 and 规格=table1.规格) sl_2 from table1 group by 品牌,型号,规格)
      

  5.   

    TO: beckhambobo(beckham)
    这样写报错:
    select  cpmc,ppmc,xhmc,s1_1,s1_2,(s1_1-s1_2) sl3 from 
    (select cpmc,ppmc,xhmc,sum(rksl) s1_1,(select sum(cksl) from v_kc_ckbb01 where 
           cpmc=v_kc_rkbb01.cpmc and ppmc=v_kc_rkbb01.ppmc and xhmc=v_kc_rkbb01.xhmc) sl_2 
      from v_kc_rkbb01 group by cpmc,ppmc,xhmc);执行后;连接到:
    Oracle8 Enterprise Edition Release 8.0.5.0.0 - Production
    PL/SQL Release 8.0.5.0.0 - ProductionSQL> select  cpmc,ppmc,xhmc,s1_1,s1_2,(s1_1-s1_2) sl3 from 
      2  (select cpmc,ppmc,xhmc,sum(rksl) s1_1,(select sum(cksl) from v_kc_ckbb01 where 
      3         cpmc=v_kc_rkbb01.cpmc and ppmc=v_kc_rkbb01.ppmc and xhmc=v_kc_rkbb01.xhmc) sl_2 
      4    from v_kc_rkbb01 group by cpmc,ppmc,xhmc);
    (select cpmc,ppmc,xhmc,sum(rksl) s1_1,(select sum(cksl) from v_kc_ckbb01 where
                                           *
    错误位于第2行:
    ORA-00936: 缺少表达式
      

  6.   

    select 品牌,型号,规格,s1_1,s1_2,(s1_1-s1_2) sl3 from 
    (select 品牌,型号,规格,sum(a.数量) s1_1,sum(b.数量) sl_2 from table1 a,table2 b where a.品牌=b.品牌(+) and a.型号=b.型号(+) and a.规格=b.规格(+) group by 品牌,型号,规格)
      

  7.   

    select  a.name,s1_1,nvl(s1_2,0) , (s1_1-nvl(s1_2,0)) s3
    from
    (select  name,sum(s1) s1_1
    from aaa
    group by name)  a,
    (select a1.name,sum(bbb.s1) s1_2  
    from bbb,(select distinct name  from aaa ) a1
    where a1.name=bbb.name(+)
    group by a1.name)  b
    where a.name=b.name