两个表的结构一样,表a,表b,表a中的数据如下:
id      ye       rq                ts
1     1200    2009-02-01           80
2     1500    2009-03-01           140
表b中的数据如下:
id      ye       rq                 ts
1       200    2009-02-01           80想要的结果如下:
id      ye       rq                ts
1     1000    2009-02-01           80
2     1500    2009-03-01           140解释:
表a,b中相同rq,ts的数据,ye进行相减,高手指教

解决方案 »

  1.   

    select b.id, a.ye - b.ye as ye, rq,ts from a,b where a.id = b.id;
      

  2.   


    条件改下,
    select b.id, a.ye - b.ye as ye, rq,ts from a,b where a.rq = b.rq and a.ts = b.ts;
      

  3.   

    不能用id相等,id这个只是主键,没有实际意义。就算有意义,你上面这个语句也不行。只是把id相同的检索到了,表a中id为2的数据显示不出来
      

  4.   

    select b.id, a.ye - b.ye as ye, rq,ts from a,b where a.rq = b.rq;
      

  5.   

    select a.id,a.ye-nvl(b.ye,0) ye,a.rq,a.ts from
      a,b where a.rq=b.rq(+) and a.ts=b.ts(+)
      

  6.   

    select a.id,a.ye-nvl(b.ye,0) ye,a.rq,a.ts from
      a,b where a.rq=b.rq(+) and a.ts=b.ts(+)
      

  7.   

    select a.id,a.ye-nvl(b.ye,0) ye,a.rq,a.ts from
      a,b where to_char(a.rq,'yyyy-mm-dd')=to_char(b.rq,'yyyy-mm-dd')(+) and a.ts=b.ts(+)
      

  8.   


    select a.id,a.ye-nvl(b.ye,0) ye,a.rq,a.ts from
      a,b where a.rq=b.rq(+) and a.ts=b.ts(+)ID YE RQ TS
    1 1000 a 80
    2 1500 b 140
      

  9.   


    可以了,关键在nvl(b.ye,0)这个,谢谢了