实际应用:如何得到相应的入库数量?

skyclin 2009-06-14 11:24:18
在采用先进先出的材料成本核算中,我想为出库数量找到相应的入库数量!
比如说:

入库                   出库
---------------------------------------------------------------------
日期 数量 单价 日期 数量 单价
---------------------------------------------------------------------
2009-01-01 100 9.5
2009-01-03 100 12.5
2009-01-05 130 10.19


请教大侠们:
如何用一条语句得到某一条出库数据的相应入库,得到如下结果(入库).
2009-01-01 100 9.5
2009-01-03 30 12.5
...全文
53 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
guguda2008 2009-06-17
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 guguda2008 的回复:]
某一条出库数据的相应入库是用什么区分的?有id吗?
另外,如果有两个出库100的,得到的结果是一样的吗?还是入库顺序相减?
[/Quote]
我是想问某一条出库数据是怎么与其它出库区分的,用哪个字段
guguda2008 2009-06-17
  • 打赏
  • 举报
回复
某一条出库数据的相应入库是用什么区分的?有id吗?
另外,如果有两个出库100的,得到的结果是一样的吗?还是入库顺序相减?
skyclin 2009-06-17
  • 打赏
  • 举报
回复
出库数据之间的区分关系中表中“ZBID”字段,这个字段是通过NewID()产生的编号进行区分。
从业务上区分的话,还有出库日期、仓库编号和物资编号字段。
skyclin 2009-06-16
  • 打赏
  • 举报
回复
对josy说:

我看了你的代码,在测试过程中发现如下问题:
把第一条入库数据换成大于或等于的出库数值,则查询出来的数据出问题。
baetgc 2009-06-14
  • 打赏
  • 举报
回复
来学习
百年树人 2009-06-14
  • 打赏
  • 举报
回复
---测试数据---
if object_id('[入库]') is not null drop table [入库]
go
create table [入库]([日期] datetime,[数量] int,[单价] numeric(3,1))
insert [入库]
select '2009-01-01',100,9.5 union all
select '2009-01-03',100,12.5
if object_id('[出库]') is not null drop table [出库]
go
create table [出库]([日期] datetime,[数量] int,[单价] numeric(4,2))
insert [出库]
select '2009-01-05',130,10.19

---查询---
select
a.日期,
入库=case
when b.数量-(select sum(数量) from 入库 where 日期<=a.日期)>0
then a.数量
else b.数量-(select sum(数量) from 入库 where 日期<a.日期)
end,
单价
from
入库 a,
(select sum(数量) as 数量 from 出库) b

---结果---
日期 入库 单价
------------------------------------------------------ ----------- -----
2009-01-01 00:00:00.000 100 9.5
2009-01-03 00:00:00.000 30 12.5

(所影响的行数为 2 行)

百年树人 2009-06-14
  • 打赏
  • 举报
回复
---测试数据---
if object_id('[入库]') is not null drop table [入库]
go
create table [入库]([日期] datetime,[数量] int,[单价] numeric(3,1))
insert [入库]
select '2009-01-01',100,9.5 union all
select '2009-01-03',100,12.5
if object_id('[出库]') is not null drop table [出库]
go
create table [出库]([日期] datetime,[数量] int,[单价] numeric(4,2))
insert [出库]
select '2009-01-05',130,10.19

---查询---
select
a.日期,
入库=case
when b.数量-(select sum(数量) from 入库 where 日期<=a.日期)>0
then a.数量
else b.数量-(select sum(数量) from 入库 where 日期<a.日期)
end,
单价
from
入库 a,
(select sum(数量) as 数量 from 出库) b

---结果---
日期 入库 单价
------------------------------------------------------ ----------- -----
2009-01-01 00:00:00.000 100 9.5
2009-01-03 00:00:00.000 30 12.5

(所影响的行数为 2 行)
百年树人 2009-06-14
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 josy 的回复:]
为什么是100和30,而不是60和70或其他呢
每次最多只能100?
[/Quote]
楼主的意思是不是这样?
---测试数据---
if object_id('[入库]') is not null drop table [入库]
go
create table [入库]([日期] datetime,[数量] int,[单价] numeric(3,1))
insert [入库]
select '2009-01-01',100,9.5 union all
select '2009-01-03',100,12.5
if object_id('[出库]') is not null drop table [出库]
go
create table [出库]([日期] datetime,[数量] int,[单价] numeric(4,2))
insert [出库]
select '2009-01-05',130,10.19

---查询---
select
a.日期,
入库=case
when b.数量-(select sum(数量) from 入库 where 日期<=a.日期)>0
then a.数量
else b.数量-(select sum(数量) from 入库 where 日期<a.日期)
end,
单价
from
入库 a,
(select sum(数量) as 数量 from 出库) b

---结果---
日期 入库 单价
------------------------------------------------------ ----------- -----
2009-01-01 00:00:00.000 100 9.5
2009-01-03 00:00:00.000 30 12.5

(所影响的行数为 2 行)

--小F-- 2009-06-14
  • 打赏
  • 举报
回复
        else (select @cost-isnull(sum(j),0)+isnull(sum(c),0)+a.c from t where name=@name and jdate<a.jdate and j!=c) 
end
end
from t a where name=@name and j!=c
end
else
raiserror('库存不足',16,1)
return
go


--测试:

exec wsp @name='A',@cost=180
select * from t


--drop table t
--drop proc wsp

--小F-- 2009-06-14
  • 打赏
  • 举报
回复
        else (select @cost-isnull(sum(j),0)+isnull(sum(c),0)+a.c from t where name=@name and jdate<a.jdate and j!=c) 
end
end
from t a where name=@name and j!=c
end
else
raiserror('库存不足',16,1)
return
go


--测试:

exec wsp @name='A',@cost=180
select * from t


--drop table t
--drop proc wsp

--小F-- 2009-06-14
  • 打赏
  • 举报
回复
--给你参考下:
--库存先进先出简单例子:

create table t(
id int identity(1,1),
name varchar(50),--商品名称
j int, --入库数量
c int, --出库数量
jdate datetime --入库时间
)
insert into t(name,j,c,jdate) select 'A',100,0,'2007-12-01'
insert into t(name,j,c,jdate) select 'A',200,0,'2008-01-07'
insert into t(name,j,c,jdate) select 'B',320,0,'2007-12-21'
insert into t(name,j,c,jdate) select 'A',100,0,'2008-01-15'
insert into t(name,j,c,jdate) select 'B',90,0,'2008-02-03'
insert into t(name,j,c,jdate) select 'A',460,0,'2008-02-01'
insert into t(name,j,c,jdate) select 'A',510,0,'2008-03-01'
go



create proc wsp
@name varchar(50),--商品名称
@cost int --销售量
as
--先得出该货物的库存是否够
declare @spare float --剩余库存
select @spare=sum(j)-sum(c) from t where name=@name
if(@spare>=@cost)
begin
--根据入库日期采用先进先出原则对货物的库存进行处理
update t set c=
case when (select @cost-isnull(sum(j),0)+isnull(sum(c),0) from t where name=@name and jdate<=a.jdate and j!=c)>=0
then a.j
else
case when (select @cost-isnull(sum(j),0)+isnull(sum(c),0) from t where name=@name and jdate<a.jdate and j!=c)<0 then 0
else (select @cost-isnull(sum(j),0)+isnull(sum(c),0)+a.c from t where name=@name and jdate<a.jdate and j!=c)
end
end
from t a where name=@name and j!=c
end
else
raiserror('库存不足',16,1)
return
go


--测试:

exec wsp @name='A',@cost=180
select * from t


--drop table t
--drop proc wsp

百年树人 2009-06-14
  • 打赏
  • 举报
回复
为什么是100和30,而不是60和70或其他呢

每次最多只能100?

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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