Moving weighted average Algorithm Using Calculated Columns

Source: Internet
Author: User

CopyCode The Code is as follows: if object_id ('tb') is not null drop table TB
If object_id ('temp ') is not null drop table temp
If object_id ('fun _ nowprice') is not null drop function fun_nowprice
If object_id ('fun _ nowqty ') is not null drop function fun_nowqty
Go

Create Table Tb (
Id int
, Date1 datetime
, Ctype varchar (10)
, Qnt float
, PRI float
)

-- Qnt quantity
-- Pri unit price
Insert TB
Select 0, '2014-1-1 ', 'purchase', 2009 Union all
Select 1, '2014-1-1 ', 'purchase', 2009 Union all
Select 2, '2014-1-2 ', 'shipping', 2009 Union all
Select 3, '2014-1-3 ', 'purchase', 2009 Union all
Select 4, '2014-3-3 ', 'shipping', 2009
Go
-- I want to calculate the cost by moving weighted average

/*
1 cost price after purchase C1 = (10*100 + 50*120)/(10 + 50)
2 cost price after shipment C2 = (10 + 50) * C1-30 * C1)/(10 + 50)-30) = c2
-- That is, the price remains unchanged during shipment.
3 cost price after purchase C3 = (10 + 50)-30) * C2 + 40*130)/(10 + 50)-30 + 40)
-- In other words, the unit price is updated to (total current inventory value + Total Database Value)/total number after warehouse receiving

And so on...
*/

-- After thinking for a long time, I thought we could only use loops, recursion, and cursors, because the price for warehouse picking was calculated based on previous records.
-- Maybe there is a classicAlgorithmWho knows how to teach me or send a link.

-- This function implements recursion in disguise.
Create Function fun_nowprice (@ id int)
Returns numeric (19,6)
As
Begin
Return (select isnull (nowprice, 0) from
(Select max (nowprice) 'nowprice' from temp T1 where ID <@ ID and
Not exists (select 1 from temp where ID> t1.id and ID <@ ID ))
T)
End
Go
-- This function is used for computing convenience.
Create Function fun_nowqty (@ id int)
Returns numeric (19,6)
As
Begin
Return (select isnull (sum (Case ctype when 'purchase 'then qnt else 0-qnt end), 0) from temp where ID <@ ID)
End
Go

-- Create a temporary table that contains all fields involved in the calculation of the original table
Create Table temp (
Id int
, Date1 datetime
, Ctype varchar (10)
, Qnt float
, PRI float
, Nowprice
Case ctype
When 'shipping 'then DBO. fun_nowprice (ID)
Else (DBO. fun_nowprice (ID) * DBO. fun_nowqty (ID) + qnt * Pri)/(DBO. fun_nowqty (ID) + qnt)
End)

Insert into temp
Select * from TB
Order by date1 ASC, Id ASC

Select * from temp

/*
0 00:00:00. 000 purchases 10 100 100
1 00:00:00. 000 purchases 50 120 116.666666666667
2 00:00:00. 000 shipment 30 150 116.666667
3 00:00:00. 000 purchases 40 130 124.285714428571
4 00:00:00. 000 shipment 25 160 124.285714
*/

This statement is not perfect because it sorts records by ID and date, and does not process the inbound and outbound data of the same day. In actual use, you can sort by time-marked fields such as createdate.
--------------------------------------------------------------------------------

The first time I wrote a technical blog, I hope this is a good start. You are welcome to correct my algorithm.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.