表1
部门  名称  数量  出入类型
办公室 铅笔  10    0
办公室 铅笔  5    1
财务部 A4纸  2     0
办公室 圆珠笔 5     0
财务部 A4纸  2     0
办公室 铅笔  50    0
客服部 圆珠笔 8     0
经理办 A4纸  2     0
办公室 铅笔  50    1
财务部 A4纸  2     3
.....求分部门每种材料领用汇总,出入类型为0的数量减去出入为1的数量select 部门,名称,(sum(数量) with (出入类型=0) - sum(数量) with(出入类型=1))
from 表1
where 出入类型 in(0,1)
group by 部门,名称这样语句不通啊~~

解决方案 »

  1.   

    select isnull(m.部门,n.部门) 部门 , isnull(m.名称,n.名称) 名称 , isnull(m.数量,0) - isnull(n.数量,0) 数量 from
    (select 部门,名称,sum(数量) 数量 from tb where 出入类型 = 0 group by 部门,名称) m 
    full join
    (select 部门,名称,sum(数量) 数量 from tb where 出入类型 = 1 group by 部门,名称) n
    on m.部门 = n.部门 and m.名称 = n.名称
      

  2.   

    create table tb(部门 varchar(10),名称 varchar(10),数量 int,出入类型 int)
    insert into tb values('办公室','铅笔',10, 0)
    insert into tb values('办公室','铅笔',5,1)
    insert into tb values('财务部','A4纸',2,  0)
    insert into tb values('办公室','圆珠笔',5,  0)
    insert into tb values('财务部','A4纸',2,  0)
    insert into tb values('办公室','铅笔',50, 0)
    insert into tb values('客服部','圆珠笔',8 , 0)
    insert into tb values('经理办','A4纸',2,  0)
    insert into tb values('办公室','铅笔',50, 1)
    goselect isnull(m.部门,n.部门) 部门 , isnull(m.名称,n.名称) 名称 , isnull(m.数量,0) - isnull(n.数量,0) 数量 from
    (select 部门,名称,sum(数量) 数量 from tb where 出入类型 = 0 group by 部门,名称) m 
    full join
    (select 部门,名称,sum(数量) 数量 from tb where 出入类型 = 1 group by 部门,名称) n
    on m.部门 = n.部门 and m.名称 = n.名称drop table tb/*
    部门         名称         数量          
    ---------- ---------- ----------- 
    办公室        铅笔        5
    办公室        圆珠笔      5
    财务部        A4纸        4
    经理办        A4纸        2
    客服部        圆珠笔      8
    (所影响的行数为 5 行)
    */
      

  3.   

    借樓上數據一用create table tb(部門 varchar(10),名稱 varchar(10),數量 int,出入類型 int)
    insert into tb values('辦公室','鉛筆',10, 0)
    insert into tb values('辦公室','鉛筆',5,1)
    insert into tb values('財務部','A4紙',2,  0)
    insert into tb values('辦公室','圓珠筆',5,  0)
    insert into tb values('財務部','A4紙',2,  0)
    insert into tb values('辦公室','鉛筆',50, 0)
    insert into tb values('客服部','圓珠筆',8 , 0)
    insert into tb values('經理辦','A4紙',2,  0)
    insert into tb values('辦公室','鉛筆',50, 1)
    select a.部門 ,a.名稱,a.qty-isnull(b.qty,0) as qty from 
    (
    select 部門 ,名稱 ,sum(數量)as qty  from tb where 出入類型=0 group by 部門 ,名稱
    )a
    left join
    (
    select 部門 ,名稱 ,sum(數量)as qty  from tb where 出入類型=1 group by 部門 ,名稱
    )b
    on a.部門=b.部門 and a.名稱=b.名稱部門         名稱         qty         
    ---------- ---------- ----------- 
    客服部        圓珠筆        8
    財務部        A4紙        4
    經理辦        A4紙        2
    辦公室        圓珠筆        5
    辦公室        鉛筆         5(5 row(s) affected)
    --drop table tb
      

  4.   

    用一个SELECT不行吗?因为还涉及到其他几个相关表
    这样写就太长了...
      

  5.   

    用一个SELECT不行吗?因为还涉及到其他几个相关表
    这样写就太长了...-----------
    這本來就是一條語句,如果你還要加其它的表,在後面
    left join 表1 on 條件 
    left join 表2 on 條件
    ....
      

  6.   

    呵呵,是一个,不是一次
    俺的意思是一次~~
    把原来的代码发上来大家看看
    SELECT DictOffice.OfficeName,
                  DictMedi.MediCode,
                  DictMedi.MediName ,
                  sum(Glide.InOutNum)as num ,
                  glide.unit ,
                  Glide.ActPri,
                  sum(Glide.ActMon)as actmon,
               glide_total.inout   
    FROM Glide,   DictMedi,  DictOffice, glide_total, dictorigin    
     WHERE ( glide.glideid = glide_total.glideid ) and 
              ( Glide.MediId = DictMedi.MediId ) and 
                ( glide_total.FellowId = DictOffice.officeId ) and 
              ( glide_total.tranid = dictorigin.originid )  and
     glide_total.happendate >= '2007-09-01' and 
    glide_total.happendate <= '2007-09-20' and 
    glide_total.basecode = 881  and glide_total.inout in(0,1) and dictorigin.print_bs = 1 
    group by DictOffice.OfficeName,
                  DictMedi.MediCode,
                  DictMedi.MediName ,
                  glide.unit,
                  Glide.ActPri,
               glide_total.inout ===================================
    glide_total.inout =0 的sum(Glide.InOutNum)和sum(Glide.ActMon)分别减去
    glide_total.inout =1 的sum(Glide.InOutNum)和sum(Glide.ActMon)
      

  7.   

    select OfficeName,MediCode,MediName,sum(num),unit ,ActPri,sum(actmon) from
    (SELECT DictOffice.OfficeName,
                  DictMedi.MediCode,
                  DictMedi.MediName ,
                  sum(Glide.InOutNum)as num ,
                  glide.unit ,
                  Glide.ActPri,
                  sum(Glide.ActMon)as actmon,
               glide_total.inout   
    FROM Glide,   DictMedi,  DictOffice, glide_total, dictorigin    
     WHERE ( glide.glideid = glide_total.glideid ) and 
              ( Glide.MediId = DictMedi.MediId ) and 
                ( glide_total.FellowId = DictOffice.officeId ) and 
              ( glide_total.tranid = dictorigin.originid )  and
     glide_total.happendate >= '2007-09-01' and 
    glide_total.happendate <= '2007-09-20' and 
    glide_total.basecode = 881  and glide_total.inout =0 and dictorigin.print_bs = 1 
    group by DictOffice.OfficeName,
                  DictMedi.MediCode,
                  DictMedi.MediName ,
                  glide.unit,
                  Glide.ActPri,
               glide_total.inout 
    union all
    SELECT DictOffice.OfficeName,
                  DictMedi.MediCode,
                  DictMedi.MediName ,
                  '-0' + sum(Glide.InOutNum)as num ,
                  glide.unit ,
                  Glide.ActPri,
                  '-0' + sum(Glide.ActMon)as actmon,
               glide_total.inout   
    FROM Glide,   DictMedi,  DictOffice, glide_total, dictorigin    
     WHERE ( glide.glideid = glide_total.glideid ) and 
              ( Glide.MediId = DictMedi.MediId ) and 
                ( glide_total.FellowId = DictOffice.officeId ) and 
              ( glide_total.tranid = dictorigin.originid )  and
     glide_total.happendate >= '2007-09-01' and 
    glide_total.happendate <= '2007-09-20' and 
    glide_total.basecode = 881  and glide_total.inout =1 and dictorigin.print_bs = 1 
    group by DictOffice.OfficeName,
                  DictMedi.MediCode,
                  DictMedi.MediName ,
                  glide.unit,
                  Glide.ActPri,
               glide_total.inout ) as xx1
    group by OfficeName,MediCode,MediName,unit ,ActPri哎~~还得这样解决