x

The ‘FIFO Stock Inventory’ SQL Problem

Phil Factor SQL Speed Phreak Competition: No 2

alt text

This competition is now over, but the winner, Dave, got an Amazon Voucher for $60, and the privilege of being able to display the 'Phil Factor SQL Speed Phreak' award on their own site

It was quite a struggle with some close competition from many of those who participated in this competition. However, Dave came up with a clever solution that produced an FIFO calculation from a million rows in a just a few seconds. The third Phil Factor Speed Phreak competition will soon be held on here. Watch out!


(here is the original preamble.)

This competition is to calculate current items in stock and current stock value in FIFO order.
I'll tell you the business rules for the algorithm and provide a sample cursor-based routine that generates the correct result, but slowly (about 40 minutes).

I have seen many different algorithms to do this and most of them involve a cursor. Can it be done more quickly without, or even with, a cursor? In other words, what is the fastest way in SQL Server (any version) to provide this stock inventory report?
It is a reasonable request. We have a stock transaction list (1,000,001 records, 17.6MB zipped) with 15,002 unique articles and we need to do a stock inventory report that gives the current breakdown of the ArticleID, the number of items in stock and the current stock value according to FIFO rules. The list should be in ArticleID order. The sample stock transaction list will include two running totals so you can check the results of your routine. They are NOT part of the competition, and you are not allowed to use them in your calculation.

Beware that the sample file includes the two extra columns; CurrentItems (INT) and CurrentValue (MONEY) !
If you want a smaller subset to test on, import all records and remove all articles but two or three. These are the samples for which all suggestions will be measured with.

The table is in this form (simplified from the way we'd do it in a real system of course).
In this made-up but realistic problem, you have an existing table and database design which you can’t do anything about. (we can all relate to that!) You have an existing cursor-based solution that is taking several minutes to run. (Yes, we all relate to that). The database is performing badly, and you need to take effective steps to remedy this. The only thing you can do is to come up with a better-performing routine. Redesigning the database isn’t an option very often, in the real world, because this requires team sign-in. This is a competition based on the real, sometimes imperfect, world: not an exposition of database design. The point is that we are faced with designs like this and we have to heal them. The competition is all about making stuff go faster.

CREATE TABLE    dbo.Stock
                (
                    StockID INT IDENTITY(1, 1) NOT NULL,
                    ArticleID SMALLINT NOT NULL,
                    TranDate DATETIME NOT NULL,
                    TranCode VARCHAR(3) NOT NULL,
                    Items INT NOT NULL,
                    Price MONEY NULL,
                    CONSTRAINT [PK_Stock] PRIMARY KEY CLUSTERED 
                    (
                        StockID ASC
                    )
                )  

CREATE NONCLUSTERED INDEX IX_Input ON dbo.Stock (TranCode, ArticleID)
--INCLUDE (TranDate, Items, Price) -- Remove comment for SQL Server 2005 and later
--WHERE TranCode IN ('IN', 'RET')   -- Remove comment for SQL Server 2008

CREATE NONCLUSTERED INDEX IX_Output ON dbo.Stock (TranCode, ArticleID)
-- INCLUDE (TranDate, Items) -- Remove comment for SQL Server 2005 and later
--WHERE TranCode = 'OUT'  -- Remove comment for SQL Server 2008

You are welcome to change the two nonclustered indexes to suit your solution. You can download the complete sample data here. I have an idea of my own of the way to do this but I don’t know if it is the fastest.

Explanation of FIFO rules (example, abbreviated)

StockID ArticleID TranDate TranCode Items  Price CurrentItems CurrentValue
   4567     10000 10:45:07 IN         738 245.94          738   181,503.72
  21628     10000 12:05:25 OUT        600                 138    33,939.72
  22571     10000 14:39:27 IN          62 199.95          200    46,336.62
  30263     10000 16:14:13 OUT        165                  35     6,998.25
  42090     10000 18:18:58 RET          5                  40     7,998.00
  58143     10000 20:18:54 IN         500 135.91          540    75,953.00

a) First IN add 738 items (each $245.94) to the stock, for a total of $181,503.72
b) Then we take out 600 items (each 245.94) from the stock, leaving a total of $33,939.72
c) Then we insert 62 items (each 199.95) to the stock, for a total of $46,336.62
d) Then we take out 165 items (138 each 245.94 and 27 each 199.95), leaving a total of $6,998.25
e) Then we return 5 items. We can’t track at which price we took them out; so all returns are priced at the price of the latest ones inserted before the return. Even if there should be items left for the price of 245.94, the returned items are valued for 199.95. After the return, the current stock value is $7,998.00
f) The final insert adds $67,995.00 to the stock value, for a total of $75,953.00

As mentioned before, the CurrentItems and CurrentValue columns in the sample data are only included for you to validate your routines.

Here are some guidelines for your entries:

1) Include a header in your suggestion. Make sure your name and date is present.
2) Include an edition number. First edition is 1. If you later improve your current suggestion post it again as version 2. Example: “Peso 1” and if improved, “Peso 1b”, “Peso 1c” etc.
3) If you are trying a new algorithm, change the edition to “Peso 2”. If you improve this algorithm, change the version to “Peso 2b”, “Peso 2c” etc. This will save Phil hours of work in the test harness!
4) If you create a temp table, make sure you delete it in the script.
5) Keep the order of columns in output as ArticleID, CurrentItems, CurrentValue

I will allow you to use an existing tally number table (make sure it starts with 0). You can use any kind of object for this competition, except SQLCLR. If you are using a fixed tally number table, it has to be named dbo.TallyNumbers and the column named Number.
The time taken for their creation will not be included in the timings. The time measured is the “Main” script/procedure. If you want to call sub-procedures, go ahead.

The winner will be amongst the tied fastest entrants (generally there is a group of these) and it will be the one with the highest number of votes. We'll announce the winner in three week's time on 16th November.

For a starter, here is a common cursor based solution that you will find in production in many places.

CREATE TABLE    #Work
                (
                    RowID INT IDENTITY(1, 1) PRIMARY KEY CLUSTERED,
                    Price MONEY
                )  

DECLARE @ArticleID INT = 1,
        @PrevID INT = 0,
        @TranCode VARCHAR(3),
        @Items INT,
        @Price MONEY,
        @Loop INT = 0,
        @StockID INT,
        @LatestPrice MONEY,
        @Total INT = (SELECT COUNT(*) FROM dbo.Stock)

DECLARE curYak CURSOR FORWARD_ONLY FOR
                SELECT      ArticleID,
                            TranCode,
                            Items,
                            Price,
                            StockID
                FROM        dbo.Stock
                ORDER BY    ArticleID,
                            TranDate

OPEN    curYak

FETCH   NEXT
FROM    curYak
INTO    @ArticleID,
        @TranCode,
        @Items,
        @Price,
        @StockID

WHILE @@FETCH_STATUS = 0
    BEGIN
        IF @ArticleID > @PrevID
            BEGIN
                TRUNCATE TABLE  #Work

                SET @LatestPrice = NULL
            END

        IF @TranCode = 'IN'
            BEGIN
                INSERT  #Work
                        (
                            Price
                        )
                SELECT  @Price
                FROM    dbo.TallyNumbers
                WHERE   Number < @Items

                SET     @LatestPrice = @Price
            END

        IF @TranCode = 'RET'
            INSERT  #Work
                    (
                        Price
                    )
            SELECT  @LatestPrice
            FROM    dbo.TallyNumbers
            WHERE   Number < @Items

        IF @TranCode = 'OUT'
            DELETE  w
            FROM    (
                        SELECT      TOP(@Items)
                                    RowID
                        FROM        #Work
                        ORDER BY    RowID
                    ) AS w

        UPDATE      s
        SET         s.CurrentItems = w.CurrentItems,
                    s.CurrentValue = COALESCE(w.CurrentValue, 0)
        FROM        dbo.Stock AS s
        INNER JOIN  (
                        SELECT  COUNT(*) AS CurrentItems,
                                SUM(Price) AS CurrentValue
                        FROM    #Work
                    ) AS w ON s.StockID = @StockID

        SELECT  @PrevID = @ArticleID,
                @Loop += 1

        IF @Loop % 1000 = 0
            RAISERROR('Now updating record %d of %d.', 10, 1, @Loop, @Total) WITH NOWAIT

        FETCH   NEXT
        FROM    curYak
        INTO    @ArticleID,
                @TranCode,
                @Items,
                @Price,
                @StockID
    END

DROP TABLE  #Work

CLOSE       curYak
DEALLOCATE  curYak

The above is already done. Here is the code to produce the final resultset.

SELECT      ArticleID,
            CurrentItems,
            CurrentValue
FROM        (
                SELECT  ArticleID,
                        CurrentItems,
                        CurrentValue,
                        ROW_NUMBER() OVER (PARTITION BY ArticleID ORDER BY TranDate DESC) AS recID
                FROM    dbo.Stock
            ) AS d
WHERE       recID = 1
ORDER BY    ArticleID

Good luck to you all!

Peter Larsson

more ▼

asked Oct 23 '09 at 03:56 PM in Default

Peso gravatar image

Peso
1.6k 5 6 8

Was there any particular reason you disallowed CLR functionality?

On an initial look, this would be the ideal scenario in which to use a CLR type...
Oct 23 '09 at 04:34 PM Peso
Two things had us made this decision: 1) We have tried CLR before and there was no performance gain. 2) To test and verify, poster need to disclode the source code for the suggestion. But if you have a CLR routine that runs on 15 seconds or less, I think Phil will be interested in testing it.
Oct 23 '09 at 05:08 PM Peso
Questions: 1) Is the Stock Transaction List referred to the one that will be used for Final Evaluation? If not, can you describe the differences? 2) What is the configuration that entries will be (finally) evaluated on?
Oct 23 '09 at 05:09 PM RBarryYoung
Yes, the downloadable file is the data against all suggestions will be measured. Phil will drop or rename the two last columns so that no suggestion can use them. I guess Phil will be using same test harness as last time with 'Subscription List' competition.
Oct 23 '09 at 05:16 PM Peso
Those filtered indexes will only work on SQL Server 2008, right?
Oct 23 '09 at 05:46 PM RBarryYoung
(comments are locked)
10|1200 characters needed characters left

28 answers: sort voted first

Dave Ballantyne - Phil Factor Challenge Entry 3.d

Peso had adding some index hints and shuffled a calculation to my initial 3.a. So this is 3.a(+) with comments

with 3.d doing the TotalStock sum in one operation shaves some more time off

“A problem well stated is a problem half solved” - Charles F. Kettering

Let us think about what has been asked within the challenge.

We have a warehouse which starts empty , the data contains the stock movements in (including returns) and out. The stock movements ALWAYS happen in a first in first out basis. With this we know that if the present stock level (sum(in's) - sum(out's) is 50 that will be the last 50 that have entered the warehouse. The key to this query is to efficiently find the cost of those 50 items.

 CREATE NONCLUSTERED INDEX [IX_Dave_General] ON [dbo].[Stock]  (  [ArticleID] ASC,  [TranDate] DESC,  [TranCode] ASC  )  INCLUDE ( [Items],  [Price]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IX_Dave_Items] ON [dbo].[Stock]
(
        [ArticleID] ASC,
        [TranDate] ASC
)
INCLUDE ( [Items])
WHERE ([TranCode] IN ('IN', 'RET'))
WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]

GO

CREATE NONCLUSTERED INDEX [IX_Dave_Price] ON [dbo].[Stock]
(
        [ArticleID] ASC,
        [TranDate] ASC
)
INCLUDE ( [Price])
WHERE ([TranCode]='IN')
WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON, FILLFACTOR = 100) ON [PRIMARY]

GO

/* Sum up the ins and outs to calculate the remaining stock level */ ;WITH cteStockSum AS (

  SELECT  ArticleID,
        SUM(CASE  WHEN TranCode = 'OUT' THEN 0-Items
                  ELSE Items END) AS TotalStock
        FROM  dbo.Stock
  GROUP BY  ArticleID

), /* Perform a rolling balance ( in reverse order ) through the stock movements in / cteReverseInSum AS ( SELECT s.ArticleID, s.TranDate, ( SELECT SUM(i.Items) FROM dbo.Stock AS i WITH (INDEX (IX_Dave_Items)) WHERE i.ArticleID = s.ArticleID AND i.TranCode IN ('IN', 'RET') AND i.TranDate >= s.TranDate / SELECT SUM(i.Items) FROM dbo.vwStockIn as i with (index (idxStockInArtTrans)) WHERE i.ArticleID = s.ArticleID AND i.TranDate >= s.TranDate*/

       ) AS RollingStock,
       s.Items AS ThisStock
 FROM  dbo.Stock AS s
WHERE  s.TranCode IN ('IN', 'RET')

), /* Using the rolling balance above find the first stock movement in that meets (or exceeds) our required stock level / / and calculate how much stock is required from the earliest stock in */ cteWithLastTranDate AS ( SELECT w.ArticleID, w.TotalStock, LastPartialStock.TranDate, LastPartialStock.StockToUse, LastPartialStock.RunningTotal, w.TotalStock - LastPartialStock.RunningTotal + LastPartialStock.StockToUse AS UseThisStock

  FROM cteStockSum AS w
       CROSS APPLY  ( SELECT TOP(1)  
                             z.TranDate,
                             z.ThisStock AS StockToUse,
                             z.RollingStock AS RunningTotal
                        FROM cteReverseInSum AS z
                       WHERE z.ArticleID = w.ArticleID
                         AND z.RollingStock &gt;= w.TotalStock
                    ORDER BY z.TranDate DESC ) AS LastPartialStock
) /* Sum up the cost of 100% of the stock movements in after the returned stockid and for that stockid we need 'UseThisStock' items' / SELECT y.ArticleID, y.TotalStock as CurrentItems , SUM(CASE WHEN e.TranDate = y.TranDate THEN y.UseThisStock ELSE e.Items END * Price.Price) AS CurrentValue FROM cteWithLastTranDate AS y INNER JOIN dbo.Stock AS e WITH (INDEX (IX_Dave_Items)) ON e.ArticleID = y.ArticleID AND e.TranDate >= y.TranDate AND e.TranCode IN ('IN', 'RET') CROSS APPLY ( / Find the Price of the item in */ SELECT TOP(1)
p.Price FROM dbo.Stock AS p WITH (INDEX (IX_Dave_Price)) WHERE p.ArticleID = e.ArticleID AND p.TranDate <= e.TranDate AND p.TranCode = 'IN' ORDER BY p.TranDate DESC ) AS Price GROUP BY y.ArticleID, y.TotalStock ORDER BY y.ArticleID go
more ▼

answered Nov 05 '09 at 06:03 AM

dave ballantyne gravatar image

dave ballantyne
928 1 1 4

(comments are locked)
10|1200 characters needed characters left

RBarryYoung_1b: Naive, Pure Set-Based Solution

OK, just to get things rolling, here is a straight-forward Set-based solution, with no T-SQL tricks (though there is a very important algorithmic trick in cteOverlapValue).

CREATE PROC SscFIFO_RBarryYoung_1b AS
/*
    Naive pure Set-based solution to SSC FIFO inventory challenge.

    - RBarryYoung, Oct-24, 2009
*/
;WITH
-- the base CTE, (just an easy way to Alias to other version of the test table)
 cteStock AS ( SELECT * FROM Stock )
-- Just the 'IN' records:
, cteStockIN AS ( 
    SELECT * 
    FROM cteStock
    WHERE TranCode = 'IN' 
    )
-- Fill in the Last IN records date for lookups later:
, cteLastINDate AS (
    SELECT * 
    , CASE TranCode WHEN 'IN' THEN TranDate
        ELSE (
            SELECT MAX(s3.TranDate)
            FROM cteStockIN s3
            WHERE s.ArticleID = s3.ArticleID
            AND  s.TranDate > s3.TranDate
            ) END AS LastINDate
    FROM cteStock s
    )
-- Carry-forward the last price for RET records:
, cteLastPriceDate AS (
    SELECT * 
    , COALESCE(Price, (
        SELECT Price 
        FROM cteStockIN s2
        WHERE s.ArticleID = s2.ArticleID
        AND  s.LastINDate =  s2.TranDate
        )) AS LastPrice
    FROM cteLastINDate s
    )
-- Build running sums of InItems and OutItems (same column, but separate counts)
, cteRunningItems AS (
    SELECT *
    , CASE WHEN TranCode IN('IN','RET') THEN (
            SELECT SUM(Items) 
            FROM cteLastPriceDate p2
            WHERE p2.TranCode IN('IN','RET')
            AND   p2.ArticleID = p.ArticleID
            AND   p2.TranDate <= p.TranDate
            )
        WHEN TranCode = 'OUT' THEN (
            SELECT SUM(Items) 
            FROM cteLastPriceDate p2
            WHERE p2.TranCode = 'OUT'
            AND   p2.ArticleID = p.ArticleID
            AND   p2.TranDate <= p.TranDate
            )
        END AS InOutItems
    , CASE WHEN TranCode IN('IN','RET') THEN (
            SELECT SUM(p2.Items*p2.LastPrice) 
            FROM cteLastPriceDate p2
            WHERE p2.TranCode IN('IN','RET')
            AND   p2.ArticleID = p.ArticleID
            AND   p2.TranDate <= p.TranDate
            ) END AS TotalValueIN
    FROM cteLastPriceDate p
    )
-- Map each OUT record to the (IN,RET) records that it will be consuming:
, cteOutMapIn AS (
    SELECT t1.*
    , t2.Items AS InItems
    , t2.LastPrice AS InPrice
    , t2.InOutItems AS TotalIn
    FROM cteRunningItems t1
    LEFT JOIN cteRunningItems t2
        ON  t1.ArticleID = t2.ArticleID
        AND t1.TranCode = 'OUT'
        AND t2.TranCode IN('IN','RET')
        AND t1.InOutItems-t1.Items <= t2.InOutItems
        AND t1.InOutItems >= t2.InOutItems-t2.Items
    )
-- Calculate the OUT consumed values, 
--based on how the OUT items overlap the (IN,RET) items:
, cteOverlapValue AS (
    SELECT *
    , CASE WHEN TranCode = 'OUT' THEN
        CASE 
            WHEN InOutItems >= TotalIn  AND (InOutItems-Items) <= (TotalIn-InItems)
                THEN InItems            -- OUT consumes all the INs
            WHEN InOutItems >= TotalIn  -- OUT consumes only the upper part of the INs
                THEN (TotalIn - (InOutItems-Items)) 
            WHEN (InOutItems-Items) <= (TotalIn-InItems)    -- OUT consumes the lower INs
                THEN (InOutItems - (TotalIn-InItems)) 
            ELSE Items  END             -- All OUTs consume an interior slice of IN
      END AS ItemsConsumed
    , CASE WHEN TranCode = 'OUT' THEN
        CASE
            WHEN InOutItems >= TotalIn  AND (InOutItems-Items) <= (TotalIn-InItems)
                THEN InItems * InPrice  -- OUT consumes all the INs
            WHEN InOutItems >= TotalIn  -- OUT consumes only the upper part of the INs
                THEN (TotalIn - (InOutItems-Items)) * InPrice
            WHEN (InOutItems-Items) <= (TotalIn-InItems)    -- OUT consumes the lower INs
                THEN (InOutItems - (TotalIn-InItems)) * InPrice
            ELSE Items * InPrice END    -- All OUTs consume an interior slice of IN
      END AS ConsumedValue
    FROM cteOutMapIn 
    )
-- Total everything up for each ArticleID:
, cteArticleTotals AS (
    SELECT t1.ArticleID
    , SUM( CASE WHEN TranCode='OUT' THEN -ItemsConsumed ELSE Items END ) AS CurrentItems
    , MAX( TotalValueIN ) - COALESCE(SUM( ConsumedValue ),0) AS CurrentValue
    FROM cteOverlapValue t1
    GROUP BY t1.ArticleID
    )
-- Output the results:
SELECT *
FROM cteArticleTotals t1
ORDER BY t1.ArticleID

Performance is not good, 9 seconds on the 5000 row (75 Articles) test set, and about 18 minutes on the full set.

Oops! I got those numbers completely wrong,...

In reality, performance is OK, but not great. it takes 9 seconds on 100,000 rows (about 1500 Articles) and 90 seconds on the full set. Not bad for virtually no optimizations.


1b: OK, I have made the corrections Requested by Peso (ORDER BY and Fixed NULLS on Article 25001). My current timings for it are 9sec on 100k rows and 95sec on 1M rows.

more ▼

answered Oct 24 '09 at 11:49 PM

RBarryYoung gravatar image

RBarryYoung
782 5 5 8

Excellent suggestion Barry! Great to see a pure set-based solution. However, there are two things I miss. 1) Articles need to be sorted by ArticleID. 2) Last ArticleID 25001 reports correct Items but wrong Value.
Oct 25 '09 at 04:43 AM Peso
OK, thanks Peso. Obviously I can fix (1) right now ... (:-)), (2) is going to take some investigation though.
Oct 25 '09 at 09:58 AM RBarryYoung
The procedure header says 1a, but the code (I think) is 1b?
Oct 26 '09 at 08:42 AM Peso
Right. I'm not very good with directions (that's why I need computers :-)). Fixed now.
Oct 26 '09 at 11:26 AM RBarryYoung
Ok, sorry for the really slow comment, but this was the one showing incorrect values for me - first article, value shows as 0.0, should be 15446.39. Sorry for the delay!
Oct 30 '09 at 06:23 PM Matt Whitfield ♦♦
(comments are locked)
10|1200 characters needed characters left
more ▼

answered Oct 24 '09 at 05:22 PM

Matt Whitfield gravatar image

Matt Whitfield ♦♦
29.4k 61 65 87

It's a great entry! However, have you disabled parallelism manually?
Oct 24 '09 at 05:48 PM Peso
I did the MAXDOP 1 because it was a typical quirky update safety addition - I did remove it at one point but didn't find it make any difference to the performance overall...
Oct 24 '09 at 05:56 PM Matt Whitfield ♦♦
It's a great learning experience for me! Thank you very much. Until now, I have only dealt with aggregate SQLCLR. Take a look here http://www.developerworkshop.net/software.html
Oct 24 '09 at 06:17 PM Peso
(comments are locked)
10|1200 characters needed characters left

Not finished yet , i think there is some more to come. Could you tell me what the performance is like against the 'test' system ? Just for reference i stopped Barry's running after 5 Minutes , this completes in a little over a minute.

drop table #inData go drop table #outData go Create table #InData( ArticleId integer not null, StockId integer not null, Items integer not null, Price money not null, RollingCount integer null) go Create unique clustered index #idxinData on #InData(ArticleId,StockID) go

Create table #OutData( ArticleId integer not null, StockId integer not null, Items integer not null, RollingCount integer null) go Create unique clustered index #idxoutData on #outData(ArticleId,StockID) go Drop Table #OutStockAdjustment go

Create Table #OutStockAdjustment ( ArticleId integer, OutStockId Integer, ItemsOut integer, PriceTotal money )

go

insert into #InData(ArticleId,StockId,Items,Price,RollingCount) select Stock.ArticleId,Stock.StockId,Stock.Items,Price = isnull(Stock.Price,RetPrice.Price),NULL from dbo.Stock outer apply (Select top 1 Price from dbo.Stock innerstock where InnerStock.StockId < Stock.StockId and InnerStock.ArticleID = Stock.ArticleId and TranCode ='In' and Stock.TranCode = 'Ret' order by stockid desc) as RetPrice

where TranCode in( 'IN','ret') --and ArticleId = 10000 go Declare @RollingCount integer Declare @RollingBalance money Declare @LastArticleID integer Select @LastArticleID = -1 Update #InData set @RollingCount = case when @LastArticleID <> ArticleId then Items else Items +@RollingCount end, @RollingBalance = case when @LastArticleID <> ArticleId then Price * Items else (Price * Items)+@RollingBalance end, RollingCount = @RollingCount, @LastArticleId = ArticleId from #InData go insert into #OutData(ArticleId,StockId,Items,RollingCount) Select ArticleId,StockId , Items ,Null from dbo.Stock where TranCode ='Out' go Declare @RollingCount integer Declare @LastArticleID integer Select @LastArticleID = -1 Update #OutData set @RollingCount = case when @LastArticleID <> ArticleId then Items else Items +@RollingCount end, RollingCount = @RollingCount, @LastArticleId = ArticleId from #OutData go delete from #OutStockAdjustment go with CteIndata(ArticleId,StockId,Items,Price,Item_id_lo,Item_id_Hi) as ( Select ArticleId,StockID,Items,Price,RollingCount - Items,RollingCount from #InData ) , CteOutdata(ArticleId,StockId,Items,Item_id_lo,Item_id_Hi) as ( Select ArticleId,StockID,Items,RollingCount - Items,RollingCount from #OutData ) CteAudit as ( select CteIndata.ArticleId, CteInData.Price, CteIndata.StockId as InStockId, CteOutdata.StockId as OutStockId, case when CteIndata.Item_Id_Lo > CteOutdata.Item_Id_Lo then CteIndata.Item_Id_Lo else CteOutdata.Item_Id_Lo end as LoItemId, case when CteIndata.Item_Id_Hi < CteOutdata.Item_Id_Hi then CteIndata.Item_Id_Hi else CteOutdata.Item_Id_Hi end as HiItemId from CteIndata full join CteOutdata on CteIndata.ArticleId = CteOutdata.ArticleId and( CteIndata.Item_Id_Lo between CteOutdata.Item_Id_lo and CteOutdata.Item_Id_hi or CteOutdata.Item_Id_Lo between CteIndata.Item_Id_lo and CteIndata.Item_Id_hi)

)

insert #OutStockAdjustment(ArticleId,OutStockId,ItemsOut,PriceTotal) Select ArticleId,OutStockId,sum(HiItemId-LoItemId)-1,SUM(Price * (HiItemId-LoItemId)-1) from CteAudit GROUP by ArticleId,OutStockId

go drop table #Finalize go Create Table #Finalize( StockId integer not null, ArticleId integer not null, TranCode varchar(3) not null, Items integer not null, Price money null, RollingCount integer null, RollingBalance money null, BalanceDecr money null ) go create unique clustered index #idxFinalize on #Finalize(ArticleId,StockId) go insert into #Finalize (StockId,ArticleId,TranCode,Items,Price,RollingCount,RollingBalance,BalanceDecr) select Stock.StockID, Stock.ArticleID, Stock.TranCode, Stock.Items, #InData.Price, null, null, Pricetotal from Stock left join #InData on #InData.ArticleId = Stock.ArticleId and #InData.StockId = Stock.StockId left join #OutStockAdjustment on #OutStockAdjustment.ArticleId = Stock.articleid and #OutStockAdjustment.OutStockId = Stock.StockId go

go declare @RollingCount integer Declare @RollingBalance money declare @LastArticleId integer Select @LastArticleId =0 Select @RollingCount=0 select @RollingBalance = 0 update #Finalize set @RollingCount = case when @LastArticleId <> ArticleId then 0 else @RollingCount end + Case when TranCode = 'Out' then 0-Items else items end, @RollingBalance = ((isnull(Items,0)*isnull(Price,0))-ISNULL(BalanceDecr,0))+ case when @LastArticleId <> ArticleId then 0 else @RollingBalance end, RollingCount = @RollingCount, RollingBalance = @RollingBalance, @LastArticleId = ArticleId from #Finalize

go select StockId , ArticleId, TranCode, Items , Price , RollingCount, RollingBalance from #Finalize where ArticleId = ArticleID order by ArticleId,StockId
more ▼

answered Oct 26 '09 at 12:49 PM

dave ballantyne gravatar image

dave ballantyne
928 1 1 4

Dave: Five Minutes?!? are you sure that you have Peso's indexes on the table? My sProc uses them pretty heavily...
Oct 26 '09 at 02:14 PM RBarryYoung
Dave - 51 seconds on my box, but the result set isn't in the right format, and the values shown in that result set don't match the provided data...
Oct 26 '09 at 02:27 PM Matt Whitfield ♦♦
Dave: It faster than mine on my machine (82 sec vs. 95 sec) but it is not producing the correct output for me. Instead of a summary by ArticleID returning (ArticleID, CurrentItems, CurrentValues) sorted by ArticleID, it's just returning the entire stock table with the columns RollingCount and RollingBalance appended. Is it possible that you posted the wrong version of your script?
Oct 26 '09 at 02:29 PM RBarryYoung
@Barry - yours runs at 1:24 on my box... although that doesn't seem to get the correct values either?
Oct 26 '09 at 02:34 PM Matt Whitfield ♦♦
Dave, I can't parse the query. Put a comma before CteAudit. Also, I get 1,000,001 records in return, where I should get 15,002 records instead.
Oct 26 '09 at 06:35 PM Peso
(comments are locked)
10|1200 characters needed characters left

RBarryYoung_2d: Pure Set-Based, summary calcs

OK, similar to my first entry, but using a smarter way to calculate the final CurrentValues:


New version, 2c (don't ask about 2b :-)), primarily leveraging a new index....


OK, now we're really cooking with gas! Andriy's version spurred me to rethink certain parts of the query, resulting in a smaller and even fast version that finally uses the indexes the way that I always wanted it to.

First the new index to create before running:

    CREATE NONCLUSTERED INDEX IX_StockIN_RBY1 
    ON dbo.Stock (ArticleID, TranDate) 
    INCLUDE (Price) 
    WHERE TranCode = 'IN'

Andthe new version:

CREATE PROC [dbo].[SscFIFO_RBarryYoung_2d] AS
/*
    Set-based solution to SSC FIFO inventory challenge.

    - RBarryYoung, Oct-26, 2009
    2009-10-26, RBarryYoung, smarter method, 3x faster: (shorter too)
                    use CurrentItems to calc CurrentVaules backwards
    2009-10-27, Use indexes that are better for this query, 
                    and fix LastPrice dual lookup problem
*/
-- Make sure the index exists:
IF NOT EXISTS(SELECT * FROM sys.indexes WHERE name='IX_StockIN_RBY1') 
    EXEC('CREATE NONCLUSTERED INDEX IX_StockIN_RBY1 ON dbo.Stock (ArticleID, TranDate) INCLUDE (Price) WHERE TranCode = ''IN''')

;WITH
-- the base CTE, (just an easy way to Alias to other version of the test table)
 cteStock AS ( SELECT ArticleID, TranDate, TranCode, Items, Price FROM StockX ) --WITH( INDEX (IX_All_RBY2) ) )
-- Fill in the date & price from Last IN record:
, cteLastPriceDate AS (
    SELECT * 
    FROM cteStock s
    )
-- Build running sums of InItems and OutItems 
, cteRunningItems AS (
    SELECT *
    , CASE TranCode WHEN 'IN' THEN Price 
        WHEN 'RET' THEN (
            SELECT TOP (1) s3.Price
            FROM cteStock s3 
            WHERE s3.ArticleID = t.ArticleID
            AND   s3.TranCode = 'IN' 
            AND   s3.TranDate < t.TranDate
            ORDER BY s3.TranDate DESC
            ) END AS LastPrice
    , CASE WHEN TranCode IN('IN','RET') THEN (
            SELECT SUM(Items) 
            FROM cteLastPriceDate p2
            WHERE p2.TranCode IN('IN','RET')
            AND   p2.ArticleID = t.ArticleID
            AND   p2.TranDate <= t.TranDate
            )
        END AS CurrentInItems
    FROM cteLastPriceDate t
    )
-- Total everything up for each ArticleID:
, cteArticleTotals AS (
    SELECT ArticleID
    , MAX(CurrentInItems) - MAX(COALESCE(TotalOutItems,0)) AS CurrentItems
    , SUM(CASE 
        WHEN CurrentInItems-Items >= COALESCE(TotalOutItems,0) THEN Items*LastPrice
        WHEN CurrentInItems >= TotalOutItems THEN (CurrentInItems-COALESCE(TotalOutItems,0))*LastPrice
        ELSE 0 END
        ) AS CurrentValue
    FROM cteRunningItems t
    CROSS APPLY (
        SELECT  SUM(s.Items) AS TotalOutItems
        FROM cteStock s
        WHERE   s.TranCode = 'OUT'
        AND     s.ArticleID = t.ArticleID
        ) s
    WHERE TranCode IN('IN','RET')
    GROUP BY ArticleID
    )
-- Output the results:
SELECT *
FROM cteArticleTotals
ORDER BY ArticleID  

/* -- This should be run after:
    DROP INDEX IX_StockIN_RBY1 ON dbo.Stock;
*/

This is twice as fast as v2c (7 sec) on my machine.

more ▼

answered Oct 26 '09 at 03:06 PM

RBarryYoung gravatar image

RBarryYoung
782 5 5 8

It is getting better and better!
Oct 27 '09 at 03:34 PM Peso
Barry - this is excellent - 21.952 seconds on my machine (excluding the index creation)... Results are A-OK on this one for me - did you still want me to post what was wrong with the other one (sorry only just saw that...)
Oct 27 '09 at 03:51 PM Matt Whitfield ♦♦
Matt: yes, please do. Make sure to not which version it is. -thnx!
Oct 27 '09 at 04:05 PM RBarryYoung
Barry, you are getting closer!
Oct 27 '09 at 04:17 PM Peso
thanks folks. :-) But I am not sure that I can squeeze anymore out of it...
Oct 27 '09 at 04:50 PM RBarryYoung
(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x977
x341
x8
x7
x5

asked: Oct 23 '09 at 03:56 PM

Seen: 27003 times

Last Updated: Nov 14 '09 at 01:55 PM