我有A,B两个表如下A表:                                B表:
part   qty   date                   part     sqty     date
001    10    070601                 001       6       070602
001    14    070602                 001       8       070605
001    8     070605                 002       10      070602
现在我要把以上两表关连后筛选了如下的数据
part  qty    date
001   10     070601         如果a.part=b.part 但a.date没有等于b.date的
                            值时qty的值就为a.qty
001   20     070602         如果a.part=b.part 并且a.date=b.date
001   16     070605         这时qty的值为a.qty+b.sqty
请问这样该如何写SQL语句

解决方案 »

  1.   

    select case when a.date <> b.date then a.qty else nvl(a.qty,0) + nvl(b.qty,0) as qty from ...where...
      

  2.   

    用楼上的,case when 就可以满足你的要求了
      

  3.   

    where 后面的条件是a.part=b.part and a.date=b.date吗???
      

  4.   

    select q.part_no_1 ,case when q.date_applied<>p.date_applied then q.qtyout else (nvl(q.qtyout,0)+nvl(p.qtyscr,0)) as qtyout,0 qtyin,q.date_applied from  
                                   ////在AS 这位置 提示缺少关键字          
    (select PART_NO PART_NO_1,sum(QUANTITY) QTYOUT,DATE_APPLIED from INVENTORY_TRANSACTION_HIST2 
     where (LOCATION_NO='CYLBSJJ' or LOCATION_NO='CYLKSJJ' or (TRANSACTION_CODE='INVSCRAP' and (LOCATION_NO='DHBS' OR LOCATION_NO='DHKS'))) AND DIRECTION='-' and part_no like 'A22%'
     and (to_char(DATE_APPLIED,'yyyy-mm-dd') > '2007-06-28' and to_char(DATE_APPLIED,'yyyy-mm-dd') <='2007-07-10') and TRANSACTION_CODE in ('INVM-ISS','INVSCRAP') 
     GROUP BY PART_NO,DATE_APPLIED) q,
     (select i.part_no,sum(t.qty_scrapped) qtyscr,i.arrival_date date_applied from purchase_receipt_new i,purchase_receipt_scrap t 
     where i.part_no like 'A22%' and (to_char(i.arrival_date,'yyyy-mm-dd') > '2007-06-28' and to_char(i.arrival_date,'yyyy-mm-dd') <= '2007-07-10') 
     and i.order_no=t.order_no and i.line_no=t.line_no and i.release_no=t.release_no and i.receipt_no=t.receipt_no GROUP BY i.PART_NO,i.arrival_date) p 
     where q.part_no_1=p.part_no
    请问哪里错了???
      

  5.   

    select temp.PART4 ,sum(temp.QTY4), date4 
    from
    (select temp4.PART part4, temp4.QTY qty4, temp4.DATE1 date4 
     from temp4 
    union  allselect temp5.PART part5, temp5.sQTY qty5, temp5.DATE1 date5
     from temp5)  tempgroup by part4, date4
      

  6.   

    没有什么,其实就是把两个表 组合union到一起,然后分组
      

  7.   

    没有什么,其实就是把两个表 组合union到一起,然后分组求和
    select TableTemp.PART4 ,sum(TableTemp.QTY4), date4 
    from
    (select temp4.PART part4, temp4.QTY qty4, temp4.DATE1 date4  from Table1 
    union  all
    select temp5.PART part4, temp5.SQTY qty4, temp5.DATE1 date4 from Table2
    )  TableTemp
    group by part4, date4
    这样能清楚点?
      

  8.   

    LighBlade(菜鸟加加油!!!) 
    正确
      

  9.   

    LighBlade(菜鸟加加油!!!) 的方法还是没满足a.part=b.part这个条件
      

  10.   

    To tandy_cs_201() ( ) 信誉:99  :
       还是没满足a.part=b.part这个条件?
    你如果真的发现了group by 如果不能满足分组
    那么你可以宣称你发现了无与伦比的BUG, 恭喜你!