try:
select a.MO号,isnull(a.a,0) 物料, isnull(b.a,0) 重量 from
(select sum(重量) a, MO号 from table1 where 物料<>'Y002100' group by MO号)a FULL OUTER join
(select sum(重量) b, MO号 from table1 where 物料='Y002100' group by MO号)b 
on a.MO号=b.MO号

解决方案 »

  1.   

    --测试
    create table [table](MO varchar(10),物料 varchar(10),重量 decimal(10,2))
    go--测试数据
    insert [table] select '1000003','F0L1300',10.00
    union all select '1000003','Y001100',80.30
    union all select '1000003','Y002100',50.00
    union all select '1000004','Y001100',100.00
    go--执行测试select a.MO,废钢=(select sum(重量) from [table] where 物料 in('F0L1300','Y001100') and MO=a.MO),海绵铁=isnull((select sum(重量) from [table] where MO=a.MO and 物料='Y002100'),0.00) from [table] a group by a.MO--测试结果 MO号      废钢       海绵铁
    1000003   90.30      50.00
    1000004   100.00     0.00
    --删除测试
    drop table [table]
      

  2.   

    select MO号
    ,废钢=sum(case when 物料 in('F0L1300','Y001100') then 重量 else 0 end)
    ,海绵铁=sum(case when 物料='Y002100' then 重量 else 0 end)
    from [table]
    group by MO号
      

  3.   

    select id,
    sum(case 物料 when 'Y002100 ' then 重量 else 0 end) as 海绵铁 ,
    sum(case 物料 when 'Y002100' then 0  else num end)as  废钢   
    from table group by id
      

  4.   

    多谢各位了,我采用的是
    (select sum(重量)as 废钢, MO号 from #temp1 where 物料='Y001100' or 物料='Y001200' or substring(物料,1,1)='F' group by MO号)a FULL OUTER join
    (select sum(重量) as 海绵铁, MO号 from #temp1 where 物料='Y002100'or 物料='Y002101' group by MO号)b on a.MO号=b.MO号 full outer join
    (select sum(重量) as 生铁, MO号 from #temp1 where 物料='YA02400' group by MO号)c on a.MO号=c.MO号 full outer join 
    (select sum(重量) as 铁水, MO号 from #temp1 where 物料='Y002300' group by MO号)d on a.MO号=d.MO号
    order by a.MO号