这是功能:
CREATE FUNCTION dbo.agedItemResult(@ILC VARCHAR(30))
RETURNS @AgedTable TABLE (
ItemID INT,
ItemLookupCode VARCHAR(30),
[Month] INT,
[Year] INT,
Aged INT,
Debug01 VARCHAR(MAX),
Debug02 VARCHAR(MAX),
Debug03 VARCHAR(MAX),
Debug04 VARCHAR(MAX),
Debug05 VARCHAR(MAX)
)
AS
BEGIN
DECLARE @TotalSold INT
DECLARE @AdjustmentQty INT
DECLARE @FirstRcvdStart DATETIME
DECLARE @FirstRcvdEnd DATETIME
--DECLARE @ILC VARCHAR(MAX)
--DECLARE @ItemID INT
DECLARE @MovingQty INT
DECLARE @MovingMonthStart DATETIME
DECLARE @MovingMonthEnd DATETIME
DECLARE @CurrentMonthStart DATETIME
DECLARE @CurrentMonthEnd DATETIME
DECLARE @CurrentRcvd INT
DECLARE @NumMonths INT
DECLARE @AgeReached INT
/*DECLARE @AgedTable TABLE ( ItemID INT,
ItemLookupCode VARCHAR(MAX),
[Month] INT,
[Year] INT,
Aged INT,
Debug01 VARCHAR(MAX),
Debug02 VARCHAR(MAX),
Debug03 VARCHAR(MAX),
Debug04 VARCHAR(MAX),
Debug05 VARCHAR(MAX)
)*/
--SET @ILC = '21208641'
SET @TotalSold = (SELECT SUM(Sold) FROM JSInventorySnapShot WHERE ItemLookupCode = @ILC)
SET @AdjustmentQty = (SELECT SUM(HQAdjustment) FROM JSInventorySnapShot WHERE ItemLookupCode = @ILC)
SET @FirstRcvdStart = (SELECT TOP 1 FirstTransferred FROM JSInventorySnapShot ORDER BY FirstTransferred ASC)
SET @FirstRcvdEnd = (SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0, @FirstRcvdStart)+1,0)))
SET @CurrentMonthStart = (SELECT DATEADD(s,0,DATEADD(mm, DATEDIFF(m,0, GETDATE()),0)))
SET @CurrentMonthEnd = (SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0, GETDATE())+1,0)))
SET @NumMonths = (SELECT CASE
WHEN DATEPART(DAY, @FirstRcvdStart) > DATEPART(DAY, @CurrentMonthEnd)
THEN DATEDIFF(MONTH, @FirstRcvdStart, @CurrentMonthEnd) - 1
ELSE DATEDIFF(MONTH, @FirstRcvdStart, @CurrentMonthEnd)
END) + 1
SET @MovingQty = @TotalSold
SET @CurrentRcvd = ISNULL((SELECT SUM(Rcvd) FROM JSInventorySnapShot WHERE ItemLookupCode = @ILC AND [Month] = Month(@FirstRcvdStart)), 0) + @AdjustmentQty
SET @MovingMonthStart = @FirstRcvdStart
SET @MovingMonthEnd = @FirstRcvdEnd
SET @MovingQty = @MovingQty - @CurrentRcvd
SET @AgeReached = 0
WHILE(@NumMonths > 0)
BEGIN
IF (@CurrentRcvd = 0)
BEGIN
INSERT INTO @AgedTable (ItemID, ItemLookupCode, [Month], [Year], Aged, Debug01)
SELECT (SELECT TOP 1 ItemID FROM JSInventorySnapShot WHERE ItemLookupCode = @ILC),
@ILC,
MONTH(@MovingMonthStart),
YEAR(@MovingMonthStart),
0,
@AdjustmentQty
END
ELSE
BEGIN
IF(@MovingQty > 0)
BEGIN
INSERT INTO @AgedTable (ItemID, ItemLookupCode, [Month], [Year], Aged, Debug01)
SELECT (SELECT TOP 1 ItemID FROM JSInventorySnapShot WHERE ItemLookupCode = @ILC),
@ILC,
MONTH(@MovingMonthStart),
YEAR(@MovingMonthStart),
0,
@AdjustmentQty
END
ELSE
BEGIN
IF (@AgeReached = 1)
BEGIN
INSERT INTO @AgedTable (ItemID, ItemLookupCode, [Month], [Year], Aged)
SELECT (SELECT TOP 1 ItemID FROM JSInventorySnapShot WHERE ItemLookupCode = @ILC),
@ILC,
MONTH(@MovingMonthStart),
YEAR(@MovingMonthStart),
@CurrentRcvd
END
ELSE
BEGIN
INSERT INTO @AgedTable (ItemID, ItemLookupCode, [Month], [Year], Aged)
SELECT (SELECT TOP 1 ItemID FROM JSInventorySnapShot WHERE ItemLookupCode = @ILC),
@ILC,
MONTH(@MovingMonthStart),
YEAR(@MovingMonthStart),
(-1 * @MovingQty)
SET @AgeReached = 1
END
END
END
SET @NumMonths = @NumMonths - 1
SET @MovingMonthStart = (SELECT DATEADD(s,0,DATEADD(mm, DATEDIFF(m,0, @MovingMonthStart)+1,0)))
SET @MovingMonthEnd = (SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0, @MovingMonthEnd)+2,0)))
SET @CurrentRcvd = ISNULL((SELECT SUM(Rcvd) FROM JSInventorySnapShot WHERE ItemLookupCode = @ILC AND [Month] = Month(@MovingMonthStart)), 0)
SET @MovingQty = @MovingQty - @CurrentRcvd
END
RETURN
END
简而言之,这就是它的作用:
首先,我们找到第一个收到或移动的物品,并使用日期作为开始日期。然后我们找到该商品的总销售额,并开始从销售额中减去收货金额。剩余部分被视为老化(未售出),并在下个月进行评估。然后我们移至下个月并重复该过程,直到我们达到负数或 0。如果我们达到负数,则意味着我们已经达到了一个月,我们订购的数量超过了迄今为止的销售量,并且该数量被视为已老化(不出售)。如果还有更多月份需要评估,则默认情况下,任何进一步收到的金额都将被视为已过期,因为它显然未售出。
以下是用于评估的表中的示例数据:
ID ItemID ItemLookupCode HQAdjustment FirstTransferred Rcvd RcvdDateEarly RcvdDateLate Sold SoldDateEarly SoldDateLate Sales Month Year StartOfMonthQty EndOfMonthQty
17350 188993 21208641 0 2012-04-05 13:29:14.000 48 2012-04-27 11:00:40.100 2012-04-27 11:00:40.100 40 2012-04-05 13:29:14.000 2012-04-30 12:50:13.000 75.20 4 2012 0 8
34427 188993 21208641 0 2012-04-05 13:29:14.000 120 2012-05-22 10:14:40.213 2012-05-22 10:14:40.213 24 2012-05-04 17:42:12.000 2012-05-29 10:22:10.000 44.72 5 2012 8 104
48437 188993 21208641 0 2012-04-05 13:29:14.000 0 NULL NULL 12 2012-06-02 16:24:45.000 2012-06-21 11:15:05.000 22.36 6 2012 104 92
62369 188993 21208641 0 2012-04-05 13:29:14.000 60 2012-07-16 12:57:33.330 2012-07-16 12:57:33.330 9 2012-07-11 14:42:01.000 2012-07-25 14:58:41.000 16.22 7 2012 92 143
75781 188993 21208641 0 2012-04-05 13:29:14.000 0 NULL NULL 2 2012-08-01 12:56:10.000 2012-08-14 19:01:00.000 4.01 8 2012 143 141
这是我对上述数据的函数的结果:
ItemLookupCode Month Rcvd Sold Aged
21208641 4 48 40 0
21208641 5 120 24 81
21208641 6 0 12 0
21208641 7 60 9 60
21208641 8 0 2 0
这告诉我,我们卖掉了四月份收到的所有东西。5 月再次订购,卖出了一些,但仍有剩余库存(老化)。然后,在 7 月份没有售出库存的情况下,我们再次订购了更多库存,因此该库存被自动视为已老化。
这个函数的目的是停止上面发生的事情。这是订购具有老化(未售出)库存的物品。它还用于评估一个部门的老化程度,以及我们是否应该考虑开始销售以摆脱老化的物品。
问题是我们要评估的项目不少于 40,000 个。此功能适用于单个项目,但 100 个项目需要 30 秒。我希望有人能长期来看我的功能并意识到我正在以完全错误的方式进行操作。如果我可以完成内联计算并结转剩余部分,那就太好了。
我基本上需要简化的伪代码:
Foreach (Month2Date)
If AllInventoryAged = 1 Then //This says we've reached our sales:rcved thresh-hold and all received inventory is aged from here on out.
ThisMonthAged = RcvedForMonth
Else
TotalSales - RcvedForMonth = Aged
If Aged < 0 Then
ThisMonthAged = Aged * -1 //We received more than sold so it's negative and we have hit our sales:rcved thresh-hold.
AllInventoryAged = 1 //Anything received from this point on is aged.
Else
ThisMonthAged = 0 //We received less this month then sold. A good thing.
TotalSales = Aged //We carry on the remainder for further evaluation
我希望这次我已经彻底解释了自己。抱歉这么久。我真的很想涵盖我在上一篇文章中遇到的所有问题。