select code,sum(qty) inqty,(select sum(qty) outqty from yourtable t2
where t1.code=t2.code)
from yourtable y1
where type=0
group by code

解决方案 »

  1.   

    select code ,inqty =sum(case type when 0 then qty else 0 end),
    outqty=sum(case type when 1 then qty else 0 end)
     from tablename group by code
      

  2.   

    Cannot resolve collation conflict for equal to operation.
    报这个错误
      

  3.   

    一楼的语句漏了个条件应该是
    select code,sum(qty) inqty,(select sum(qty) outqty from tablename t2
    where t1.code=t2.code and t2.type=1)
    from tablename t1
    where type=0
    group by code 二楼的语句应该没错
      

  4.   

    select code ,inqty =sum(case type when 0 then qty else 0 end),
    outqty=sum(case type when 1 then qty else 0 end)
     from tablename group by code
    应该没问题啊,楼主是什么数据库?
      

  5.   

    select code ,inqty =sum(case type when 0 then qty else 0 end),
    outqty=sum(case type when 1 then qty else 0 end)
     from tablename group by code