求一单表查询语句

HU0L0NG 2007-09-21 03:13:13
表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 部门,名称

这样语句不通啊~~
...全文
123 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
HU0L0NG 2007-09-21
  • 打赏
  • 举报
回复
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

哎~~还得这样解决
晓风残月0110 2007-09-21
  • 打赏
  • 举报
回复
up
HU0L0NG 2007-09-21
  • 打赏
  • 举报
回复
呵呵,是一个,不是一次
俺的意思是一次~~
把原来的代码发上来大家看看
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)
fa_ge 2007-09-21
  • 打赏
  • 举报
回复
用一个SELECT不行吗?因为还涉及到其他几个相关表
这样写就太长了...

-----------
這本來就是一條語句,如果你還要加其它的表,在後面
left join 表1 on 條件
left join 表2 on 條件
....
tomyuansir 2007-09-21
  • 打赏
  • 举报
回复
楼上正解!!
dawugui 2007-09-21
  • 打赏
  • 举报
回复
这是一个SELECT,长是长了点.
HU0L0NG 2007-09-21
  • 打赏
  • 举报
回复
用一个SELECT不行吗?因为还涉及到其他几个相关表
这样写就太长了...
fa_ge 2007-09-21
  • 打赏
  • 举报
回复
借樓上數據一用



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
dawugui 2007-09-21
  • 打赏
  • 举报
回复
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)
go

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.名称

drop table tb

/*
部门 名称 数量
---------- ---------- -----------
办公室 铅笔 5
办公室 圆珠笔 5
财务部 A4纸 4
经理办 A4纸 2
客服部 圆珠笔 8
(所影响的行数为 5 行)
*/
dawugui 2007-09-21
  • 打赏
  • 举报
回复
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.名称

34,591

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧