现有张表,表结构及数据类似如下:
date   itemnumber  lotnumber   actiontype  receiptqty reversedqty  rerunqty
5/10     100010        x07      v              null      200        null
 
5/14     100010        x07      r             5000     null        null7/6      100010        x07      x             null      null        800
.
.
.
.
.
现在要:当itemnumber和lotnumber相同的情况下,用actiontype为r的receiptquantity减去actiontype为v的reversedquantity和
actiontype为x的rerunquantity,如上面的就是:5000-200-800;再取出actiontype为r的date,如上就是:5/14/2007 .同时取出itemnumber和lotnumber.请叫各位高人.谢谢!

解决方案 »

  1.   

    上面提问的receiptquantity/reversedquantity/rerunquantity都改成receiptqty/reversedqty/rerunqty.不好意思
      

  2.   

    select date, itemnumber, lotnumber,
    (select isnull(receiptqty, 0) from 表
     where itemnumber = t.itemnumber and lotnumber = t.lotnumber and actiontype = r) -
    (select isnull(reversedqty, 0) from 表
     where itemnumber = t.itemnumber and lotnumber = t.lotnumber and actiontype = v) -
    (select isnull(rerunqty, 0) from 表
     where itemnumber = t.itemnumber and lotnumber = t.lotnumber and actiontype = x)
    from 表 t
    where actiontype = r這樣?
      

  3.   

    试试这个select date,itemnumber,lotnumber,(select actiontype from a where actiontype=r)-(select actiontype from a where actiontype=v)-(select actiontype from a where actiontype=x) as 差
    from t a
    where t.itemnumber=a.itemnumber and t.lotnumber=a.lotnumber