[急]赠100分求解本论坛1周没解决的问题——寻高手!!!
[求助]求计算货物移动加权平均价格的语句
一、基础表2个:
DJ——单据表,记录每一次进货的单据详细内容:id(货物编号int),sl(数量decimal(10,2)),dj(单 价decimal(10,2));
KC——库存表,记录目前货物的库存情况:id(货物编号int),kc(库存数量decimal(10,2))
二、两个表的数据示例(很长,截取前面1段,货物编号从1开始,不连续,最大编号为144,即id <=144)
==========DJ============= ==============kc=============
id sl dj id kc
1 110 5 1 224
1 123 6 3 432
1 225 5 4 2232
3 11 7 5 370
3 24 7 7 32
3 123 43 12 800
3 432 23
4 123 23
4 43 22
4 321 51
4 234 23
4 2131 44
5 45 23
5 324 23
5 324 34
5 43 12
7 543 32
7 876 34
7 764 32
12 768 678
12 45 768
12 3453 867
12 324 876
12 245 888
12 324 777
12 312 666
12 123 678
三、计算方法
要求计算已售货物的平均价格,首先要根据库存货物数量分析已售出货物的数量及价格情况,来进行计算。例如:
(一)对id=1的货物,期末库存为224,DJ表中id=1的进货记录,最后1笔的进货数量为225,说明期末的224全部是最后1笔所进的货物,因此已售货物的平均价格=[110*5+123*6+(225-224)*5]/(110+123+225-224);
(二)对id=3的货物,期末库存为432,DJ表中id=3的进货记录,最后1笔的进货数量为432,说明库存货物恰好是最后1笔单据所进的货物,因此已售货物的平均价格=[11*7+24*7+123*43+(432-432)*23]/(11+24+123+432-432),计算方法同(一);
(三)对id=4的货物,期末库存为2232,DJ表中id=4的进货记录,最后1笔的进货数量为2131,说明有2232-2131=101的库存是在以前的单据记录中进的,倒数第二笔单据的进货sl为234,这也就说明了在2232的库存货物中:有2131的货物进价为44,有101的货物进价为23,因此已售货物的平均价格=[123*23+43*22+321*51+(234+2131-2232)*23]/(123+43+321+234+2131-2232)
(四)id=5同(三),只是根据id=5的库存数量追溯到dj表中id=5的单据的第二行记录的数量,已售货物的平均价格=[45*23+(324+324+43-370)*23]/(45+324+324+43-370);
(五)id=7同(一),已售货物的平均价格=[543*32+876*34+(764-32)*32]/(543+876+764-32);
(六)id=12的同(三),只是根据id=12的库存数量追溯到dj表中id=12的单据的正数第5行记录的数量,已售货物的平均价格=[768*678+45*768+3453*867+(245+324+312+123)*888]/(768+45+3453+324+245+324+312+123-800);
四、最终要求
将求出的已售货物的平均价格(PJJG,两位小数)和相应的货物编号(ID,int类型)生成一张新表:已售货物平均价格表(YSHWPJJG)中。
五、请高手指教,谢谢!!!!!!!
[解决办法]
1、感觉实际的dj表应该还有其他字段,比如进货时间或者纪录id字段,如果没有,只能借助临时表。
2、借助临时表的语句及测试
--建立环境
declare @DJ table (
id int,
sl int,
dj int
)
insert @DJ select
1, 110, 5
union all select
1, 123 , 6
union all select
1, 225 , 5
union all select
3, 11 , 7
union all select
3, 24 ,7
union all select
3, 123, 43
union all select
3, 432 , 23
union all select
4, 123 , 23
union all select
4, 43 , 22
union all select
4, 321 ,51
union all select
4, 234, 23
union all select
4, 2131, 44
union all select
5, 45 , 23
union all select
5, 324 , 23
union all select
5, 324 ,34
union all select
5, 43, 12
union all select
7, 543, 32
union all select
7, 876 , 34
union all select
7, 764 , 32
union all select
12, 768 ,678
union all select
12, 45 ,768
union all select
12, 3453, 867
union all select
12, 324 , 876
union all select
12, 245 , 888
union all select
12, 324 , 777
union all select
12, 312 ,666
union all select
12, 123 , 678
declare @kc table (
id int,
kc int
)
insert @kc select
1, 224
union all select
3 , 432
union all select
4 , 2232
union all select
5 , 370
union all select
7 , 32
union all select
12 , 800
--产生临时表
select *,IDENTITY(int,1,1) as id0 into # from @dj
--查询
select id,1.0*sum(金额)/sum(数量) as 平均价
from (
select a.id,
case when isnull((select sum(sl) from # where id=a.id and id0> a.id0),0)> b.kc then a.sl*a.dj
else (isnull((select sum(sl) from # where id=a.id and id0> =a.id0),0)-b.kc)*a.dj
end as 金额,
case when isnull((select sum(sl) from # where id=a.id and id0> a.id0),0)> b.kc then a.sl
else (isnull((select sum(sl) from # where id=a.id and id0> =a.id0),0)-b.kc)
end as 数量
from # a,@kc b
where a.id=b.id
and isnull((select sum(sl) from # where id=a.id and id0> =a.id0),0)> b.kc
) as t
group by id
--结果
id 平均价
----------- --------------------------
1 5.525641025641
3 35.025316455696
4 37.427419354838
5 23.000000000000
7 32.814504881450
12 837.294743429286
(所影响的行数为 6 行)
[解决办法]
--就这段代码
select d.*,k.kc,0 as 未售出 into #tmp from dj d join kc k on k.id = d.id order by d.id,ywrq desc
declare @kc int,@id int
update t
set @kc =
case
when t.id = @id then @kc-sl
else k.kc-sl
end
,@id = t.id,未售出 = case when @kc > 0 then sl when @kc+sl> 0 then @kc+sl else 0 end
from #tmp t
join kc k on k.id = t.id
select id,convert(numeric(10,2),convert(numeric(10,2),sum((sl-未售出)*dj))/sum(sl-未售出)) as 单价
from #tmp
group by id
drop table #tmp