--测试数据
create table OrderDetail(orderID varchar(20),ItemNo varchar(20),Quantity varchar(20))
insert OrderDetail select '1','2','100'
union  all         select '2','3','200'create table Shipping(ID int,OrderID varchar(20),ItemNo varchar(20),ShippingQuantity varchar(20))
insert Shipping select '1','1','2','20'
union  all      select '2','1','2','30'
union  all      select '3','1','2','10'
union  all      select '4','2','3','50'
union  all      select '5','2','3','20'
go--查询统计
select a.OrderID,a.ItemNO,a.Quantity
,剩余数量=cast(a.Quantity as int)-isnull(b.ShippingQuantity,0)
from OrderDetail a
left join(
select OrderID,ItemNO,ShippingQuantity=sum(cast(ShippingQuantity as int))
from Shipping
group by OrderID,ItemNO
)b on a.OrderID=b.OrderID and a.ItemNO=b.ItemNO
go--删除测试
drop table OrderDetail,Shipping/*--结果OrderID              ItemNO               Quantity             剩余数量        
-------------------- -------------------- -------------------- ----------- 
1                    2                    100                  40
2                    3                    200                  130(所影响的行数为 2 行)
--*/

解决方案 »

  1.   

    楼上的很对
    isnull(b.ShippingQuantity,0)
    中的b.ShippingQuantity最好也能转换成数字
    当然不转换的话sql server会自动转
      

  2.   

    看看这句
    ShippingQuantity=sum(cast(ShippingQuantity as int))
    ShippingQuantity已经转为Int了。
      

  3.   

    --测试数据
    create table OrderDetail(orderID varchar(20),ItemNo varchar(20),Quantity varchar(20))
    insert OrderDetail select '1','2','100'
    union  all         select '2','3','200'create table Shipping(ID int,OrderID varchar(20),ItemNo varchar(20),ShippingQuantity varchar(20))
    insert Shipping select '1','1','2','20'
    union  all      select '2','1','2','30'
    union  all      select '3','1','2','10'
    union  all      select '4','2','3','50'
    union  all      select '5','2','3','20'
    go--查询统计
     select orderID=max(orderID),ItemNo,Quantity=sum(case when Qry>0 then Qry else 0 end),剩余数量=sum(Qry)
     from(
     select orderID,ItemNo,Qry=cast(Quantity as integer) from OrderDetail 
    union
     select null,ItemNo,-cast(ShippingQuantity as integer) from Shipping )a
    group by ItemNo
    go--删除测试
    drop table OrderDetail,Shipping/*--结果OrderID              ItemNO               Quantity             剩余数量        
    -------------------- -------------------- -------------------- ----------- 
    1                    2                    100                  40
    2                    3                    200                  130(所影响的行数为 2 行)
    --*/
      

  4.   

    可以简化一点
    --建立测试环境
    create table OrderDetail(orderID varchar(20),ItemNo varchar(20),Quantity varchar(20))
    insert OrderDetail select '1','2','100'
    union  all         select '2','3','200'create table Shipping(ID int,OrderID varchar(20),ItemNo varchar(20),ShippingQuantity varchar(20))
    insert Shipping select '1','1','2','20'
    union  all      select '2','1','2','30'
    union  all      select '3','1','2','10'
    union  all      select '4','2','3','50'
    union  all      select '5','2','3','20'
    go
    --测试
    Select OrderID,ItemNo,Quantity,
    剩余数量=(Quantity-(Select IsNull(SUM(Convert(Int,ShippingQuantity)),0) from Shipping  Where OrderID=A.OrderID And ItemNo=A.ItemNo Group By  OrderID,ItemNo)) 
    from OrderDetail A
    --删除测试环境
    drop table OrderDetail,Shipping
    --结果
    /*
    OrderID ItemNo Quantity 剩余数量
    1 2 100 40
    2 3 200 130
    */