日期:2014-05-18  浏览次数:20571 次

又來麻煩大家啦, 想找个简便点的计算方法, 大虾帮下
SQL code

表1有6個字段
单号    自动编号         需求日期      需求数量     ONTIME数量 OVERTIME数量
A     1        2011-1-3      10
A     2        2011-1-7      15
A     3        2011-1-15      20

表2有2个资料
日期        生产数
2011-1-1      7
2011-1-5      12
2011-1-8      11
2011-1-14      15

要求计算出结果
单号    自动编号         需求日期       需求数量     ONTIME数量 OVERTIME数量
A     1        2011-1-3      10        7           3
A     1        2011-1-7      15            9           6
A     1        2011-1-15      20            20

想找个快捷点的办法, 游标的话试了下感觉效率很低。。



------解决方案--------------------
ONTIME数量怎么算出来的?
------解决方案--------------------
http://topic.csdn.net/u/20111110/11/2f3a0106-8085-4d43-981a-d105ac9b92b8.html
看看这个帖子
------解决方案--------------------
靠 有点看懂了。类似库存的先进先出。
------解决方案--------------------
SQL code
--库存先进先出简单例子:

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

------解决方案--------------------
探讨
小F好久不见了。。
ONTIME数量是这样算的
不是1月3号有10PCS的需求, 然后1月1号有生产7个, 1月5号有生产12个
所以ONTIME的是7PCS, 剩余3PCS就是OVERTIME的

------解决方案--------------------
力气活.........

------解决方案--------------------
这例子里我觉得return好像没用
没在begin end里
就相当于所有情况都走到最后的return
但是这时过程已经完了。。。
探讨

SQL code
--库存先进先出简单例子:

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,……

------解决方案--------------------
SQL code

create table tb1(
单号 varchar(10),
自动编号 int,
需求日期 datetime,
需求数量 int,
ONTIME数量 int,
OVERTIME数量 int
)
insert into tb1
select 'A',1,'2011-1-3',10,null,null union all
select 'A',2,'2011-1-7',15,null,null union all
select 'A',3,'2011-1-15',20,null,null
go

create table tb2(
日期 datetime,
生产数 int
)
insert into tb2
select '2011-1-1',7 union all
select '2011-1-5',12 union all
select '2011-1-8',11 union all
select '2011-1-14',15
go
/*
要求计算出结果
单号    自动编号         需求日期       需求数量     ONTIME数量 OVERTIME数量
A     1        2011-1-3      10        7