Taborders  其中OrderCode是唯一的 
----------------------------------- 
id ¦OrderCode   ¦Prices 
1 ¦[email protected]    ¦1000 
2 ¦[email protected]  ¦800 
------------------------------------ 
taborder_clild 其中的每一个O_code 都并不是唯一,但是和TABLE1中一一对应 
------------------------------------ 
id ¦O_code      ¦gongji 
1 ¦[email protected]    ¦100 
2 ¦[email protected]  ¦50 
3 ¦[email protected]    ¦200 
------------------------------------ tabsend_child  其中ordercode(注意是小写)是唯一的,与table1中的Taborders 对应,但是price不一样 
-------------------------------- 
id ¦ordercode   ¦yunfei
1 ¦[email protected]    ¦500 
2 ¦[email protected]  ¦1000 
---------------------------------- 
问题是这样的: 从Taborders中读取OrderCode和Prices,让Taborders中的Prices减去taborder_clild中对应的O_code的gongji相加的和,然后与tabsend_child中对应的ordercode的yunfei进行比较,,小于tabsend_child中的显示在dbgrid中

解决方案 »

  1.   


    select a.ordercode,a.Prices from (select a.ordercode,a.Prices,b.Prices-a.g as p from (select ordercode,sum(gongji) g from taborder_child group by ordercode) a,Taborders b 
    where a.ordercode=b.ordercode) a,tabsend_child b
    where a.ordercode=b.ordercode and a.p<b.yunfei
      

  2.   


    select d.*
      from tabsend_child c,
           (select a.OrderCode, nvl((a.Prices - b.gongji), 0) Prices
              from Taborders a,
                   (select distinct O_code, sum(gongji) gongji
                      from taborder_clild
                     group by O_code) b
             where a.OrderCode = b.O_code) d
     where c.ordercode = d.OrderCode
       and d.Prices < c.yunfei
      

  3.   

    select c.ordercode,c.p from 
    (select a.code as ordercode,b.Prices-a.g as p from 
    (select O_code as code,sum(gongji) as g from taborder_clild group by O_code) a,Taborders b 
    where a.code=b.ordercode) c,tabsend_child d
    where c.ordercode=d.ordercode and c.p<d.yunfei结果:
    ordercode  p
    [email protected] 750