Logo

SQL Server FIFOtvf Function

Updated 2023-10-12 12:36:03.583000

Description

Use the table-valued function FIFOtvf to calculate running FIFO (First In, First Out) values in an ordered resultant table. FIFOtvf calculates balances for each value from the first value to the last value in the ordered group or partition. FIFOtvf returns:

    •     quantity on-hand

    •     inventory cost

    •     cost of goods sold

    •     gross margin on sale

    •     gross margin percentage

    •     average inventory price

    •     last inventory price

    •     cumulative cost of goods sold

    •     cumulative gross margin on sale

    •     cumulative gross margin percentage.

FIFOtvf supports both long and short inventory positions. In other words, if the quantity on hand falls below the zero, FIFOtvf calculates from the last sale or withdrawal transaction, rather than from the last purchases or additions to inventory.

FIFOtvf assumes that the quantity, i.e., the number of units of the transaction, and the monetary value of the transaction have the same sign. FIFOtvf adds NULL values to the inventory at the last price.

FIFOtvf requires monotonically ascending row numbers within a partition.

FIFOtvf resets all calculated values when the row number value is 1.

Syntax

SELECT * FROM [westclintech].[wct].[FIFOtvf] (
   <@DataQuery, nvarchar(max),>)

Arguments

@DataQuery

An SQL statement which returns a resultant table containing the unique transaction identifier, ROW_NUMBER(), quantity and amount.

Return Type

table

colNamecolDatatypecolDesc
IDsql_variantunique transaction identifier
QTYfloatinventory quantity
EBfloatinventory value
GMfloatgross margin on sale
COGSfloatcost of goods sold
UPfloataverage inventory price per unit
LPfloatlast item added price
COGSCfloatcumulative cost of goods sold
GMCfloatcumulative gross margin
GMPfloatgross margin percentage
GMPCfloatcumulative gross margin percentage

Remarks

If the 3rd column (quantity) of the resultant table from @DataQuery contains NULL an error will be generated.

This function relies on the SQL Server ROW_NUMBER() function and expects results to be returned in ascending ROW_NUMBER() order within a PARTITION.

When ROW_NUMBER() = 1 all inventory values are re-initialized.

Examples

Example #1

In the following examples, we calculate FIFO inventory values for stock trades in the ABC, XYZ, and GHI companies. We will create a temporary table, #c and populate it with some data. We can be either short or long the shares at any point in time.

--Create the temporary table
CREATE TABLE #c
(
    trn int,
    sym char(3),
    tDate date,
    qty money,
    price_unit money,
    price_extended money,
    PRIMARY KEY (trn)
);
--Populate the table with some data
INSERT INTO #c
VALUES
(01131019, 'XYZ', '2013-10-19', 424, 25.13, 10655.12);
INSERT INTO #c
VALUES
(02130617, 'ABC', '2013-06-17', 313, 12.93, 4047.09);
INSERT INTO #c
VALUES
(03130308, 'ABC', '2013-03-08', -157, 13.17, -2067.69);
INSERT INTO #c
VALUES
(04130516, 'GHI', '2013-05-16', 160, 34.48, 5516.8);
INSERT INTO #c
VALUES
(05130706, 'XYZ', '2013-07-06', -170, 23.46, -3988.2);
INSERT INTO #c
VALUES
(06130924, 'GHI', '2013-09-24', 328, 34.95, 11463.6);
INSERT INTO #c
VALUES
(07130722, 'ABC', '2013-07-22', 599, 13.65, 8176.35);
INSERT INTO #c
VALUES
(08131231, 'ABC', '2013-12-31', -145, 13.19, -1912.55);
INSERT INTO #c
VALUES
(09131025, 'XYZ', '2013-10-25', -153, 24.31, -3719.43);
INSERT INTO #c
VALUES
(10130908, 'ABC', '2013-09-08', -386, 13.65, -5268.9);
INSERT INTO #c
VALUES
(11130906, 'XYZ', '2013-09-06', -13, 23.97, -311.61);
INSERT INTO #c
VALUES
(12130621, 'ABC', '2013-06-21', -326, 12.73, -4149.98);
INSERT INTO #c
VALUES
(13131221, 'GHI', '2013-12-21', 72, 34.38, 2475.36);
INSERT INTO #c
VALUES
(14130705, 'XYZ', '2013-07-05', -277, 25.01, -6927.77);
INSERT INTO #c
VALUES
(15130307, 'GHI', '2013-03-07', 559, 35.21, 19682.39);
INSERT INTO #c
VALUES
(16131107, 'ABC', '2013-11-07', 27, 12.68, 342.36);
INSERT INTO #c
VALUES
(17130924, 'GHI', '2013-09-24', -291, 35.69, -10385.79);
INSERT INTO #c
VALUES
(18140125, 'GHI', '2014-01-25', -78, 35.46, -2765.88);
INSERT INTO #c
VALUES
(19130516, 'XYZ', '2013-05-16', 315, 23.57, 7424.55);
INSERT INTO #c
VALUES
(20130518, 'ABC', '2013-05-18', 298, 13.23, 3942.54);
INSERT INTO #c
VALUES
(21131103, 'XYZ', '2013-11-03', -326, 23.24, -7576.24);
INSERT INTO #c
VALUES
(22131012, 'XYZ', '2013-10-12', 596, 23.16, 13803.36);
INSERT INTO #c
VALUES
(23130619, 'XYZ', '2013-06-19', 296, 23.46, 6944.16);
INSERT INTO #c
VALUES
(24130418, 'XYZ', '2013-04-18', 275, 24.83, 6828.25);
INSERT INTO #c
VALUES
(25130408, 'ABC', '2013-04-08', 298, 12.98, 3868.04);
INSERT INTO #c
VALUES
(26130320, 'ABC', '2013-03-20', -92, 13.64, -1254.88);
INSERT INTO #c
VALUES
(27130906, 'XYZ', '2013-09-06', -147, 23.81, -3500.07);
INSERT INTO #c
VALUES
(28131209, 'XYZ', '2013-12-09', 315, 24.46, 7704.9);
INSERT INTO #c
VALUES
(29130602, 'XYZ', '2013-06-02', 114, 24.29, 2769.06);
INSERT INTO #c
VALUES
(30130519, 'XYZ', '2013-05-19', 467, 23.15, 10811.05);
INSERT INTO #c
VALUES
(31140205, 'XYZ', '2014-02-05', 42, 24.39, 1024.38);
INSERT INTO #c
VALUES
(32130310, 'ABC', '2013-03-10', -63, 12.61, -794.43);
INSERT INTO #c
VALUES
(33140102, 'XYZ', '2014-01-02', -196, 22.98, -4504.08);
INSERT INTO #c
VALUES
(34130507, 'XYZ', '2013-05-07', 55, 23.43, 1288.65);
INSERT INTO #c
VALUES
(35130321, 'XYZ', '2013-03-21', 275, 24.83, 6828.25);
INSERT INTO #c
VALUES
(36130917, 'XYZ', '2013-09-17', 92, 24.6, 2263.2);
INSERT INTO #c
VALUES
(37130220, 'XYZ', '2013-02-20', 528, 23.54, 12429.12);
INSERT INTO #c
VALUES
(38130311, 'XYZ', '2013-03-11', -193, 24.9, -4805.7);
INSERT INTO #c
VALUES
(39130908, 'ABC', '2013-09-08', 490, 12.69, 6218.1);
INSERT INTO #c
VALUES
(40131013, 'XYZ', '2013-10-13', 359, 23.91, 8583.69);
INSERT INTO #c
VALUES
(41130310, 'ABC', '2013-03-10', 463, 13.38, 6194.94);
INSERT INTO #c
VALUES
(42131011, 'XYZ', '2013-10-11', -250, 23.12, -5780);
INSERT INTO #c
VALUES
(43130521, 'GHI', '2013-05-21', -174, 34.2, -5950.8);
INSERT INTO #c
VALUES
(44130227, 'XYZ', '2013-02-27', 357, 22.86, 8161.02);
INSERT INTO #c
VALUES
(45131030, 'XYZ', '2013-10-30', -350, 23.36, -8176);
INSERT INTO #c
VALUES
(46130301, 'ABC', '2013-03-01', 157, 13.01, 2042.57);
INSERT INTO #c
VALUES
(47130619, 'XYZ', '2013-06-19', 413, 25.18, 10399.34);
INSERT INTO #c
VALUES
(48130430, 'ABC', '2013-04-30', 229, 13.32, 3050.28);
INSERT INTO #c
VALUES
(49130508, 'ABC', '2013-05-08', 238, 12.79, 3044.02);
INSERT INTO #c
VALUES
(50131103, 'GHI', '2013-11-03', -246, 35.61, -8760.06);
INSERT INTO #c
VALUES
(51131206, 'GHI', '2013-12-06', 85, 33.64, 2859.4);
INSERT INTO #c
VALUES
(52131014, 'GHI', '2013-10-14', -91, 33.12, -3013.92);
INSERT INTO #c
VALUES
(53140102, 'GHI', '2014-01-02', 396, 35.52, 14065.92);
INSERT INTO #c
VALUES
(54130831, 'XYZ', '2013-08-31', -61, 23.23, -1417.03);
INSERT INTO #c
VALUES
(55130630, 'XYZ', '2013-06-30', -272, 23.45, -6378.4);
INSERT INTO #c
VALUES
(56130419, 'GHI', '2013-04-19', 416, 34.46, 14335.36);
INSERT INTO #c
VALUES
(57130813, 'XYZ', '2013-08-13', -163, 23.65, -3854.95);
INSERT INTO #c
VALUES
(58130722, 'XYZ', '2013-07-22', -88, 24.64, -2168.32);
INSERT INTO #c
VALUES
(59130320, 'XYZ', '2013-03-20', -20, 23.59, -471.8);
INSERT INTO #c
VALUES
(60130419, 'XYZ', '2013-04-19', 277, 22.83, 6323.91);
INSERT INTO #c
VALUES
(61130916, 'ABC', '2013-09-16', -202, 12.94, -2613.88);
INSERT INTO #c
VALUES
(62131027, 'XYZ', '2013-10-27', -248, 24.71, -6128.08);
INSERT INTO #c
VALUES
(63130806, 'GHI', '2013-08-06', 445, 33.5, 14907.5);
INSERT INTO #c
VALUES
(64140109, 'XYZ', '2014-01-09', -253, 24.46, -6188.38);
INSERT INTO #c
VALUES
(65131227, 'GHI', '2013-12-27', 376, 33.49, 12592.24);
INSERT INTO #c
VALUES
(66140203, 'XYZ', '2014-02-03', 459, 23.7, 10878.3);
INSERT INTO #c
VALUES
(67130302, 'XYZ', '2013-03-02', 9, 24.04, 216.36);
INSERT INTO #c
VALUES
(68130223, 'ABC', '2013-02-23', 238, 12.78, 3041.64);
INSERT INTO #c
VALUES
(69130403, 'GHI', '2013-04-03', -151, 33.16, -5007.16);
INSERT INTO #c
VALUES
(70130702, 'GHI', '2013-07-02', -162, 35.48, -5747.76);
INSERT INTO #c
VALUES
(71130731, 'ABC', '2013-07-31', 79, 13.55, 1070.45);
INSERT INTO #c
VALUES
(72140204, 'XYZ', '2014-02-04', -208, 24.36, -5066.88);
INSERT INTO #c
VALUES
(73131028, 'GHI', '2013-10-28', -46, 34.65, -1593.9);
INSERT INTO #c
VALUES
(74130619, 'XYZ', '2013-06-19', 202, 22.95, 4635.9);
INSERT INTO #c
VALUES
(75131216, 'GHI', '2013-12-16', 500, 33.55, 16775);
INSERT INTO #c
VALUES
(76131009, 'ABC', '2013-10-09', 249, 13.47, 3354.03);
INSERT INTO #c
VALUES
(77130825, 'GHI', '2013-08-25', 272, 33.86, 9209.92);
INSERT INTO #c
VALUES
(78140112, 'XYZ', '2014-01-12', 332, 24.28, 8060.96);
INSERT INTO #c
VALUES
(79131223, 'XYZ', '2013-12-23', -376, 25.12, -9445.12);
INSERT INTO #c
VALUES
(80140126, 'XYZ', '2014-01-26', 404, 24.23, 9788.92);
INSERT INTO #c
VALUES
(81131123, 'GHI', '2013-11-23', 187, 33.86, 6331.82);
INSERT INTO #c
VALUES
(82140131, 'XYZ', '2014-01-31', 548, 23.65, 12960.2);
INSERT INTO #c
VALUES
(83130428, 'XYZ', '2013-04-28', 142, 23.13, 3284.46);
INSERT INTO #c
VALUES
(84140104, 'XYZ', '2014-01-04', 261, 24.46, 6384.06);
INSERT INTO #c
VALUES
(85131002, 'GHI', '2013-10-02', -295, 32.51, -9590.45);
INSERT INTO #c
VALUES
(86131114, 'ABC', '2013-11-14', -386, 12.4, -4786.4);
INSERT INTO #c
VALUES
(87131116, 'GHI', '2013-11-16', -320, 34.16, -10931.2);
INSERT INTO #c
VALUES
(88131009, 'ABC', '2013-10-09', 187, 13.36, 2498.32);
INSERT INTO #c
VALUES
(89130611, 'ABC', '2013-06-11', 130, 13.02, 1692.6);
INSERT INTO #c
VALUES
(90130430, 'ABC', '2013-04-30', -191, 13.49, -2576.59);
INSERT INTO #c
VALUES
(91130615, 'XYZ', '2013-06-15', 545, 23.88, 13014.6);
INSERT INTO #c
VALUES
(92130924, 'XYZ', '2013-09-24', -248, 24.58, -6095.84);
INSERT INTO #c
VALUES
(93130622, 'ABC', '2013-06-22', -227, 13.47, -3057.69);
INSERT INTO #c
VALUES
(94131117, 'GHI', '2013-11-17', -92, 32.65, -3003.8);
INSERT INTO #c
VALUES
(95130908, 'GHI', '2013-09-08', 103, 35.42, 3648.26);
INSERT INTO #c
VALUES
(96130716, 'GHI', '2013-07-16', -347, 35.29, -12245.63);
INSERT INTO #c
VALUES
(97131125, 'XYZ', '2013-11-25', -278, 23.18, -6444.04);
INSERT INTO #c
VALUES
(98130413, 'XYZ', '2013-04-13', 243, 24.25, 5892.75);
INSERT INTO #c
VALUES
(99130515, 'XYZ', '2013-05-15', 97, 25.13, 2437.61);
--Calculate the FIFO Values and store in temp table
SELECT CAST([ID] as Int) as ID,
       [QTY],
       [EB],
       [GM],
       [COGS],
       [UP],
       [LP],
       [COGSC],
       [GMC],
       [GMP],
       [CGMP]
INTO #fifo
FROM wct.FIFOtvf('SELECT trn, ROW_NUMBER() OVER (PARTITION by sym ORDER BY sym, 
          tDate, trn),qty,price_extended FROM #c ORDER BY sym, tDate, trn');
--JOIN to the source data to produce the inventory output
SELECT c.trn,
       c.sym,
       c.tDate,
       c.qty,
       c.price_unit,
       c.price_extended,
       f.[QTY] as [Inventory-On-Hand],
       f.[EB] as [Inventory Cost],
       f.[GM] as [Gross Margin on Sales],
       f.[COGS] as [Cost-of-Goods Sold],
       f.[UP] as [Average Price],
       f.[LP] as [Last Price],
       f.[COGSC] as [Cumulative COGS],
       f.[GMC] as [Cumulative Gross Margin],
       f.[GMP] as [GM Percentage],
       f.[CGMP] as [Cumulative GM Percentage]
FROM #c c
    INNER JOIN #FIFO f
        ON c.trn = f.[ID]
ORDER BY c.sym,
         c.tDate,
         c.trn;

This produces the following result.

trnsymtDateqtyprice_unitprice_extendedInventory-On-HandInventory CostGross Margin on SalesCost-of-Goods SoldAverage PriceLast PriceCumulative COGSCumulative Gross MarginGM PercentageCumulative GM Percentage
68130223ABC2013-02-23238.0012.783041.642383041.640012.7812.7800NULLNULL
46130301ABC2013-03-01157.0013.012042.573955084.210012.87141772151913.0100NULLNULL
3130308ABC2013-03-08-157.0013.17-2067.692383077.7561.23-2006.4612.931722689075613.01-2006.4661.230.02961275626423690.0296127562642369
32130310ABC2013-03-10-63.0012.61-794.431752272.61-10.7100000000004-805.1412.986342857142913.01-2811.650.5199999999996-0.01348136399682840.0176512515198523
41130310ABC2013-03-10463.0013.386194.946388467.550013.272021943573713.38-2811.650.5199999999996NULL0.0176512515198523
26130320ABC2013-03-20-92.0013.64-1254.885467274.7762.1000000000004-1192.7813.323754578754613.38-4004.38112.620.04948680351906190.027354870051008
25130408ABC2013-04-08298.0012.983868.0484411142.810013.202381516587712.98-4004.38112.62NULL0.027354870051008
48130430ABC2013-04-30229.0013.323050.28107314193.090013.227483690587113.32-4004.38112.62NULL0.027354870051008
90130430ABC2013-04-30-191.0013.49-2576.5988211668.2251.7199999999993-2524.8713.229274376417213.32-6529.25164.3399999999990.02007304227680750.0245518473644187
49130508ABC2013-05-08238.0012.793044.02112014712.240013.135928571428612.79-6529.25164.339999999999NULL0.0245518473644187
20130518ABC2013-05-18298.0013.233942.54141818654.780013.155698166431613.23-6529.25164.339999999999NULL0.0245518473644187
89130611ABC2013-06-11130.0013.021692.60154820347.380013.144302325581413.02-6529.25164.339999999999NULL0.0245518473644187
2130617ABC2013-06-17313.0012.934047.09186124394.470013.108259000537312.93-6529.25164.339999999999NULL0.0245518473644187
12130621ABC2013-06-21-326.0012.73-4149.98153520032.59-211.899999999998-4361.8813.050547231270412.93-10891.13-47.5599999999985-0.0510604870384912-0.00438600940465165
93130622ABC2013-06-22-227.0013.47-3057.69130817074.5399.6299999999987-2958.0613.053922018348612.93-13849.1952.07000000000030.03258342081767570.0037457036268655
7130722ABC2013-07-22599.0013.658176.35190725250.880013.241153644467713.65-13849.1952.0700000000003NULL0.0037457036268655
71130731ABC2013-07-3179.0013.551070.45198626321.330013.253439073514613.55-13849.1952.0700000000003NULL0.0037457036268655
10130908ABC2013-09-08-386.0013.65-5268.90160021244.02191.590000000006-5077.3099999999913.277512513.55-18926.5243.6600000000060.03636242859040890.0127103790474365
39130908ABC2013-09-08490.0012.696218.10209027462.120013.139770334928212.69-18926.5243.660000000006NULL0.0127103790474365
61130916ABC2013-09-16-202.0012.94-2613.88188824869.321.0599999999968-2592.8213.172298728813612.69-21519.32264.7200000000030.008056988079023050.0121520158795156
76131009ABC2013-10-09249.0013.473354.03213728223.330013.206986429574213.47-21519.32264.720000000003NULL0.0121520158795156
88131009ABC2013-10-09187.0013.362498.32232430721.650013.219298623063713.36-21519.32264.720000000003NULL0.0121520158795156
16131107ABC2013-11-0727.0012.68342.36235131064.010013.213105061675912.68-21519.32264.720000000003NULL0.0121520158795156
86131114ABC2013-11-14-386.0012.40-4786.40196525980.12-297.49-5083.8913.221435114503812.68-26603.21-32.7699999999971-0.0621531840213939-0.00123332545490391
8131231ABC2013-12-31-145.0013.19-1912.55182024103.3835.810000000002-1876.7413.243615384615412.68-28479.953.040000000004850.01872369349821020.000106730367844277
15130307GHI2013-03-07559.0035.2119682.3955919682.390035.2135.2100NULLNULL
69130403GHI2013-04-03-151.0033.16-5007.1640814365.68-309.549999999999-5316.7135.2135.21-5316.71-309.549999999999-0.0618214716525933-0.0618214716525933
56130419GHI2013-04-19416.0034.4614335.3682428701.040034.83135922330134.46-5316.71-309.549999999999NULL-0.0618214716525933
4130516GHI2013-05-16160.0034.485516.8098434217.840034.774227642276434.48-5316.71-309.549999999999NULL-0.0618214716525933
43130521GHI2013-05-21-174.0034.20-5950.8081028091.3-175.740000000004-6126.5434.680617283950634.48-11443.25-485.290000000004-0.0295321637426908-0.044286527784369
70130702GHI2013-07-02-162.0035.48-5747.7664822387.2843.7399999999998-5704.0234.548271604938334.48-17147.27-441.5500000000040.00760992108229985-0.0264310667244515
96130716GHI2013-07-16-347.0035.29-12245.6330110375.66234.01-12011.6234.470631229235934.48-29158.89-207.5400000000040.0191096742266425-0.00716857763109505
63130806GHI2013-08-06445.0033.5014907.5074625283.160033.8916353887433.5-29158.89-207.540000000004NULL-0.00716857763109505
77130825GHI2013-08-25272.0033.869209.92101834493.080033.883182711198433.86-29158.89-207.540000000004NULL-0.00716857763109505
95130908GHI2013-09-08103.0035.423648.26112138141.340034.024388938447835.42-29158.89-207.540000000004NULL-0.00716857763109505
6130924GHI2013-09-24328.0034.9511463.60144949604.940034.233913043478334.95-29158.89-207.540000000004NULL-0.00716857763109505
17130924GHI2013-09-24-291.0035.69-10385.79115839574.08354.93-10030.8634.174507772020734.95-39189.75147.3899999999970.03417457891985110.00374684077185064
85131002GHI2013-10-02-295.0032.51-9590.4586329681.78-301.850000000002-9892.334.393719582850534.95-49082.05-154.460000000005-0.0314740184245788-0.00315691003787445
52131014GHI2013-10-14-91.0033.12-3013.9277226633.28-34.5799999999999-3048.534.499067357512934.95-52130.55-189.040000000005-0.0114734299516908-0.00363947832860472
73131028GHI2013-10-28-46.0034.65-1593.9072625092.2852.9000000000001-154134.562369146005534.95-53671.55-136.1400000000050.0331890331890332-0.00254298977069579
50131103GHI2013-11-03-246.0035.61-8760.0648016771438.780000000001-8321.2834.939583333333334.95-61992.83302.6399999999950.05008869802261640.00485813815996565
87131116GHI2013-11-16-320.0034.16-10931.201605592-247.799999999999-1117934.9534.95-73171.8354.8399999999961-0.02266905737704910.000748907467729941
94131117GHI2013-11-17-92.0032.65-3003.80682376.6-211.6-3215.434.9534.95-76387.23-156.760000000004-0.0704441041347626-0.0020563955594135
81131123GHI2013-11-23187.0033.866331.822558708.420034.150666666666733.86-76387.23-156.760000000004NULL-0.0020563955594135
51131206GHI2013-12-0685.0033.642859.4034011567.820034.02333.64-76387.23-156.760000000004NULL-0.0020563955594135
75131216GHI2013-12-16500.0033.5516775.0084028342.820033.741452380952433.55-76387.23-156.760000000004NULL-0.0020563955594135
13131221GHI2013-12-2172.0034.382475.3691230818.180033.791864035087734.38-76387.23-156.760000000004NULL-0.0020563955594135
65131227GHI2013-12-27376.0033.4912592.24128843410.420033.703742236024833.49-76387.23-156.760000000004NULL-0.0020563955594135
53140102GHI2014-01-02396.0035.5214065.92168457476.340034.130843230403835.52-76387.23-156.760000000004NULL-0.0020563955594135
18140125GHI2014-01-25-78.0035.46-2765.88160654761.1450.6800000000103-2715.1999999999934.097845579078535.52-79102.43-106.0799999999940.0183232822826769-0.00134284685305072
37130220XYZ2013-02-20528.0023.5412429.1252812429.120023.5423.5400NULLNULL
44130227XYZ2013-02-27357.0022.868161.0288520590.140023.265694915254222.8600NULLNULL
67130302XYZ2013-03-029.0024.04216.3689420806.50023.273489932885924.0400NULLNULL
38130311XYZ2013-03-11-193.0024.90-4805.7070116263.28262.480000000002-4543.2223.200114122681924.04-4543.22262.4800000000020.05461847389558280.0546184738955828
59130320XYZ2013-03-20-20.0023.59-471.8068115792.480.99999999999892-470.80000000000123.190132158590324.04-5014.02263.4800000000010.002119542178887070.0499251539554716
35130321XYZ2013-03-21275.0024.836828.2595622620.730023.661851464435124.83-5014.02263.480000000001NULL0.0499251539554716
98130413XYZ2013-04-13243.0024.255892.75119928513.480023.781050875729824.25-5014.02263.480000000001NULL0.0499251539554716
24130418XYZ2013-04-18275.0024.836828.25147435341.730023.97675033921324.83-5014.02263.480000000001NULL0.0499251539554716
60130419XYZ2013-04-19277.0022.836323.91175141665.640023.795339805825222.83-5014.02263.480000000001NULL0.0499251539554716
83130428XYZ2013-04-28142.0023.133284.46189344950.10023.745430533544623.13-5014.02263.480000000001NULL0.0499251539554716
34130507XYZ2013-05-0755.0023.431288.65194846238.750023.736524640657123.43-5014.02263.480000000001NULL0.0499251539554716
99130515XYZ2013-05-1597.0025.132437.61204548676.360023.802621026894925.13-5014.02263.480000000001NULL0.0499251539554716
19130516XYZ2013-05-16315.0023.577424.55236056100.910023.771572033898323.57-5014.02263.480000000001NULL0.0499251539554716
30130519XYZ2013-05-19467.0023.1510811.05282766911.960023.66889281924323.15-5014.02263.480000000001NULL0.0499251539554716
29130602XYZ2013-06-02114.0024.292769.06294169681.020023.692968378102724.29-5014.02263.480000000001NULL0.0499251539554716
91130615XYZ2013-06-15545.0023.8813014.60348682695.620023.722208835341423.88-5014.02263.480000000001NULL0.0499251539554716
23130619XYZ2013-06-19296.0023.466944.16378289639.780023.70168693812823.46-5014.02263.480000000001NULL0.0499251539554716
47130619XYZ2013-06-19413.0025.1810399.344195100039.120023.847227651966625.18-5014.02263.480000000001NULL0.0499251539554716
74130619XYZ2013-06-19202.0022.954635.904397104675.020023.806008642256122.95-5014.02263.480000000001NULL0.0499251539554716
55130630XYZ2013-06-30-272.0023.45-6378.40412598272.14-24.4799999999905-6402.8799999999923.823549090909122.95-11416.9239.000000000011-0.003837953091682940.0205046371365584
14130705XYZ2013-07-05-277.0025.01-6927.77384891910.68566.309999999994-6361.4600000000123.885311850311922.95-17778.36805.3100000000050.08174491936077470.0433342821950672
5130706XYZ2013-07-06-170.0023.46-3988.2036788793916.5200000000068-3971.6799999999923.909461663947822.95-21750.04821.8300000000120.004142219547667320.0364094778146433
58130722XYZ2013-07-22-88.0024.64-2168.32359085753.96-16.720000000008-2185.0400000000123.886896935933122.95-23935.08805.110000000004-0.007711038961042640.0325425956712541
57130813XYZ2013-08-13-163.0023.65-3854.95342781714.79-184.219999999998-4039.1723.844409104172722.95-27974.25620.890000000005-0.04778790905199770.0217131302731865
54130831XYZ2013-08-31-61.0023.23-1417.03336680235.54-62.22-1479.2523.837058823529422.95-29453.5558.670000000005-0.0439087386999570.0186147819367945
11130906XYZ2013-09-06-13.0023.97-311.61335379920.29-3.63999999999999-315.2523.83545779898622.95-29768.75555.030000000005-0.01168126825198160.0183034568909287
27130906XYZ2013-09-06-147.0023.81-3500.07320676355.54-64.6799999999998-3564.7523.816450405489722.95-33333.5490.350000000005-0.01847963040739180.0144971669398961
36130917XYZ2013-09-1792.0024.602263.20329878618.740023.838308065494224.6-33333.5490.350000000005NULL0.0144971669398961
92130924XYZ2013-09-24-248.0024.58-6095.84305072465.54-57.3599999999969-6153.223.75919344262324.6-39486.7432.990000000008-0.009409695792539990.010846527114815
42131011XYZ2013-10-11-250.0023.12-5780.00280066688.042.5-5777.523.817157142857124.6-45264.2435.4900000000080.0004325259515570930.00952938630437117
22131012XYZ2013-10-12596.0023.1613803.36339680491.40023.701825677267423.16-45264.2435.490000000008NULL0.00952938630437117
40131013XYZ2013-10-13359.0023.918583.69375589075.090023.721728362183823.91-45264.2435.490000000008NULL0.00952938630437117
1131019XYZ2013-10-19424.0025.1310655.12417999730.210023.864611150993125.13-45264.2435.490000000008NULL0.00952938630437117
9131025XYZ2013-10-25-153.0024.31-3719.43402696209.92199.140000000006-3520.2899999999923.897148534525625.13-48784.49634.6300000000150.05354046184496180.0128417907886667
62131027XYZ2013-10-27-248.0024.71-6128.08377890243.38161.539999999992-5966.5400000000123.886548438327225.13-54751.03796.1700000000060.02636062192399440.0143332157156437
45131030XYZ2013-10-30-350.0023.36-8176.00342882027.48-39.8999999999942-8215.8999999999923.928669778296425.13-62966.93756.270000000012-0.004880136986300660.0118680480578504
21131103XYZ2013-11-03-326.0023.24-7576.24310274480.5829.3399999999911-7546.9000000000124.01050290135425.13-70513.83785.6100000000030.00387263339070450.0110184596120251
97131125XYZ2013-11-25-278.0023.18-6444.04282467839.73-196.809999999991-6640.8499999999924.022567280453325.13-77154.68588.800000000012-0.03054139949472560.0075736254667274
28131209XYZ2013-12-09315.0024.467704.90313975544.630024.066463841987924.46-77154.68588.800000000012NULL0.0075736254667274
79131223XYZ2013-12-23-376.0025.12-9445.12276366565.75466.239999999996-8978.8824.091838581252324.46-86133.561055.040000000010.04936305732484040.0121006645364188
33140102XYZ2014-01-02-196.0022.98-4504.08256761939.87-121.800000000005-4625.8824.129283209972724.46-90759.44933.240000000003-0.02704214845207120.0101779116937143
84140104XYZ2014-01-04261.0024.466384.06282868323.930024.159805516265924.46-90759.44933.240000000003NULL0.0101779116937143
64140109XYZ2014-01-09-253.0024.46-6188.38257562238.91103.360000000011-6085.0199999999924.170450485436924.46-96844.461036.600000000010.01670227103054610.0105904043131533
78140112XYZ2014-01-12332.0024.288060.96290770299.870024.182961816305524.28-96844.461036.60000000001NULL0.0105904043131533
80140126XYZ2014-01-26404.0024.239788.92331180088.790024.188701298701324.23-96844.461036.60000000001NULL0.0105904043131533
82140131XYZ2014-01-31548.0023.6512960.20385993048.990024.112202643171823.65-96844.461036.60000000001NULL0.0105904043131533
66140203XYZ2014-02-03459.0023.7010878.304318103927.290024.068385826771723.7-96844.461036.60000000001NULL0.0105904043131533
72140204XYZ2014-02-04-208.0024.36-5066.88411098689.85-170.560000000017-5237.4400000000224.012128953771323.7-102081.9866.039999999997-0.03366174055829560.00841240728080618
31140205XYZ2014-02-0542.0024.391024.38415299714.230024.015951348747624.39-102081.9866.039999999997NULL0.00841240728080618

Example #2

In this example, we will look at a simple FX blotter. In FX trading, it is not at all unusual to switch from a long currency position to a short currency position during the course of a day. Additionally, if you are trading something like the US dollars against the Euro, if you are long US dollars, you are short the Euro. Thus, it's critical to pay attention to the sign. We will create the #fx table and populate it with some data.

--Create the temporary table
CREATE TABLE #fx
(
    trn int,
    ccy char(3),
    amt_ccy money,
    rate float,
    ctr char(3),
    amt_ctr money,
    PRIMARY KEY (trn)
);
--Populate the table with some data
INSERT INTO #fx
VALUES
(101, 'GBP', 8000000, 1.619, 'USD', -12952000);
INSERT INTO #fx
VALUES
(102, 'GBP', -10000000, 1.62, 'USD', 16200000);
INSERT INTO #fx
VALUES
(103, 'GBP', -4000000, 1.613, 'USD', 6452000);
INSERT INTO #fx
VALUES
(104, 'GBP', 7000000, 1.618, 'USD', -11326000);
INSERT INTO #fx
VALUES
(105, 'GBP', 6000000, 1.623, 'USD', -9738000);
INSERT INTO #fx
VALUES
(106, 'GBP', -5000000, 1.618, 'USD', 8090000);
INSERT INTO #fx
VALUES
(107, 'GBP', -10000000, 1.602, 'USD', 16020000);
INSERT INTO #fx
VALUES
(108, 'GBP', 2000000, 1.608, 'USD', -3216000);
INSERT INTO #fx
VALUES
(109, 'GBP', -2000000, 1.602, 'USD', 3204000);
INSERT INTO #fx
VALUES
(110, 'GBP', 10000000, 1.626, 'USD', -16260000);
--Calculate the FIFO Values and store in temp table
SELECT CAST([ID] as Int) as ID,
       [QTY],
       [EB],
       [GM],
       [COGS],
       [UP],
       [LP],
       [COGSC],
       [GMC],
       [GMP],
       [CGMP]
INTO #fifo
FROM wct.FIFOtvf('SELECT trn, ROW_NUMBER() OVER (ORDER BY trn),amt_ccy,-amt_ctr 
          FROM #fx ORDER BY trn');
--JOIN to the source data to produce the inventory output
SELECT c.trn,
       c.ccy,
       c.amt_ccy,
       c.rate,
       c.ctr,
       c.amt_ctr,
       f.[QTY] as [GBP Position],
       f.[EB] as [USD Position],
       f.[GM] as [P/L],
       f.[COGS] as [Cost-of-Goods Sold],
       f.[UP] as [Average Price],
       f.[LP] as [Last Price],
       f.[COGSC] as [Cumulative COGS],
       f.[GMC] as [Cumulative P/L],
       f.[GMP] as [GM Percentage],
       f.[CGMP] as [Cumulative GM Percentage]
FROM #fx c
    INNER JOIN #FIFO f
        ON c.trn = f.[ID]
ORDER BY c.trn;

This produces the following result.

trnccyamt_ccyratectramt_ctrGBP PositionUSD PositionP/LCost-of-Goods SoldAverage PriceLast PriceCumulative COGSCumulative P/LGM PercentageCumulative GM Percentage
101GBP8000000.001.619USD-12952000.00800000012952000001.6191.61900NULLNULL
102GBP-10000000.001.62USD16200000.00-2000000-32400008000-129520001.621.62-1295200080000.0006172839506172840.000617283950617284
103GBP-4000000.001.613USD6452000.00-6000000-9692000001.615333333333331.613-129520008000NULL0.000617283950617284
104GBP7000000.001.618USD-11326000.0010000001618000-1600096920001.6181.618-3260000-80000.00164812525751957-0.002460024600246
105GBP6000000.001.623USD-9738000.00700000011356000001.622285714285711.623-3260000-8000NULL-0.002460024600246
106GBP-5000000.001.618USD8090000.0020000003246000-20000-81100001.6231.623-11370000-28000-0.00247218788627936-0.00246870040557221
107GBP-10000000.001.602USD16020000.00-8000000-12816000-42000-32460001.6021.602-14616000-70000-0.0131086142322097-0.00481231953801732
108GBP2000000.001.608USD-3216000.00-6000000-9612000-1200032040001.6021.602-11412000-820000.00373134328358209-0.00723742277140335
109GBP-2000000.001.602USD3204000.00-8000000-12816000001.6021.602-11412000-82000NULL-0.00723742277140335
110GBP10000000.001.626USD-16260000.0020000003252000-192000128160001.6261.6261404000-2740000.0147601476014760.163289630512515

Example #3

Using the #c temp table created in Example #1, we will insert the necessary input for the FIFOtvf into the #inv temp table containing an IDENTITY column which is then used to link the input data to the output data. This simplifies the @DataQuery SQL.

--put the #c data into #inv
SELECT IDENTITY(int, 1, 1) as id,
       trn,
       ROW_NUMBER() OVER (PARTITION by sym ORDER BY sym, tDate, trn) as rn,
       qty,
       price_extended
INTO #inv
FROM #c
ORDER BY sym,
         tDate,
         trn;
--Calculate the FIFO Values and store in temp table
SELECT CAST([ID] as Int) as ID,
       [QTY],
       [EB],
       [GM],
       [COGS],
       [UP],
       [LP],
       [COGSC],
       [GMC],
       [GMP],
       [CGMP]
INTO #fifo
FROM wct.FIFOtvf('SELECT id,rn,qty,price_extended FROM #inv ORDER BY id');
--JOIN to the source data to produce the inventory output
SELECT i.trn,
       c.sym,
       c.tDate,
       i.qty,
       c.price_unit,
       i.price_extended,
       f.[QTY] as [Inventory-On-Hand],
       f.[EB] as [Inventory Cost],
       f.[GM] as [Gross Margin on Sales],
       f.[COGS] as [Cost-of-Goods Sold],
       f.[UP] as [Average Price],
       f.[LP] as [Last Price],
       f.[COGSC] as [Cumulative COGS],
       f.[GMC] as [Cumulative Gross Margin],
       f.[GMP] as [GM Percentage],
       f.[CGMP] as [Cumulative GM Percentage]
FROM #inv i
    INNER JOIN #FIFO f
        ON i.ID = f.[ID]
    INNER JOIN #c c
        ON i.trn = c.trn
ORDER BY i.id;

This produces the following result.

trnsymtDateqtyprice_unitprice_extendedInventory-On-HandInventory CostGross Margin on SalesCost-of-Goods SoldAverage PriceLast PriceCumulative COGSCumulative Gross MarginGM PercentageCumulative GM Percentage
68130223ABC2013-02-23238.0012.783041.642383041.640012.7812.7800NULLNULL
46130301ABC2013-03-01157.0013.012042.573955084.210012.87141772151913.0100NULLNULL
3130308ABC2013-03-08-157.0013.17-2067.692383077.7561.23-2006.4612.931722689075613.01-2006.4661.230.02961275626423690.0296127562642369
32130310ABC2013-03-10-63.0012.61-794.431752272.61-10.7100000000004-805.1412.986342857142913.01-2811.650.5199999999996-0.01348136399682840.0176512515198523
41130310ABC2013-03-10463.0013.386194.946388467.550013.272021943573713.38-2811.650.5199999999996NULL0.0176512515198523
26130320ABC2013-03-20-92.0013.64-1254.885467274.7762.1000000000004-1192.7813.323754578754613.38-4004.38112.620.04948680351906190.027354870051008
25130408ABC2013-04-08298.0012.983868.0484411142.810013.202381516587712.98-4004.38112.62NULL0.027354870051008
48130430ABC2013-04-30229.0013.323050.28107314193.090013.227483690587113.32-4004.38112.62NULL0.027354870051008
90130430ABC2013-04-30-191.0013.49-2576.5988211668.2251.7199999999993-2524.8713.229274376417213.32-6529.25164.3399999999990.02007304227680750.0245518473644187
49130508ABC2013-05-08238.0012.793044.02112014712.240013.135928571428612.79-6529.25164.339999999999NULL0.0245518473644187
20130518ABC2013-05-18298.0013.233942.54141818654.780013.155698166431613.23-6529.25164.339999999999NULL0.0245518473644187
89130611ABC2013-06-11130.0013.021692.60154820347.380013.144302325581413.02-6529.25164.339999999999NULL0.0245518473644187
2130617ABC2013-06-17313.0012.934047.09186124394.470013.108259000537312.93-6529.25164.339999999999NULL0.0245518473644187
12130621ABC2013-06-21-326.0012.73-4149.98153520032.59-211.899999999998-4361.8813.050547231270412.93-10891.13-47.5599999999985-0.0510604870384912-0.00438600940465165
93130622ABC2013-06-22-227.0013.47-3057.69130817074.5399.6299999999987-2958.0613.053922018348612.93-13849.1952.07000000000030.03258342081767570.0037457036268655
7130722ABC2013-07-22599.0013.658176.35190725250.880013.241153644467713.65-13849.1952.0700000000003NULL0.0037457036268655
71130731ABC2013-07-3179.0013.551070.45198626321.330013.253439073514613.55-13849.1952.0700000000003NULL0.0037457036268655
10130908ABC2013-09-08-386.0013.65-5268.90160021244.02191.590000000006-5077.3099999999913.277512513.55-18926.5243.6600000000060.03636242859040890.0127103790474365
39130908ABC2013-09-08490.0012.696218.10209027462.120013.139770334928212.69-18926.5243.660000000006NULL0.0127103790474365
61130916ABC2013-09-16-202.0012.94-2613.88188824869.321.0599999999968-2592.8213.172298728813612.69-21519.32264.7200000000030.008056988079023050.0121520158795156
76131009ABC2013-10-09249.0013.473354.03213728223.330013.206986429574213.47-21519.32264.720000000003NULL0.0121520158795156
88131009ABC2013-10-09187.0013.362498.32232430721.650013.219298623063713.36-21519.32264.720000000003NULL0.0121520158795156
16131107ABC2013-11-0727.0012.68342.36235131064.010013.213105061675912.68-21519.32264.720000000003NULL0.0121520158795156
86131114ABC2013-11-14-386.0012.40-4786.40196525980.12-297.49-5083.8913.221435114503812.68-26603.21-32.7699999999971-0.0621531840213939-0.00123332545490391
8131231ABC2013-12-31-145.0013.19-1912.55182024103.3835.810000000002-1876.7413.243615384615412.68-28479.953.040000000004850.01872369349821020.000106730367844277
15130307GHI2013-03-07559.0035.2119682.3955919682.390035.2135.2100NULLNULL
69130403GHI2013-04-03-151.0033.16-5007.1640814365.68-309.549999999999-5316.7135.2135.21-5316.71-309.549999999999-0.0618214716525933-0.0618214716525933
56130419GHI2013-04-19416.0034.4614335.3682428701.040034.83135922330134.46-5316.71-309.549999999999NULL-0.0618214716525933
4130516GHI2013-05-16160.0034.485516.8098434217.840034.774227642276434.48-5316.71-309.549999999999NULL-0.0618214716525933
43130521GHI2013-05-21-174.0034.20-5950.8081028091.3-175.740000000004-6126.5434.680617283950634.48-11443.25-485.290000000004-0.0295321637426908-0.044286527784369
70130702GHI2013-07-02-162.0035.48-5747.7664822387.2843.7399999999998-5704.0234.548271604938334.48-17147.27-441.5500000000040.00760992108229985-0.0264310667244515
96130716GHI2013-07-16-347.0035.29-12245.6330110375.66234.01-12011.6234.470631229235934.48-29158.89-207.5400000000040.0191096742266425-0.00716857763109505
63130806GHI2013-08-06445.0033.5014907.5074625283.160033.8916353887433.5-29158.89-207.540000000004NULL-0.00716857763109505
77130825GHI2013-08-25272.0033.869209.92101834493.080033.883182711198433.86-29158.89-207.540000000004NULL-0.00716857763109505
95130908GHI2013-09-08103.0035.423648.26112138141.340034.024388938447835.42-29158.89-207.540000000004NULL-0.00716857763109505
6130924GHI2013-09-24328.0034.9511463.60144949604.940034.233913043478334.95-29158.89-207.540000000004NULL-0.00716857763109505
17130924GHI2013-09-24-291.0035.69-10385.79115839574.08354.93-10030.8634.174507772020734.95-39189.75147.3899999999970.03417457891985110.00374684077185064
85131002GHI2013-10-02-295.0032.51-9590.4586329681.78-301.850000000002-9892.334.393719582850534.95-49082.05-154.460000000005-0.0314740184245788-0.00315691003787445
52131014GHI2013-10-14-91.0033.12-3013.9277226633.28-34.5799999999999-3048.534.499067357512934.95-52130.55-189.040000000005-0.0114734299516908-0.00363947832860472
73131028GHI2013-10-28-46.0034.65-1593.9072625092.2852.9000000000001-154134.562369146005534.95-53671.55-136.1400000000050.0331890331890332-0.00254298977069579
50131103GHI2013-11-03-246.0035.61-8760.0648016771438.780000000001-8321.2834.939583333333334.95-61992.83302.6399999999950.05008869802261640.00485813815996565
87131116GHI2013-11-16-320.0034.16-10931.201605592-247.799999999999-1117934.9534.95-73171.8354.8399999999961-0.02266905737704910.000748907467729941
94131117GHI2013-11-17-92.0032.65-3003.80682376.6-211.6-3215.434.9534.95-76387.23-156.760000000004-0.0704441041347626-0.0020563955594135
81131123GHI2013-11-23187.0033.866331.822558708.420034.150666666666733.86-76387.23-156.760000000004NULL-0.0020563955594135
51131206GHI2013-12-0685.0033.642859.4034011567.820034.02333.64-76387.23-156.760000000004NULL-0.0020563955594135
75131216GHI2013-12-16500.0033.5516775.0084028342.820033.741452380952433.55-76387.23-156.760000000004NULL-0.0020563955594135
13131221GHI2013-12-2172.0034.382475.3691230818.180033.791864035087734.38-76387.23-156.760000000004NULL-0.0020563955594135
65131227GHI2013-12-27376.0033.4912592.24128843410.420033.703742236024833.49-76387.23-156.760000000004NULL-0.0020563955594135
53140102GHI2014-01-02396.0035.5214065.92168457476.340034.130843230403835.52-76387.23-156.760000000004NULL-0.0020563955594135
18140125GHI2014-01-25-78.0035.46-2765.88160654761.1450.6800000000103-2715.1999999999934.097845579078535.52-79102.43-106.0799999999940.0183232822826769-0.00134284685305072
37130220XYZ2013-02-20528.0023.5412429.1252812429.120023.5423.5400NULLNULL
44130227XYZ2013-02-27357.0022.868161.0288520590.140023.265694915254222.8600NULLNULL
67130302XYZ2013-03-029.0024.04216.3689420806.50023.273489932885924.0400NULLNULL
38130311XYZ2013-03-11-193.0024.90-4805.7070116263.28262.480000000002-4543.2223.200114122681924.04-4543.22262.4800000000020.05461847389558280.0546184738955828
59130320XYZ2013-03-20-20.0023.59-471.8068115792.480.99999999999892-470.80000000000123.190132158590324.04-5014.02263.4800000000010.002119542178887070.0499251539554716
35130321XYZ2013-03-21275.0024.836828.2595622620.730023.661851464435124.83-5014.02263.480000000001NULL0.0499251539554716
98130413XYZ2013-04-13243.0024.255892.75119928513.480023.781050875729824.25-5014.02263.480000000001NULL0.0499251539554716
24130418XYZ2013-04-18275.0024.836828.25147435341.730023.97675033921324.83-5014.02263.480000000001NULL0.0499251539554716
60130419XYZ2013-04-19277.0022.836323.91175141665.640023.795339805825222.83-5014.02263.480000000001NULL0.0499251539554716
83130428XYZ2013-04-28142.0023.133284.46189344950.10023.745430533544623.13-5014.02263.480000000001NULL0.0499251539554716
34130507XYZ2013-05-0755.0023.431288.65194846238.750023.736524640657123.43-5014.02263.480000000001NULL0.0499251539554716
99130515XYZ2013-05-1597.0025.132437.61204548676.360023.802621026894925.13-5014.02263.480000000001NULL0.0499251539554716
19130516XYZ2013-05-16315.0023.577424.55236056100.910023.771572033898323.57-5014.02263.480000000001NULL0.0499251539554716
30130519XYZ2013-05-19467.0023.1510811.05282766911.960023.66889281924323.15-5014.02263.480000000001NULL0.0499251539554716
29130602XYZ2013-06-02114.0024.292769.06294169681.020023.692968378102724.29-5014.02263.480000000001NULL0.0499251539554716
91130615XYZ2013-06-15545.0023.8813014.60348682695.620023.722208835341423.88-5014.02263.480000000001NULL0.0499251539554716
23130619XYZ2013-06-19296.0023.466944.16378289639.780023.70168693812823.46-5014.02263.480000000001NULL0.0499251539554716
47130619XYZ2013-06-19413.0025.1810399.344195100039.120023.847227651966625.18-5014.02263.480000000001NULL0.0499251539554716
74130619XYZ2013-06-19202.0022.954635.904397104675.020023.806008642256122.95-5014.02263.480000000001NULL0.0499251539554716
55130630XYZ2013-06-30-272.0023.45-6378.40412598272.14-24.4799999999905-6402.8799999999923.823549090909122.95-11416.9239.000000000011-0.003837953091682940.0205046371365584
14130705XYZ2013-07-05-277.0025.01-6927.77384891910.68566.309999999994-6361.4600000000123.885311850311922.95-17778.36805.3100000000050.08174491936077470.0433342821950672
5130706XYZ2013-07-06-170.0023.46-3988.2036788793916.5200000000068-3971.6799999999923.909461663947822.95-21750.04821.8300000000120.004142219547667320.0364094778146433
58130722XYZ2013-07-22-88.0024.64-2168.32359085753.96-16.720000000008-2185.0400000000123.886896935933122.95-23935.08805.110000000004-0.007711038961042640.0325425956712541
57130813XYZ2013-08-13-163.0023.65-3854.95342781714.79-184.219999999998-4039.1723.844409104172722.95-27974.25620.890000000005-0.04778790905199770.0217131302731865
54130831XYZ2013-08-31-61.0023.23-1417.03336680235.54-62.22-1479.2523.837058823529422.95-29453.5558.670000000005-0.0439087386999570.0186147819367945
11130906XYZ2013-09-06-13.0023.97-311.61335379920.29-3.63999999999999-315.2523.83545779898622.95-29768.75555.030000000005-0.01168126825198160.0183034568909287
27130906XYZ2013-09-06-147.0023.81-3500.07320676355.54-64.6799999999998-3564.7523.816450405489722.95-33333.5490.350000000005-0.01847963040739180.0144971669398961
36130917XYZ2013-09-1792.0024.602263.20329878618.740023.838308065494224.6-33333.5490.350000000005NULL0.0144971669398961
92130924XYZ2013-09-24-248.0024.58-6095.84305072465.54-57.3599999999969-6153.223.75919344262324.6-39486.7432.990000000008-0.009409695792539990.010846527114815
42131011XYZ2013-10-11-250.0023.12-5780.00280066688.042.5-5777.523.817157142857124.6-45264.2435.4900000000080.0004325259515570930.00952938630437117
22131012XYZ2013-10-12596.0023.1613803.36339680491.40023.701825677267423.16-45264.2435.490000000008NULL0.00952938630437117
40131013XYZ2013-10-13359.0023.918583.69375589075.090023.721728362183823.91-45264.2435.490000000008NULL0.00952938630437117
1131019XYZ2013-10-19424.0025.1310655.12417999730.210023.864611150993125.13-45264.2435.490000000008NULL0.00952938630437117
9131025XYZ2013-10-25-153.0024.31-3719.43402696209.92199.140000000006-3520.2899999999923.897148534525625.13-48784.49634.6300000000150.05354046184496180.0128417907886667
62131027XYZ2013-10-27-248.0024.71-6128.08377890243.38161.539999999992-5966.5400000000123.886548438327225.13-54751.03796.1700000000060.02636062192399440.0143332157156437
45131030XYZ2013-10-30-350.0023.36-8176.00342882027.48-39.8999999999942-8215.8999999999923.928669778296425.13-62966.93756.270000000012-0.004880136986300660.0118680480578504
21131103XYZ2013-11-03-326.0023.24-7576.24310274480.5829.3399999999911-7546.9000000000124.01050290135425.13-70513.83785.6100000000030.00387263339070450.0110184596120251
97131125XYZ2013-11-25-278.0023.18-6444.04282467839.73-196.809999999991-6640.8499999999924.022567280453325.13-77154.68588.800000000012-0.03054139949472560.0075736254667274
28131209XYZ2013-12-09315.0024.467704.90313975544.630024.066463841987924.46-77154.68588.800000000012NULL0.0075736254667274
79131223XYZ2013-12-23-376.0025.12-9445.12276366565.75466.239999999996-8978.8824.091838581252324.46-86133.561055.040000000010.04936305732484040.0121006645364188
33140102XYZ2014-01-02-196.0022.98-4504.08256761939.87-121.800000000005-4625.8824.129283209972724.46-90759.44933.240000000003-0.02704214845207120.0101779116937143
84140104XYZ2014-01-04261.0024.466384.06282868323.930024.159805516265924.46-90759.44933.240000000003NULL0.0101779116937143
64140109XYZ2014-01-09-253.0024.46-6188.38257562238.91103.360000000011-6085.0199999999924.170450485436924.46-96844.461036.600000000010.01670227103054610.0105904043131533
78140112XYZ2014-01-12332.0024.288060.96290770299.870024.182961816305524.28-96844.461036.60000000001NULL0.0105904043131533
80140126XYZ2014-01-26404.0024.239788.92331180088.790024.188701298701324.23-96844.461036.60000000001NULL0.0105904043131533
82140131XYZ2014-01-31548.0023.6512960.20385993048.990024.112202643171823.65-96844.461036.60000000001NULL0.0105904043131533
66140203XYZ2014-02-03459.0023.7010878.304318103927.290024.068385826771723.7-96844.461036.60000000001NULL0.0105904043131533
72140204XYZ2014-02-04-208.0024.36-5066.88411098689.85-170.560000000017-5237.4400000000224.012128953771323.7-102081.9866.039999999997-0.03366174055829560.00841240728080618
31140205XYZ2014-02-0542.0024.391024.38415299714.230024.015951348747624.39-102081.9866.039999999997NULL0.00841240728080618

Example #4

In this example we require multiple columns to uniquely identify the inventory items and there are no unique transaction identifiers.

--Put some data into #p
SELECT *
INTO #p
FROM
(
    VALUES
        ('ZYX', 'Coffee Mug', 'Ceramic', '8 oz', 'Black', '2017-01-15', 50, 1.12, 56),
        ('ZYX', 'Coffee Mug', 'Ceramic', '12 oz', 'Black', '2017-01-15', 50, 1.12, 56),
        ('ZYX', 'Coffee Mug', 'Ceramic', '16 oz', 'Black', '2017-01-15', 50, 1.12, 56),
        ('ZYX', 'Coffee Mug', 'Ceramic', '20 oz', 'Black', '2017-01-15', 50, 1.12, 56),
        ('ZYX', 'Coffee Mug', 'Ceramic', '8 oz', 'White', '2017-01-15', 50, 1.12, 56),
        ('ZYX', 'Coffee Mug', 'Ceramic', '12 oz', 'White', '2017-01-15', 50, 1.12, 56),
        ('ZYX', 'Coffee Mug', 'Ceramic', '16 oz', 'White', '2017-01-15', 50, 1.12, 56),
        ('ZYX', 'Coffee Mug', 'Ceramic', '20 oz', 'White', '2017-01-15', 50, 1.12, 56),
        ('ZYX', 'Coffee Mug', 'Plastic', '8 oz', 'Black', '2017-01-15', 50, 0.89, 44.5),
        ('ZYX', 'Coffee Mug', 'Plastic', '12 oz', 'Black', '2017-01-15', 50, 0.89, 44.5),
        ('ZYX', 'Coffee Mug', 'Plastic', '16 oz', 'Black', '2017-01-15', 50, 0.89, 44.5),
        ('ZYX', 'Coffee Mug', 'Plastic', '20 oz', 'Black', '2017-01-15', 50, 0.89, 44.5),
        ('ZYX', 'Coffee Mug', 'Plastic', '8 oz', 'White', '2017-01-15', 50, 0.89, 44.5),
        ('ZYX', 'Coffee Mug', 'Plastic', '12 oz', 'White', '2017-01-15', 50, 0.89, 44.5),
        ('ZYX', 'Coffee Mug', 'Plastic', '16 oz', 'White', '2017-01-15', 50, 0.89, 44.5),
        ('ZYX', 'Coffee Mug', 'Plastic', '20 oz', 'White', '2017-01-15', 50, 0.89, 44.5),
        ('ZYX', 'Coffee Mug', 'Ceramic', '8 oz', 'Black', '2017-02-15', 50, 1.15, 57.5),
        ('ZYX', 'Coffee Mug', 'Ceramic', '12 oz', 'Black', '2017-02-15', 50, 1.15, 57.5),
        ('ZYX', 'Coffee Mug', 'Ceramic', '16 oz', 'Black', '2017-02-15', 50, 1.15, 57.5),
        ('ZYX', 'Coffee Mug', 'Ceramic', '20 oz', 'Black', '2017-02-15', 50, 1.15, 57.5),
        ('ZYX', 'Coffee Mug', 'Ceramic', '8 oz', 'White', '2017-02-15', 50, 1.15, 57.5),
        ('ZYX', 'Coffee Mug', 'Ceramic', '12 oz', 'White', '2017-02-15', 50, 1.15, 57.5),
        ('ZYX', 'Coffee Mug', 'Ceramic', '16 oz', 'White', '2017-02-15', 50, 1.15, 57.5),
        ('ZYX', 'Coffee Mug', 'Ceramic', '20 oz', 'White', '2017-02-15', 50, 1.15, 57.5),
        ('ZYX', 'Coffee Mug', 'Plastic', '8 oz', 'Black', '2017-02-15', 50, 1.15, 57.5),
        ('ZYX', 'Coffee Mug', 'Plastic', '12 oz', 'Black', '2017-02-15', 50, 1.15, 57.5),
        ('ZYX', 'Coffee Mug', 'Plastic', '16 oz', 'Black', '2017-02-15', 50, 1.15, 57.5),
        ('ZYX', 'Coffee Mug', 'Plastic', '20 oz', 'Black', '2017-02-15', 50, 1.15, 57.5),
        ('ZYX', 'Coffee Mug', 'Plastic', '8 oz', 'White', '2017-02-15', 50, 1.15, 57.5),
        ('ZYX', 'Coffee Mug', 'Plastic', '12 oz', 'White', '2017-02-15', 50, 1.15, 57.5),
        ('ZYX', 'Coffee Mug', 'Plastic', '16 oz', 'White', '2017-02-15', 50, 1.15, 57.5),
        ('ZYX', 'Coffee Mug', 'Plastic', '20 oz', 'White', '2017-02-15', 50, 1.15, 57.5),
        ('ZYX', 'Coffee Mug', 'Plastic', '8 oz', 'White', '2017-01-16', -5, 2.95, -14.75),
        ('ZYX', 'Coffee Mug', 'Plastic', '8 oz', 'White', '2017-01-23', -2, 2.95, -5.9),
        ('ZYX', 'Coffee Mug', 'Plastic', '8 oz', 'White', '2017-01-24', -1, 2.95, -2.95),
        ('ZYX', 'Coffee Mug', 'Plastic', '8 oz', 'White', '2017-02-01', -3, 2.95, -8.85),
        ('ZYX', 'Coffee Mug', 'Plastic', '8 oz', 'White', '2017-02-08', -7, 2.95, -20.65),
        ('ZYX', 'Coffee Mug', 'Plastic', '8 oz', 'White', '2017-02-15', -6, 2.95, -17.7),
        ('ZYX', 'Coffee Mug', 'Plastic', '8 oz', 'White', '2017-02-22', -2, 2.95, -5.9)
) n ([Manufacturer], [Description], [Material], [Size], [Color], [Date], [qty], [unit price], [extended price])

--Copy the #p data into #p2 and put it in order for FIFO processing
SELECT IDENTITY(int, 1, 1) as ID,
       ROW_NUMBER() OVER (PARTITION BY [Manufacturer],
                                       [Description],
                                       [Material],
                                       [Size],
                                       [Color]
                          ORDER BY [Manufacturer],
                                   [Description],
                                   [Material],
                                   [Size],
                                   [Color],
                                   [Date],
                                   qty DESC
                         ) as rn,
       #p.*
INTO #p2
FROM #p
ORDER BY [Manufacturer],
         [Description],
         [Material],
         [Size],
         [Color],
         [Date],
         qty DESC

--Put the tvf results into the #fifo table
SELECT CAST([ID] as Int) as ID,
       [QTY],
       [EB],
       [GM],
       [COGS],
       [UP],
       [LP],
       [COGSC],
       [GMC],
       [GMP],
       [CGMP]
INTO #fifo
FROM wct.FIFOtvf('SELECT ID, rn,qty,[extended price] FROM #p2 ORDER BY ID')

--JOIN to the source data to produce the inventory output
SELECT #p2.[Manufacturer],
       #p2.[Description],
       #p2.[Material],
       #p2.[Size],
       #p2.[Color],
       #p2.[Date],
       #p2.[qty],
       #p2.[unit price],
       #p2.[extended price],
       f.[QTY] as [Inventory-On-Hand],
       f.[EB] as [Inventory Cost],
       f.[GM] as [Gross Margin on Sales],
       f.[COGS] as [Cost-of-Goods Sold],
       f.[UP] as [Average Price],
       f.[LP] as [Last Price],
       f.[COGSC] as [Cumulative COGS],
       f.[GMC] as [Cumulative Gross Margin],
       f.[GMP] as [GM Percentage],
       f.[CGMP] as [Cumulative GM Percentage]
FROM #p2
    INNER JOIN #FIFO f
        ON #p2.ID = f.[ID]
ORDER BY #p2.id

This produces the following result.

ManufacturerDescriptionMaterialSizeColorDateqtyunit priceextended priceInventory-On-HandInventory CostGross Margin on SalesCost-of-Goods SoldAverage PriceLast PriceCumulative COGSCumulative Gross MarginGM PercentageCumulative GM Percentage
ZYXCoffee MugCeramic12 ozBlack2017-01-15501.1256.005056001.121.1200NULLNULL
ZYXCoffee MugCeramic12 ozBlack2017-02-15501.1557.50100113.5001.1351.1500NULLNULL
ZYXCoffee MugCeramic12 ozWhite2017-01-15501.1256.005056001.121.1200NULLNULL
ZYXCoffee MugCeramic12 ozWhite2017-02-15501.1557.50100113.5001.1351.1500NULLNULL
ZYXCoffee MugCeramic16 ozBlack2017-01-15501.1256.005056001.121.1200NULLNULL
ZYXCoffee MugCeramic16 ozBlack2017-02-15501.1557.50100113.5001.1351.1500NULLNULL
ZYXCoffee MugCeramic16 ozWhite2017-01-15501.1256.005056001.121.1200NULLNULL
ZYXCoffee MugCeramic16 ozWhite2017-02-15501.1557.50100113.5001.1351.1500NULLNULL
ZYXCoffee MugCeramic20 ozBlack2017-01-15501.1256.005056001.121.1200NULLNULL
ZYXCoffee MugCeramic20 ozBlack2017-02-15501.1557.50100113.5001.1351.1500NULLNULL
ZYXCoffee MugCeramic20 ozWhite2017-01-15501.1256.005056001.121.1200NULLNULL
ZYXCoffee MugCeramic20 ozWhite2017-02-15501.1557.50100113.5001.1351.1500NULLNULL
ZYXCoffee MugCeramic8 ozBlack2017-01-15501.1256.005056001.121.1200NULLNULL
ZYXCoffee MugCeramic8 ozBlack2017-02-15501.1557.50100113.5001.1351.1500NULLNULL
ZYXCoffee MugCeramic8 ozWhite2017-01-15501.1256.005056001.121.1200NULLNULL
ZYXCoffee MugCeramic8 ozWhite2017-02-15501.1557.50100113.5001.1351.1500NULLNULL
ZYXCoffee MugPlastic12 ozBlack2017-01-15500.8944.505044.5000.890.8900NULLNULL
ZYXCoffee MugPlastic12 ozBlack2017-02-15501.1557.50100102001.021.1500NULLNULL
ZYXCoffee MugPlastic12 ozWhite2017-01-15500.8944.505044.5000.890.8900NULLNULL
ZYXCoffee MugPlastic12 ozWhite2017-02-15501.1557.50100102001.021.1500NULLNULL
ZYXCoffee MugPlastic16 ozBlack2017-01-15500.8944.505044.5000.890.8900NULLNULL
ZYXCoffee MugPlastic16 ozBlack2017-02-15501.1557.50100102001.021.1500NULLNULL
ZYXCoffee MugPlastic16 ozWhite2017-01-15500.8944.505044.5000.890.8900NULLNULL
ZYXCoffee MugPlastic16 ozWhite2017-02-15501.1557.50100102001.021.1500NULLNULL
ZYXCoffee MugPlastic20 ozBlack2017-01-15500.8944.505044.5000.890.8900NULLNULL
ZYXCoffee MugPlastic20 ozBlack2017-02-15501.1557.50100102001.021.1500NULLNULL
ZYXCoffee MugPlastic20 ozWhite2017-01-15500.8944.505044.5000.890.8900NULLNULL
ZYXCoffee MugPlastic20 ozWhite2017-02-15501.1557.50100102001.021.1500NULLNULL
ZYXCoffee MugPlastic8 ozBlack2017-01-15500.8944.505044.5000.890.8900NULLNULL
ZYXCoffee MugPlastic8 ozBlack2017-02-15501.1557.50100102001.021.1500NULLNULL
ZYXCoffee MugPlastic8 ozWhite2017-01-15500.8944.505044.5000.890.8900NULLNULL
ZYXCoffee MugPlastic8 ozWhite2017-01-16-52.95-14.754540.0510.3-4.450.890.89-4.4510.30.6983050847457630.698305084745763
ZYXCoffee MugPlastic8 ozWhite2017-01-23-22.95-5.904338.274.11999999999999-1.780000000000010.890.89-6.2314.420.6983050847457610.698305084745763
ZYXCoffee MugPlastic8 ozWhite2017-01-24-12.95-2.954237.382.06-0.8900000000000010.890.89-7.1216.480.6983050847457630.698305084745763
ZYXCoffee MugPlastic8 ozWhite2017-02-01-32.95-8.853934.716.18000000000001-2.669999999999990.890.89-9.7922.660.6983050847457630.698305084745763
ZYXCoffee MugPlastic8 ozWhite2017-02-08-72.95-20.653228.4814.42-6.230.890.89-16.0237.080.6983050847457630.698305084745763
ZYXCoffee MugPlastic8 ozWhite2017-02-15501.1557.508285.98001.048536585365851.15-16.0237.08NULL0.698305084745763
ZYXCoffee MugPlastic8 ozWhite2017-02-15-62.95-17.707680.6412.36-5.341.061052631578951.15-21.3649.440.6983050847457620.698305084745763
ZYXCoffee MugPlastic8 ozWhite2017-02-22-22.95-5.907478.864.12-1.781.065675675675681.15-23.1453.560.6983050847457630.698305084745763

See Also

FIFO - Calculate FIFO (first in, first out) values in an ordered resultant table.

LIFO - Calculate LIFO (last in, first out) values in an ordered resultant table.

WAC - Calculate running weighted average cost in an ordered resultant table.

FIFOEnd - Calculate the ending FIFO balances in an ordered resultant table.

LIFOend - Calculate the ending LIFO balances in an ordered resultant table.

LIFOtvf - Calculate the running LIFO balances in an ordered resultant table.

WACtvf - Calculate running weighted-average cost values in an ordered resultant table.