求教:怎样求产品日均库存数?

jx_liu 2008-03-05 08:24:15
产品型号 入库 出库 日期
A100 50 30 2008-02-02
A200 30 10 2008-02-02
A100 10 20 2008-02-03
A200 0 10 2008-02-03
A100 50 20 2008-02-06
A100 0 10 2008-02-20
A100 20 30 2008-02-20
…………………………………………………
求各产品型号在2008-02-01至2008-02-20日均库存数。
计算方法:每天库存数相加除以天数(当天库存数=(入库-出库)+上日库存数)
经果应该是
产品型号 日均库存量
A100 22.069
A200 10
...全文
1024 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
jx_liu 2008-03-06
  • 打赏
  • 举报
回复
蒋洪涛:
谢谢谢谢!!!!
你的方法非常好,用sl*ts解决了问题.
JiangHongTao 2008-03-06
  • 打赏
  • 举报
回复
一条语句也可以搞定
create table os(产品类型 varchar(10),入库 int,出库 int,日期 datetime)
insert into os select 'A100',50,30,'2008-02-02'
insert into os select 'A200',30,10,'2008-02-02'
insert into os select 'A100',10,20,'2008-02-03'
insert into os select 'A200',0,10,'2008-02-03'
insert into os select 'A100',50,20,'2008-02-06'
insert into os select 'A100',20,30,'2008-02-20'

select 产品类型,sum(sl*ts)*1.00/datediff(day,'2008-02-01',dateadd(day,1,'2008-02-20')) as 日均库存 from (
select 产品类型,日期,
isnull((select sum(入库-出库) from os where 产品类型 = a.产品类型 and 日期 <= a.日期),0) as sl,
datediff(day,日期,isnull((select min(日期) from os where 产品类型=a.产品类型 and 日期>a.日期),dateadd(day,1,'2008-02-20'))) as ts
from os a where 日期 between '2008-02-01' and '2008-02-20' group by 产品类型,日期) b
group by 产品类型
/*
产品类型 日均库存
---------- ---------------------------
A100 32.0000000000000
A200 10.0000000000000
*/
drop table os
pt1314917 2008-03-05
  • 打赏
  • 举报
回复
直接在查询分析器里面建那个存储过程。在程序中只需要执行exec www_p '2008-02-01','2008-02-20'这个就行了。。

ps:存储过程中,别忘了把表名os改成你自己的表名了。。。
jx_liu 2008-03-05
  • 打赏
  • 举报
回复
我是要放在页面里的,sql应怎样写?
pt1314917 2008-03-05
  • 打赏
  • 举报
回复

--执行存储过程:
exec www_p '2008-02-01','2008-02-20'

pt1314917 2008-03-05
  • 打赏
  • 举报
回复
这一整个是存储过程来的。 。

create proc www_p
@start datetime,
@end datetime
as
declare @start1 datetime
set @start1=@start
declare @t table(产品类型 varchar(50),data datetime)
declare @产品类型 varchar(50)
declare cur cursor for select distinct 产品类型 from os
open cur
fetch next from cur into @产品类型
while(@@fetch_status=0)
begin
while(@start<=@end)
begin
insert into @t select @产品类型,@start
set @start=dateadd(dd,1,@start)
end
set @start=@start1
fetch next from cur into @产品类型
end
close cur
deallocate cur

select b.产品类型,b.data,库存 into # from @t b left join
(select 产品类型,日期,
库存=(select isnull(sum(入库)-sum(出库),0) from os where a.产品类型=产品类型 and 日期<a.日期)+(a.入库-a.出库)
from os a)c
on b.data=c.日期 and b.产品类型=c.产品类型

select 产品类型,日均库存量=sum(库存)*1.0/count(1) from
(select 产品类型,data,
库存=isnull(case isnull(库存,'') when '' then
(select 库存 from # b where b.产品类型=a.产品类型 and b.data<a.data and isnull(b.库存,'')!='' and not exists
(select 1 from # where 产品类型=b.产品类型 and data>b.data and data<a.data and isnull(库存,'')!=''))
else 库存 end,0)
from # a)s
group by 产品类型

jx_liu 2008-03-05
  • 打赏
  • 举报
回复
运行
select 产品类型,日均库存量=sum(库存)*1.0/count(1) from
(select 产品类型,data,
库存=isnull(case isnull(库存,'') when '' then
(select 库存 from # b where b.产品类型=a.产品类型 and b.data<a.data and isnull(b.库存,'')!='' and not exists
(select 1 from # where 产品类型=b.产品类型 and data>b.data and data<a.data and isnull(库存,'')!=''))
else 库存 end,0)
from # a)s
group by 产品类型

列名 '库存' 无效。
zbc1009 2008-03-05
  • 打赏
  • 举报
回复
一步一步来吧,按部就班最简单(没有编辑器,徒手写,没颜色区分,应该能直接运行):

--先声名
declare @inv_today int,@inv_history int,@certain_day datetime
--初始化你的日期,哪天的库存
set certain_day='2008-2-20'
--计算当天除入库差异
select @inv_today=sum(入库-出库) from 库存表 where 日期 = certain_day
--计算历史库存
select @inv_history=sum(入库)-sum(出库) from 库存表 where 日期 < certain_day

--打印结果
print '该天的库存为:' + convert(varchar(10),@inv_today+@inv_history)
pt1314917 2008-03-05
  • 打赏
  • 举报
回复
--结果:
产品类型 日均库存量
A100 32.000000000000
A200 10.000000000000
pt1314917 2008-03-05
  • 打赏
  • 举报
回复

--这题比较麻烦。做出来了效率也很低。。
create table os(产品类型 varchar(10),入库 int,出库 int,日期 datetime)
insert into os select 'A100',50,30,'2008-02-02'
insert into os select 'A200',30,10,'2008-02-02'
insert into os select 'A100',10,20,'2008-02-03'
insert into os select 'A200',0,10,'2008-02-03'
insert into os select 'A100',50,20,'2008-02-06'
insert into os select 'A100',20,30,'2008-02-20'


create proc www_p
@start datetime,
@end datetime
as
declare @start1 datetime
set @start1=@start
declare @t table(产品类型 varchar(50),data datetime)
declare @产品类型 varchar(50)
declare cur cursor for select distinct 产品类型 from os
open cur
fetch next from cur into @产品类型
while(@@fetch_status=0)
begin
while(@start<=@end)
begin
insert into @t select @产品类型,@start
set @start=dateadd(dd,1,@start)
end
set @start=@start1
fetch next from cur into @产品类型
end
close cur
deallocate cur

select b.产品类型,b.data,库存 into # from @t b left join
(select 产品类型,日期,
库存=(select isnull(sum(入库)-sum(出库),0) from os where a.产品类型=产品类型 and 日期<a.日期)+(a.入库-a.出库)
from os a)c
on b.data=c.日期 and b.产品类型=c.产品类型


select 产品类型,日均库存量=sum(库存)*1.0/count(1) from
(select 产品类型,data,
库存=isnull(case isnull(库存,'') when '' then
(select 库存 from # b where b.产品类型=a.产品类型 and b.data<a.data and isnull(b.库存,'')!='' and not exists
(select 1 from # where 产品类型=b.产品类型 and data>b.data and data<a.data and isnull(库存,'')!=''))
else 库存 end,0)
from # a)s
group by 产品类型



exec www_p '2008-02-01','2008-02-20'

jx_liu 2008-03-05
  • 打赏
  • 举报
回复
对不起我算错了,你是对的报歉!
jx_liu 2008-03-05
  • 打赏
  • 举报
回复
对不起我算错了,你是对的抱歉!
pt1314917 2008-03-05
  • 打赏
  • 举报
回复

按照楼主的算法;A100应该为:
select (0+20+10+10+10+40+40+40+40+40+40+40+40+40+40+40+40+40+40+30)*1.0/20
可结果是32.000000噢。楼主再说明下

jx_liu 2008-03-05
  • 打赏
  • 举报
回复
试过
结果为:
A100 5
A200 5
不对.非常谢谢你!
cxmcxm 2008-03-05
  • 打赏
  • 举报
回复
对每日求和,再对每种产品求平均.
--设表名为tb
select 产品型号,sum(数量)/count(*) from
(select 产品型号,sum(入库-出库) 数量,日期 from tb group by 产品型号,日期) a
group by 产品型号

将此结果加上 上一期末数即可
jx_liu 2008-03-05
  • 打赏
  • 举报
回复
谢谢!
pt1314917 2008-03-05
  • 打赏
  • 举报
回复
哦。这样算啦。。
我看看```
jx_liu 2008-03-05
  • 打赏
  • 举报
回复
从2月1日至2月20日库存数分别为0,20,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10
日均=(0+20+10+10+10+10+10+10+10+10+10+10+10+10+10+10+10+10+10+10)/20
=10
jx_liu 2008-03-05
  • 打赏
  • 举报
回复
背着灵魂漫步:
谢谢你!因为2008-02-01为0
pt1314917 2008-03-05
  • 打赏
  • 举报
回复

产品型号 入库 出库 日期
A100 50 30 2008-02-02
A200 30 10 2008-02-02
A100 10 20 2008-02-03
A200 0 10 2008-02-03
A100 50 20 2008-02-06
A100 20 30 2008-02-20
------------------------------------
按照楼主的意思。以A200为例,2008-02-02日的库存数为30-10=20,2008-02-03日的库存数为0-10+20=10
则A200的日均库存量=(20+10)/2=15
为什么会是10呢?
到底是怎么个算法?
加载更多回复(1)

34,590

社区成员

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

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