请问一个SQL语句?
这里有以下几张表
A:备件名称(bjname),备件规格(bjspec),备件编码(bjid),安全库存(safetyamount)
B:备件编码(bjid),入库日期(yyyy-mm格式,字段为indate),入库数量(inamount)
C: 备件编码(bjid),出库日期(yyyy-mm格式,字段为outdate),入库数量(outamount)
现在想实现一个表,按照备件别、月别统计入库和出库的数量,例如
备件名称 备件规格 备件编码 入库数量 出库数量 月别
轴承 spec1 001 10 8 2006-01
轴承 spec1 001 0 1 2006-02
问题点数:20、回复次数:11Top
1 楼xhy1112(cecilia)回复于 2006-03-01 13:36:33 得分 0
select 备件名称 , 备件规格, 备件编码 , 入库数量, 出库数量 , 月别
from (select * from A
left join B on A.bjid = B.bjid
left join c on B.inamount = C.inamount)Top
2 楼whawkqq(快乐做人)回复于 2006-03-01 13:45:28 得分 0
楼上的不行啊。。。Top
3 楼lsqkeke(可可)回复于 2006-03-01 13:49:29 得分 0
select 备件名称=A.bjname,
备件规格=A.bjspec,
备件编码=A.bjid,
入库数量=sum(inamount),
出库数量=sum(outamount),
月别=B.indate
from A,B,C
where B.bjid=C.bjid and indate=outdate and A.bjid=B.bjid
group by B.bjid,indate
Top
4 楼whawkqq(快乐做人)回复于 2006-03-01 13:59:01 得分 0
如果要查这个备件在某个月的入库总量和出库总量,这个语句可能不行吧,因为可能存在某个月这个备件入库10,出库8,而另外一个月是入库为0,出库1;所以如果把这些数据都罗列出来,怎么写呢?Top
5 楼whawkqq(快乐做人)回复于 2006-03-01 14:05:36 得分 0
select B.bj_id,B.total,C.total2,B.indate2 from
(select bj_id,convert(varchar(7),indate,20) as indate2,sum(isnull(inamount,0)) as total from instorage
group by bj_id,convert(varchar(7),indate,20)) B,
(select bj_id,convert(varchar(7),outdate,20) as indate3,sum(isnull(amount,0)) as total2 from outstorage
group by bj_id,convert(varchar(7),outdate,20)) C
where B.bj_id=C.bj_id
order by B.bj_id
这个是我写的语句,其实把instorage即为B表,outstorage即为C表,A表可以暂时去掉
执行的结果还是达不到效果Top
6 楼whawkqq(快乐做人)回复于 2006-03-01 15:59:50 得分 0
请大家帮忙看一下,这个语句错误在什么地方 ?Top
7 楼xhy1112(cecilia)回复于 2006-03-01 22:07:57 得分 0
--Create table
Create table A(bjname varchar(10)
,bjspec varchar(10)
,bjid varchar(10)
,safetyamount int
)
Create table B(bjid varchar(10)
,indate varchar(10)
,inamount int
)
Create table C(bjid varchar(10)
,outdate varchar(10)
,outamount int
)
--Insert Data
insert into A select '轴承','spec1','001',100
insert into B select '001','2006-01',10
union all select '001','2006-02',0
insert into C select '001','2006-01',8
union all select '001','2006-02',1
--Test
select 备件名称 = a.bjname
,备件规格 = a.bjspec
,备件编码 = a.bjid
,入库数量 = BC.inamount
,出库数量 = BC.outamount
,月别 = BC.ThisDate
from A
inner join
(
select b.bjid,inamount = sum(b.inamount)
,outamount = sum(Case when c.outdate is null then 0 else c.outamount end) --考虑到出库月份在如库月份不存在的情况下,出库数量为0处理
,ThisDate = b.indate
from B
left join C
on B.bjid = C.bjid
and B.indate = c.outdate
group by b.bjid ,b.indate
)BC
on a.bjid = BC.bjid
--Drop Table
Drop Table A
Drop Table B
Drop Table CTop
8 楼xhy1112(cecilia)回复于 2006-03-01 22:18:24 得分 0
--当出库月份或入库月份不存在时
--Create Table
Create table A(bjname varchar(10)
,bjspec varchar(10)
,bjid varchar(10)
,safetyamount int
)
Create table B(bjid varchar(10)
,indate varchar(10)
,inamount int
)
Create table C(bjid varchar(10)
,outdate varchar(10)
,outamount int
)
--Insert Date
insert into A select '轴承','spec1','001',100
insert into B select '001','2006-01',10
union all select '001','2006-02',0
union all select '001','2006-03',10
insert into C select '001','2006-01',8
union all select '001','2006-02',1
union all select '001','2006-04',5
--Test
--当出库月份或入库月份不存在时
select 备件名称 = a.bjname
,备件规格 = a.bjspec
,备件编码 = a.bjid
,入库数量 = BC.inamount
,出库数量 = BC.outamount
,月别 = BC.ThisDate
from A
inner join
(
select bjid = max(Case when b.bjid is null then c.bjid when c.bjid is null then b.bjid else b.bjid end)
,inamount = sum(Case when b.indate is null then 0 else b.inamount end)
,outamount = sum(Case when c.outdate is null then 0 else c.outamount end) --考虑到出库月份或入库月份不存在的情况下,数量为0处理
,ThisDate = max(Case when b.indate is null then c.outdate when c.outdate is null then b.indate else b.indate end)
from B
full join C
on B.bjid = C.bjid
and B.indate = c.outdate
group by b.bjid ,b.indate
)BC
on a.bjid = BC.bjid
order by BC.ThisDate
--Drop Table
Drop Table A
Drop Table B
Drop Table C
Top
9 楼whawkqq(快乐做人)回复于 2006-03-02 08:08:47 得分 0
谢谢楼上的高手在深夜里帮我解答问题(很详细),非常感动
不过我还是想问一下:如果B表中没有0,就是2月份如果没有入库,那么这样的语句可以嘛?Top
10 楼whawkqq(快乐做人)回复于 2006-03-02 09:19:30 得分 0
还有里面的max是什么意思啊
我实验了,如果把2月份入库的记录去掉,就发生了错误Top
11 楼whawkqq(快乐做人)回复于 2006-03-03 08:33:28 得分 0
Help me ! I am very urgency...Top




