Oracle 9i环境下,客户端是XP系统,现有2表:
表1字段:ID,AA,BB
表2字段:ID,NO
表1和表2通过ID关联,现2表中数据如下:表1:ID    AA      BB                    表2:ID     NO     
     1   中国     汽车                       1     12
     1   中国     飞机                       1      7
     1   法国     轮船                       2      9
     2   美国     汽车                       2      5
     2   美国     自行车                     2      13现在想实现如下输出结果:    ID    AA      BB       sum(NO)     
     1   中国     汽车       19     
     1   中国     飞机       19 
     1   法国     轮船       19  
     2   美国     汽车       27 
     2   美国     自行车     27请问具体的SQL语句该怎么写??越简单越好!!  

解决方案 »

  1.   


    select a.*,sum(NO)over(partition by 1 order by no) from a,b where a.id=b.id(+)
      

  2.   

    select a.id,a.aa,a.bb,sum(B.no) 
      from a,b 
     where a.id=b.id(+)
    group by a.id,a.aa,a.bb
    ;
      

  3.   

    或者用
    select a.id,a.aa,a.bb,b1.s
    from a,
      (select id,sum(no)s group by id)b1
    where a.id=b.id
      

  4.   


    select a.id,a.aa,a.bb,b.sumno 
      from a, 
           (select id,sum(no)sumno group by id) b
     where a.id=b.id
      

  5.   

    select a.id,a.aa,a.bb,b1.s
    from a,
      (select id,sum(no)s group by id)b1
    where a.id=b.id
      

  6.   

    粗心了.select a.id,a.aa,a.bb,b1.s 
    from a, 
      (select id,sum(no)s from b group by id)b1 
    where a.id=b1.id
      

  7.   

    感谢各位的辛苦工作,个人认为“sbaz”的答案为最佳答案,select a.id,a.aa,a.bb,sum(B.no) 
      from a,b 
    where a.id=b.id(+) 
    group by a.id,a.aa,a.bb 没有嵌套,没有别名,没有函数,纯粹的select语句,衷心感谢!