27,579
社区成员
发帖
与我相关
我的任务
分享
--5楼说的有道理
--4楼的也有点小问题
if object_id('产品表')is not null
drop table 产品表
if object_id('入库表')is not null
drop table 入库表
if object_id('出库表')is not null
drop table 出库表
if object_id('库存表')is not null
drop table 库存表
go
create table 产品表
( 代码 int)
create table 入库表
(代码 int,
入库数量 int,
入库时间 datetime
)
create table 出库表
( 代码 int,
出库数量 int,
出库时间 datetime
)
create table 库存表
( 代码 int,
当前库存 int
)
insert into 产品表 select 1
union all select 2
union all select 3
union all select 4
union all select 5
insert into 入库表 select 1,10,'2008-06-09'
union all select 3,15,'2008-09-10'
insert into 出库表 select 2,15,'2008-05-10'
union all select 3,10,'2008-09-12'
insert into 库存表 select 1,20
union all select 3,15
-----------
select 产品表.代码,期初库存=isnull(库存表.当前库存,0)+isnull(出库表.出库数量,0)-isnull(入库表.入库数量,0),入库数量=isnull(入库表.入库数量,0),
出库数量=isnull(出库表.出库数量,0),期末库存=isnull(库存表.当前库存,0)
from 产品表 left join 入库表 on 产品表.代码=入库表.代码 and convert(varchar(7),入库表.入库时间,120) = '2008-09'
left join 出库表 on 产品表.代码=出库表.代码 and convert(varchar(7),出库表.出库时间,120) = '2008-09'
left join 库存表 on 产品表.代码=库存表.代码
--二楼的结果可能没测试,有点问题
--1)产品表
create table PRO(代码 INT)
INSERT INTO PRO
SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5
--2)入库表
create table RK(代码 INT,入库数量 int ,入库时间 varchar(10))
INSERT INTO RK
SELECT 1,10,'2008-06-09' UNION SELECT 3,15,'2008-09-10'
--3)出库表
create table KK(代码 INT,出库数量 int ,出库时间 varchar(10))
INSERT INTO KK
SELECT 2,15,'2008-05-10' UNION SELECT 3,10,'2008-09-12'
--4)库存表(库存归零就删掉)
create table KC(代码 INT,当前库存 int)
INSERT INTO KC
SELECT 1,20 UNION SELECT 3,15
select a.代码,
期初库存 = isnull(sum(d.当前库存),0) - (isnull(sum(b.入库数量),0) - isnull(sum(c.出库数量),0)) ,
isnull(sum(b.入库数量),0) 入库数量,
isnull(sum(c.出库数量),0) 出库数量,
isnull(sum(d.当前库存),0) 期末库存
from PRO a
left join RK b on a.代码 = b.代码 and datepart(mm,b.入库时间 ) = 9
left join KK c on a.代码 = c.代码 and datepart(mm,c.出库时间 ) = 9
left join KC d on a.代码 = d.代码
group by a.代码
drop table PRO,RK,KK,KC
create table 产品表(代码 int)
insert into 产品表 select 1
union all select 2
union all select 3
union all select 4
union all select 5
create table 入库表(代码 int,入库数量 int,入库时间 datetime)
insert into 入库表 select 1,10,'2008-06-09'
union all select 3,15,'2008-09-10'
union all select 3,20,'2008-10-05' --统计是在十月份进行,可能又有十月份的记录
create table 出库表(代码 int,出库数量 int,出库时间 datetime)
insert into 出库表 select 2,15,'2008-05-10'
union all select 3,10,'2008-09-12'
union all select 3,10,'2008-10-08' --同上
create table 库存表(代码 int,当前库存 int)
insert into 库存表 select 1, 20
union all select 3,25 --设当前时间为十月二十日
go
select a.代码,
期初库存 =isnull(b.当前库存,0)
+(select isnull(sum(出库数量),0) from 出库表 where 代码=a.代码 and convert(varchar(7),出库时间,120)>='2008-09')
-(select isnull(sum(入库数量),0) from 入库表 where 代码=a.代码 and convert(varchar(7),入库时间,120)>='2008-09'),
入库数量=(select isnull(sum(入库数量),0) from 入库表 where 代码=a.代码 and convert(varchar(7),入库时间,120)='2008-09'),
出库数量=(select isnull(sum(出库数量),0) from 出库表 where 代码=a.代码 and convert(varchar(7),出库时间,120)='2008-09'),
期末库存 =isnull(b.当前库存,0)
+(select isnull(sum(出库数量),0) from 出库表 where 代码=a.代码 and convert(varchar(7),出库时间,120)>'2008-09')
-(select isnull(sum(入库数量),0) from 入库表 where 代码=a.代码 and convert(varchar(7),入库时间,120)>'2008-09')
from 产品表 a left join 库存表 b on a.代码=b.代码
go
drop table 产品表,入库表,出库表,库存表
/*
代码 期初库存 入库数量 出库数量 期末库存
----------- ----------- ----------- ----------- -----------
1 20 0 0 20
2 0 0 0 0
3 10 15 10 15
4 0 0 0 0
5 0 0 0 0
(5 行受影响)
*/
create table 产品表(代码 int)
insert into 产品表 select 1
union all select 2
union all select 3
union all select 4
union all select 5
create table 入库表(代码 int,入库数量 int,入库时间 datetime)
insert into 入库表 select 1,10,'2008-06-09'
union all select 3,15,'2008-09-10'
create table 出库表(代码 int,出库数量 int,出库时间 datetime)
insert into 出库表 select 2,15,'2008-05-10'
union all select 3,10,'2008-09-12'
create table 库存表(代码 int,当前库存 int)
insert into 库存表 select 1, 20
union all select 3,15
go
select a.代码,
期初库存 =isnull(b.当前库存,0)
+(select isnull(sum(出库数量),0) from 出库表 where 代码=a.代码 and convert(varchar(7),出库时间,120)>='2008-09')
-(select isnull(sum(入库数量),0) from 入库表 where 代码=a.代码 and convert(varchar(7),入库时间,120)>='2008-09'),
入库数量=(select isnull(sum(入库数量),0) from 入库表 where 代码=a.代码 and convert(varchar(7),入库时间,120)='2008-09'),
出库数量=(select isnull(sum(出库数量),0) from 出库表 where 代码=a.代码 and convert(varchar(7),出库时间,120)='2008-09'),
期末库存 =isnull(b.当前库存,0)
+(select isnull(sum(出库数量),0) from 出库表 where 代码=a.代码 and convert(varchar(7),出库时间,120)>'2008-09')
-(select isnull(sum(入库数量),0) from 入库表 where 代码=a.代码 and convert(varchar(7),入库时间,120)>'2008-09')
from 产品表 a left join 库存表 b on a.代码=b.代码
go
drop table 产品表,入库表,出库表,库存表
/*
代码 期初库存 入库数量 出库数量 期末库存
----------- ----------- ----------- ----------- -----------
1 20 0 0 20
2 0 0 0 0
3 10 15 10 15
4 0 0 0 0
5 0 0 0 0
(5 行受影响)
*/
SELECT 品名,本月出库 = IsNull(
(
SELECT SUM(数量)
FROM 出库表
WHERE 品名=a.品名 AND 出库时间 Between '2008-09-01' AND '2008-09-30'
),0),
本月进库= IsNull(
(
SELECT SUM(数量)
FROM 进库表
WHERE 品名=a.品名 AND 入库时间 Between '2008-09-01' AND '2008-09-30'
),0),
数量 as 库存结余
FROM 库存表 as A
SELECT 品名,本月出库 = IsNull(
(
SELECT SUM(数量)
FROM 出库表
WHERE 品名=a.品名 AND 出库时间 Between '2008-09-01' AND '2008-09-30'
),0),
本月进库= IsNull(
(
SELECT SUM(数量)
FROM 进库表
WHERE 品名=a.品名 AND 入库时间 Between '2008-09-01' AND '2008-09-30'
),0),
数量
FROM 库存表 as A
if object_id('产品表')is not null
drop table 产品表
if object_id('入库表')is not null
drop table 入库表
if object_id('出库表')is not null
drop table 出库表
if object_id('库存表')is not null
drop table 库存表
go
create table 产品表
( 代码 int)
create table 入库表
(代码 int,
入库数量 int,
入库时间 datetime
)
create table 出库表
( 代码 int,
出库数量 int,
出库时间 datetime
)
create table 库存表
( 代码 int,
当前库存 int
)
insert into 产品表 select 1
union all select 2
union all select 3
union all select 4
union all select 5
insert into 入库表 select 1,10,'2008-06-09'
union all select 3,15,'2008-09-10'
insert into 出库表 select 2,15,'2008-05-10'
union all select 3,10,'2008-09-12'
insert into 库存表 select 1,20
union all select 3,15
-----------
select 产品表.代码,期初库存=isnull(库存表.当前库存,0),isnull(入库表.入库数量,0),
isnull(出库表.出库数量,0),期末库存=isnull(库存表.当前库存,0)
from 产品表 left join 入库表 on 产品表.代码=入库表.代码
left join 出库表 on 产品表.代码=出库表.代码
left join 库存表 on 产品表.代码=库存表.代码
1)产品表
create table PRO(代码 INT)
INSERT INTO PRO
SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5
2)入库表
create table RK(代码 INT,入库数量 int ,入库时间 varchar(10))
INSERT INTO RK
SELECT 1,10,'2008-06-09' UNION SELECT 3,15,'2008-09-10'UNION SELECT 3,15,'2008-09-11'
3)出库表
create table KK(代码 INT,出库数量 int ,出库时间 varchar(10))
INSERT INTO KK
SELECT 2,15,'2008-05-10' UNION SELECT 3,10,'2008-09-12'
4)库存表(库存归零就删掉)
create table KC(代码 INT,当前库存 int)
INSERT INTO KC
SELECT 2,20 UNION SELECT 3,15
代码 当前库存
1 20
3 15
目标:
统计表(统计九月份仓库进出情况,期初库存用倒推)
代码 期初库存 入库数量 出库数量 期末库存
1 20 0 0 20
2 0 0 0 0
3 10 15 10 15
4 0 0 0 0
5 0 0 0 0
select a.代码,
期初库存 = isnull(sum(d.当前库存),0) - (isnull(sum(b.入库数量),0) - isnull(sum(c.出库数量),0)) ,
isnull(sum(b.入库数量),0) 入库数量,
isnull(sum(c.出库数量),0) 出库数量,
isnull(sum(d.当前库存),0) 期末库存
from PRO a
left join RK b on a.代码 = b.代码
left join KK c on a.代码 = c.代码
left join KC d on a.代码 = d.代码
where datepart(mm,b.入库时间 ) = 9 and datepart(mm,c.出库时间 ) = 9
group by a.代码