select 编号,sum(case 开关 when 1 then 数量 else 0 end) as 进货数量,sum(case 开关 when 0 then 数量 else 0 end) as 销货数量 from 表名 group 编号

解决方案 »

  1.   

    select a,sum(case c when 1 then b else 0 end) as 进货,
    sum(case c when 0 then b else 0 end) as 销售,
    sum(case c when 1 then b else 0 end)-sum(case c when 0 then b else 0 end) as 剩余
    FROM 表名 group by a
    在MSSQL里通过
      

  2.   

    to funboy88(司令):
     為什麼在sqlserver 裡提示語法不正確
      

  3.   

    哈,多谢楼上
    哈,楼主我改了一下,下面的效率更好些select a,sum(case c when 1 then b else 0 end) as 进货,
    sum(case c when 0 then b else 0 end) as 销售,
    sum(case c when 1 then b when 0 then b*(-1) end) as 剩余
    FROM 表名 group by a
      

  4.   

    create table #table1(sid varchar(20),number int,switch int)
    goinsert into #table1 values ('A01',  8  ,  1)
    insert into #table1 values ('A01',  7  ,  0)
    insert into #table1 values ('A01',  2  ,  1)
    goselect sid as 編號 ,sum(case when switch=1 then number end)as 進貨數量,
    sum(case when switch=0 then number end)as 銷貨數量, 
    sum(case when switch=1 then number end)-sum(case when switch=0 then number end) as 剩余數量
    from #table1
    group by sid
      

  5.   

    funboy88(司令)升级就差这几分啊,马上两星变三星