表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 部门,名称这样语句不通啊~~
部门 名称 数量 出入类型
办公室 铅笔 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 部门,名称这样语句不通啊~~
解决方案 »
- 数据库恢复只有BAK文件,没有MDF文件也没有LOG文件,如何恢复数据库,急!!!!
- 大家看看两种查询语句哪个快
- SQL数据库运行后多次进行备份,如何删除过时的备份?
- 比较难的sql面试题,令我比较郁闷!
- sql统计空记录问题
- 存储过程查询结果输出到asp成功,但无法得到数值
- 高手请进,一个ms sqlserver 的replace函数高级问题
- 这个SQL语句怎么写?好着急呀!
- 一个极端麻烦的需求,怎么数据库设计?高手快进!
- 请问使用JBDC-ODBC开发时,查询操作对字段是否有长度要求?为什么我的最大长度是9位?
- 如何根据年月知道当月最大日期?
- -----------SQL数据运算-----------
(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.名称
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 行)
*/
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
这样写就太长了...
这样写就太长了...-----------
這本來就是一條語句,如果你還要加其它的表,在後面
left join 表1 on 條件
left join 表2 on 條件
....
俺的意思是一次~~
把原来的代码发上来大家看看
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)
(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哎~~还得这样解决