Logo

SQL Server LIFOtvf Function

Updated 2023-10-12 14:25:00.540000

Description

Use the SQL Server table-valued function LIFOtvf to calculate running LIFO (Last In, First Out) values in an ordered resultant table. LIFOtvf calculates balances for each value from the first value to the last value in the ordered group or partition. LIFOtvf 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.

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

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

LIFOtvf requires monotonically ascending row numbers within a partition.

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

Syntax

SELECT * FROM [westclintech].[wct].[LIFOtvf] (
   <@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
UPfloatunit price
LPfloatlast item-added price
COGSCfloatcumulative cost-of-goods-sold
GMCfloatcumulative gross margin
GMPfloatgross margin percentage
CGMPfloatcumulative 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 LIFO 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 LIFO Values and store in temp table
SELECT CAST([ID] as Int) as ID,
       [QTY],
       [EB],
       [GM],
       [COGS],
       [UP],
       [LP],
       [COGSC],
       [GMC],
       [GMP],
       [CGMP]
INTO #lifo
FROM wct.LIFOtvf('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,
       l.[QTY] as [Inventory-On-Hand],
       l.[EB] as [Inventory Cost],
       l.[GM] as [Gross Margin on Sales],
       l.[COGS] as [Cost-of-Goods Sold],
       l.[UP] as [Average Price],
       l.[LP] as [Last Price],
       l.[COGSC] as [Cumulative COGS],
       l.[GMC] as [Cumulative Gross Margin],
       l.[GMP] as [GM Percentage],
       l.[CGMP] as [Cumulative GM Percentage]
FROM #c c
    INNER JOIN #LIFO l
        ON c.trn = l.[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.692383041.6425.1199999999999-2042.5712.7813.01-2042.5725.11999999999990.01214882308276380.0121488230827638
32130310ABC2013-03-10-63.0012.61-794.431752236.5-10.7099999999999-805.1412.7813.01-2847.7114.41-0.01348136399682780.00503472950120888
41130310ABC2013-03-10463.0013.386194.946388431.440013.215423197492213.38-2847.7114.41NULL0.00503472950120888
26130320ABC2013-03-20-92.0013.64-1254.885467200.4823.920000000001-1230.9613.187692307692313.38-4078.6738.3300000000010.01906158357771340.00931017731357808
25130408ABC2013-04-08298.0012.983868.0484411068.520013.114360189573512.98-4078.6738.330000000001NULL0.00931017731357808
48130430ABC2013-04-30229.0013.323050.28107314118.80013.158247903075513.32-4078.6738.330000000001NULL0.00931017731357808
90130430ABC2013-04-30-191.0013.49-2576.5988211574.6832.4699999999993-2544.1213.123219954648513.32-6622.7970.80000000000030.0126019273535950.0105772836400198
49130508ABC2013-05-08238.0012.793044.02112014618.70013.052410714285712.79-6622.7970.8000000000003NULL0.0105772836400198
20130518ABC2013-05-18298.0013.233942.54141818561.240013.089732016925213.23-6622.7970.8000000000003NULL0.0105772836400198
89130611ABC2013-06-11130.0013.021692.60154820253.840013.083875968992213.02-6622.7970.8000000000003NULL0.0105772836400198
2130617ABC2013-06-17313.0012.934047.09186124300.930013.057995701235912.93-6622.7970.8000000000003NULL0.0105772836400198
12130621ABC2013-06-21-326.0012.73-4149.98153520084.58-66.369999999999-4216.3513.084416938110812.93-10839.144.43000000000131-0.01599284815830410.000408537040845525
93130622ABC2013-06-22-227.0013.47-3057.69130817105.9479.0500000000006-2978.6413.077935779816512.93-13817.7883.4800000000020.02585284970026410.006005211038424
7130722ABC2013-07-22599.0013.658176.35190725282.290013.257624541164113.65-13817.7883.480000000002NULL0.006005211038424
71130731ABC2013-07-3179.0013.551070.45198626352.740013.269254783484413.55-13817.7883.480000000002NULL0.006005211038424
10130908ABC2013-09-08-386.0013.65-5268.90160021091.747.89999999999964-526113.182337513.55-19078.7891.38000000000160.001499364193664640.00476678337583002
39130908ABC2013-09-08490.0012.696218.10209027309.840013.066909090909112.69-19078.7891.3800000000016NULL0.00476678337583002
61130916ABC2013-09-16-202.0012.94-2613.88188824746.4650.4999999999991-2563.3813.107235169491512.69-21642.16141.8800000000010.01931993817619750.00651302513216101
76131009ABC2013-10-09249.0013.473354.03213728100.490013.149503977538613.47-21642.16141.880000000001NULL0.00651302513216101
88131009ABC2013-10-09187.0013.362498.32232430598.810013.166441480206513.36-21642.16141.880000000001NULL0.00651302513216101
16131107ABC2013-11-0727.0012.68342.36235130941.170013.160854955338212.68-21642.16141.880000000001NULL0.00651302513216101
86131114ABC2013-11-14-386.0012.40-4786.40196525783.65-371.120000000001-5157.5213.121450381679412.68-26799.68-229.24-0.0775363530001673-0.00862763281300574
8131231ABC2013-12-31-145.0013.19-1912.55182023883.5412.4399999999994-1900.1113.122824175824212.68-28699.79-216.8000000000010.00650440511359147-0.00761156044361918
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.8081028218.6-48.4400000000051-5999.2400000000134.837777777777834.48-11315.95-357.990000000004-0.00814008200578159-0.03266940196898
70130702GHI2013-07-02-162.0035.48-5747.7664822636.08165.240000000003-5582.5234.932222222222234.48-16898.47-192.7500000000010.0287485907553557-0.0115379642421878
96130716GHI2013-07-16-347.0035.29-12245.6330110598.21207.759999999998-12037.8735.2134.48-28936.3415.00999999999750.01696605237950180.000518455961466304
63130806GHI2013-08-06445.0033.5014907.5074625505.710034.189959785522833.5-28936.3415.0099999999975NULL0.000518455961466304
77130825GHI2013-08-25272.0033.869209.92101834715.630034.101797642436133.86-28936.3415.0099999999975NULL0.000518455961466304
95130908GHI2013-09-08103.0035.423648.26112138363.890034.222917038358635.42-28936.3415.0099999999975NULL0.000518455961466304
6130924GHI2013-09-24328.0034.9511463.60144949827.490034.387501725327834.95-28936.3415.0099999999975NULL0.000518455961466304
17130924GHI2013-09-24-291.0035.69-10385.79115839657.04215.340000000004-10170.4534.246148531951634.95-39106.79230.3500000000010.02073409918744780.0058557892109086
85131002GHI2013-10-02-295.0032.51-9590.4586329467.33-599.260000000002-10189.7134.145225955967634.95-49296.5-368.910000000001-0.062485076299861-0.00753991766199808
52131014GHI2013-10-14-91.0033.12-3013.9277226386.07-67.3399999999983-3081.2634.178847150259134.95-52377.76-436.249999999999-0.0223429951690816-0.00839887019072028
73131028GHI2013-10-28-46.0034.65-1593.9072624835.7143.5399999999995-1550.3634.208966942148834.95-53928.12-392.710.0273166447079487-0.00733551867819822
50131103GHI2013-11-03-246.0035.61-8760.0648016594.71519.059999999999-824134.572312534.95-62169.12126.350.05925301881493960.00202823736621619
87131116GHI2013-11-16-320.0034.16-10931.201605633.6-29.909999999998-10961.1135.2134.95-73130.2396.4400000000019-0.002736204625292560.00131700649503797
94131117GHI2013-11-17-92.0032.65-3003.80682394.28-235.52-3239.3235.2134.95-76369.55-139.079999999999-0.0784073506891272-0.00182446730290393
81131123GHI2013-11-23187.0033.866331.822558726.10034.2233.86-76369.55-139.079999999999NULL-0.00182446730290393
51131206GHI2013-12-0685.0033.642859.4034011585.50034.07533.64-76369.55-139.079999999999NULL-0.00182446730290393
75131216GHI2013-12-16500.0033.5516775.0084028360.50033.762533.55-76369.55-139.079999999999NULL-0.00182446730290393
13131221GHI2013-12-2172.0034.382475.3691230835.860033.8112534.38-76369.55-139.079999999999NULL-0.00182446730290393
65131227GHI2013-12-27376.0033.4912592.24128843428.10033.717468944099433.49-76369.55-139.079999999999NULL-0.00182446730290393
53140102GHI2014-01-02396.0035.5214065.92168457494.020034.141342042755335.52-76369.55-139.079999999999NULL-0.00182446730290393
18140125GHI2014-01-25-78.0035.46-2765.88160654723.46-4.67999999999756-2770.5634.074383561643835.52-79140.11-143.759999999996-0.00169204737732568-0.00181983091623849
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.7070116383.9383.100000000001-4422.623.37218259629124.04-4422.6383.1000000000010.07971783507085360.0797178350708536
59130320XYZ2013-03-20-20.0023.59-471.8068115926.714.5999999999993-457.20000000000123.387224669603524.04-4879.8397.7000000000010.03094531581178310.0753576504026529
35130321XYZ2013-03-21275.0024.836828.2595622754.950023.802248953974924.83-4879.8397.700000000001NULL0.0753576504026529
98130413XYZ2013-04-13243.0024.255892.75119928647.70023.892994161801524.25-4879.8397.700000000001NULL0.0753576504026529
24130418XYZ2013-04-18275.0024.836828.25147435475.950024.067808683853524.83-4879.8397.700000000001NULL0.0753576504026529
60130419XYZ2013-04-19277.0022.836323.91175141799.860023.871993146773322.83-4879.8397.700000000001NULL0.0753576504026529
83130428XYZ2013-04-28142.0023.133284.46189345084.320023.816333861595423.13-4879.8397.700000000001NULL0.0753576504026529
34130507XYZ2013-05-0755.0023.431288.65194846372.970023.805426078028723.43-4879.8397.700000000001NULL0.0753576504026529
99130515XYZ2013-05-1597.0025.132437.61204548810.580023.868254278728625.13-4879.8397.700000000001NULL0.0753576504026529
19130516XYZ2013-05-16315.0023.577424.55236056235.130023.828444915254223.57-4879.8397.700000000001NULL0.0753576504026529
30130519XYZ2013-05-19467.0023.1510811.05282767046.180023.716370711001123.15-4879.8397.700000000001NULL0.0753576504026529
29130602XYZ2013-06-02114.0024.292769.06294169815.240023.73860591635524.29-4879.8397.700000000001NULL0.0753576504026529
91130615XYZ2013-06-15545.0023.8813014.60348682829.840023.76071141709723.88-4879.8397.700000000001NULL0.0753576504026529
23130619XYZ2013-06-19296.0023.466944.163782897740023.73717609730323.46-4879.8397.700000000001NULL0.0753576504026529
47130619XYZ2013-06-19413.0025.1810399.344195100173.340023.879222884386225.18-4879.8397.700000000001NULL0.0753576504026529
74130619XYZ2013-06-19202.0022.954635.904397104809.240023.836534000454922.95-4879.8397.700000000001NULL0.0753576504026529
55130630XYZ2013-06-30-272.0023.45-6378.40412598410.74-20.0999999999858-6398.4999999999923.857149090909122.95-11278.3377.600000000015-0.003151260504199460.0323956108065456
14130705XYZ2013-07-05-277.0025.01-6927.77384891435.88-47.0900000000001-6974.8623.761923076923122.95-18253.16330.510000000015-0.006797281087564990.0177849692767906
5130706XYZ2013-07-06-170.0023.46-3988.20367887334.16-113.520000000016-4101.7200000000223.745013594344822.95-22354.88216.989999999999-0.02846396870769170.00961329300585192
58130722XYZ2013-07-22-88.0024.64-2168.32359085269.68103.840000000004-2064.4823.75222.95-24419.36320.8300000000030.04788961038961230.0129679683139056
57130813XYZ2013-08-13-163.0023.65-3854.95342781420.926.19000000000506-3848.7599999999923.758657718120822.95-28268.12327.0200000000080.00160572770074970.0114362090900764
54130831XYZ2013-08-31-61.0023.23-1417.03336679964.24-39.6500000000076-1456.6800000000123.75645870469422.95-29724.8287.37-0.02798105897546810.00957511569473318
11130906XYZ2013-09-06-13.0023.97-311.61335379653.81.16999999999769-310.44000000000223.75597971965422.95-30035.24288.5399999999980.003754693366700960.00951530449040318
27130906XYZ2013-09-06-147.0023.81-3500.07320676143.44-10.2900000000004-3510.3623.750293200249522.95-33545.6278.249999999998-0.00293994120117610.00822644376675031
36130917XYZ2013-09-1792.0024.602263.20329878406.640023.773996361431224.6-33545.6278.249999999998NULL0.00822644376675031
92130924XYZ2013-09-24-248.0024.58-6095.84305072418.16107.360000000004-5988.4823.743659016393424.6-39534.08385.6100000000020.01761201081393280.0096596441505433
42131011XYZ2013-10-11-250.0023.12-5780.00280066421.13-217.029999999999-5997.0323.721832142857124.6-45531.11168.580000000003-0.03754844290657420.00368886528551951
22131012XYZ2013-10-12596.0023.1613803.36339680224.490023.62323027090723.16-45531.11168.580000000003NULL0.00368886528551951
40131013XYZ2013-10-13359.0023.918583.69375588808.180023.650647137150523.91-45531.11168.580000000003NULL0.00368886528551951
1131019XYZ2013-10-19424.0025.1310655.12417999463.30023.800741804259425.13-45531.11168.580000000003NULL0.00368886528551951
9131025XYZ2013-10-25-153.0024.31-3719.43402695618.41-125.46-3844.8923.750226030799825.13-4937643.1200000000035-0.03373097490744540.000872536783334132
62131027XYZ2013-10-27-248.0024.71-6128.08377889386.17-104.159999999991-6232.2399999999923.659653255690825.13-55608.24-61.0399999999873-0.0169971671388087-0.00109888527234473
45131030XYZ2013-10-30-350.0023.36-8176.00342880989.61-220.560000000012-8396.5600000000123.625907234539125.13-64004.8-281.6-0.0269765166340524-0.00441911266226429
21131103XYZ2013-11-03-326.0023.24-7576.24310273415.452.08000000001084-7574.1599999999923.667134107027725.13-71578.96-279.5199999999890.00027454251713394-0.00392036739699482
97131125XYZ2013-11-25-278.0023.18-6444.04282466976.975.55999999998949-6438.4800000000123.717057365439125.13-78017.44-273.9599999999990.000862812769627359-0.00352389679494665
28131209XYZ2013-12-09315.0024.467704.90313974681.870023.79161197833724.46-78017.44-273.959999999999NULL-0.00352389679494665
79131223XYZ2013-12-23-376.0025.12-9445.12276365564.58327.830000000007-9117.2899999999923.729489685124924.46-87134.7353.8700000000080.03470892905542830.000617856004110721
33140102XYZ2014-01-02-196.0022.98-4504.08256761027.18-33.3199999999943-4537.3999999999923.773735878457324.46-91672.1320.5500000000138-0.007397737162748940.000224118217506717
84140104XYZ2014-01-04261.0024.466384.06282867411.240023.83707213578524.46-91672.1320.5500000000138NULL0.000224118217506717
64140109XYZ2014-01-09-253.0024.46-6188.38257561222.862.72848410531878E-12-6188.3823.775867961165124.46-97860.5120.55000000001654.40904421725683E-160.000209948686702172
78140112XYZ2014-01-12332.0024.288060.96290769283.820023.833443412452724.28-97860.5120.5500000000165NULL0.000209948686702172
80140126XYZ2014-01-26404.0024.239788.92331179072.740023.881830262760524.23-97860.5120.5500000000165NULL0.000209948686702172
82140131XYZ2014-01-31548.0023.6512960.20385992032.940023.848909043793723.65-97860.5120.5500000000165NULL0.000209948686702172
66140203XYZ2014-02-03459.0023.7010878.304318102911.240023.833080129689723.7-97860.5120.5500000000165NULL0.000209948686702172
72140204XYZ2014-02-04-208.0024.36-5066.88411097981.64137.279999999994-4929.6000000000123.839815085158223.7-102790.11157.8300000000110.02709359605911220.00153310498490801
31140205XYZ2014-02-0542.0024.391024.38415299006.020023.84538053949924.39-102790.11157.830000000011NULL0.00153310498490801

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 LIFO Values and store in temp table
SELECT CAST([ID] as Int) as ID,
       [QTY],
       [EB],
       [GM],
       [COGS],
       [UP],
       [LP],
       [COGSC],
       [GMC],
       [GMP],
       [CGMP]
INTO   #lifo
  FROM wct.LIFOtvf('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,
       l.[QTY] as [GBP Position],
       l.[EB] as [USD Position],
       l.[GM] as [P/L],
       l.[COGS] as [Cost-of-Goods Sold],
       l.[UP] as [Average Price],
       l.[LP] as [Last Price],
       l.[COGSC] as [Cumulative COGS],
       l.[GMC] as [Cumulative P/L],
       l.[GMP] as [GM Percentage],
       l.[CGMP] as [Cumulative GM Percentage]
  FROM #fx c
 INNER JOIN #LIFO l
    ON c.trn = l.[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.0020000003241000-25000-81150001.62051.623-11375000-33000-0.0030902348578492-0.0029095397637101
107GBP-10000000.001.602USD16020000.00-8000000-12816000-37000-32410001.6021.602-14616000-70000-0.0115480649188514-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 LIFOtvf 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 LIFO Values and store in temp table
SELECT CAST([ID] as Int) as ID,
       [QTY],
       [EB],
       [GM],
       [COGS],
       [UP],
       [LP],
       [COGSC],
       [GMC],
       [GMP],
       [CGMP]
INTO   #LIFO
  FROM wct.LIFOtvf('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,
       l.[QTY] as [Inventory-On-Hand],
       l.[EB] as [Inventory Cost],
       l.[GM] as [Gross Margin on Sales],
       l.[COGS] as [Cost-of-Goods Sold],
       l.[UP] as [Average Price],
       l.[LP] as [Last Price],
       l.[COGSC] as [Cumulative COGS],
       l.[GMC] as [Cumulative Gross Margin],
       l.[GMP] as [GM Percentage],
       l.[CGMP] as [Cumulative GM Percentage]
  FROM #inv i
 INNER JOIN #LIFO l
    ON i.ID  = l.[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.692383041.6425.1199999999999-2042.5712.7813.01-2042.5725.11999999999990.01214882308276380.0121488230827638
32130310ABC2013-03-10-63.0012.61-794.431752236.5-10.7099999999999-805.1412.7813.01-2847.7114.41-0.01348136399682780.00503472950120888
41130310ABC2013-03-10463.0013.386194.946388431.440013.215423197492213.38-2847.7114.41NULL0.00503472950120888
26130320ABC2013-03-20-92.0013.64-1254.885467200.4823.920000000001-1230.9613.187692307692313.38-4078.6738.3300000000010.01906158357771340.00931017731357808
25130408ABC2013-04-08298.0012.983868.0484411068.520013.114360189573512.98-4078.6738.330000000001NULL0.00931017731357808
48130430ABC2013-04-30229.0013.323050.28107314118.80013.158247903075513.32-4078.6738.330000000001NULL0.00931017731357808
90130430ABC2013-04-30-191.0013.49-2576.5988211574.6832.4699999999993-2544.1213.123219954648513.32-6622.7970.80000000000030.0126019273535950.0105772836400198
49130508ABC2013-05-08238.0012.793044.02112014618.70013.052410714285712.79-6622.7970.8000000000003NULL0.0105772836400198
20130518ABC2013-05-18298.0013.233942.54141818561.240013.089732016925213.23-6622.7970.8000000000003NULL0.0105772836400198
89130611ABC2013-06-11130.0013.021692.60154820253.840013.083875968992213.02-6622.7970.8000000000003NULL0.0105772836400198
2130617ABC2013-06-17313.0012.934047.09186124300.930013.057995701235912.93-6622.7970.8000000000003NULL0.0105772836400198
12130621ABC2013-06-21-326.0012.73-4149.98153520084.58-66.369999999999-4216.3513.084416938110812.93-10839.144.43000000000131-0.01599284815830410.000408537040845525
93130622ABC2013-06-22-227.0013.47-3057.69130817105.9479.0500000000006-2978.6413.077935779816512.93-13817.7883.4800000000020.02585284970026410.006005211038424
7130722ABC2013-07-22599.0013.658176.35190725282.290013.257624541164113.65-13817.7883.480000000002NULL0.006005211038424
71130731ABC2013-07-3179.0013.551070.45198626352.740013.269254783484413.55-13817.7883.480000000002NULL0.006005211038424
10130908ABC2013-09-08-386.0013.65-5268.90160021091.747.89999999999964-526113.182337513.55-19078.7891.38000000000160.001499364193664640.00476678337583002
39130908ABC2013-09-08490.0012.696218.10209027309.840013.066909090909112.69-19078.7891.3800000000016NULL0.00476678337583002
61130916ABC2013-09-16-202.0012.94-2613.88188824746.4650.4999999999991-2563.3813.107235169491512.69-21642.16141.8800000000010.01931993817619750.00651302513216101
76131009ABC2013-10-09249.0013.473354.03213728100.490013.149503977538613.47-21642.16141.880000000001NULL0.00651302513216101
88131009ABC2013-10-09187.0013.362498.32232430598.810013.166441480206513.36-21642.16141.880000000001NULL0.00651302513216101
16131107ABC2013-11-0727.0012.68342.36235130941.170013.160854955338212.68-21642.16141.880000000001NULL0.00651302513216101
86131114ABC2013-11-14-386.0012.40-4786.40196525783.65-371.120000000001-5157.5213.121450381679412.68-26799.68-229.24-0.0775363530001673-0.00862763281300574
8131231ABC2013-12-31-145.0013.19-1912.55182023883.5412.4399999999994-1900.1113.122824175824212.68-28699.79-216.8000000000010.00650440511359147-0.00761156044361918
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.8081028218.6-48.4400000000051-5999.2400000000134.837777777777834.48-11315.95-357.990000000004-0.00814008200578159-0.03266940196898
70130702GHI2013-07-02-162.0035.48-5747.7664822636.08165.240000000003-5582.5234.932222222222234.48-16898.47-192.7500000000010.0287485907553557-0.0115379642421878
96130716GHI2013-07-16-347.0035.29-12245.6330110598.21207.759999999998-12037.8735.2134.48-28936.3415.00999999999750.01696605237950180.000518455961466304
63130806GHI2013-08-06445.0033.5014907.5074625505.710034.189959785522833.5-28936.3415.0099999999975NULL0.000518455961466304
77130825GHI2013-08-25272.0033.869209.92101834715.630034.101797642436133.86-28936.3415.0099999999975NULL0.000518455961466304
95130908GHI2013-09-08103.0035.423648.26112138363.890034.222917038358635.42-28936.3415.0099999999975NULL0.000518455961466304
6130924GHI2013-09-24328.0034.9511463.60144949827.490034.387501725327834.95-28936.3415.0099999999975NULL0.000518455961466304
17130924GHI2013-09-24-291.0035.69-10385.79115839657.04215.340000000004-10170.4534.246148531951634.95-39106.79230.3500000000010.02073409918744780.0058557892109086
85131002GHI2013-10-02-295.0032.51-9590.4586329467.33-599.260000000002-10189.7134.145225955967634.95-49296.5-368.910000000001-0.062485076299861-0.00753991766199808
52131014GHI2013-10-14-91.0033.12-3013.9277226386.07-67.3399999999983-3081.2634.178847150259134.95-52377.76-436.249999999999-0.0223429951690816-0.00839887019072028
73131028GHI2013-10-28-46.0034.65-1593.9072624835.7143.5399999999995-1550.3634.208966942148834.95-53928.12-392.710.0273166447079487-0.00733551867819822
50131103GHI2013-11-03-246.0035.61-8760.0648016594.71519.059999999999-824134.572312534.95-62169.12126.350.05925301881493960.00202823736621619
87131116GHI2013-11-16-320.0034.16-10931.201605633.6-29.909999999998-10961.1135.2134.95-73130.2396.4400000000019-0.002736204625292560.00131700649503797
94131117GHI2013-11-17-92.0032.65-3003.80682394.28-235.52-3239.3235.2134.95-76369.55-139.079999999999-0.0784073506891272-0.00182446730290393
81131123GHI2013-11-23187.0033.866331.822558726.10034.2233.86-76369.55-139.079999999999NULL-0.00182446730290393
51131206GHI2013-12-0685.0033.642859.4034011585.50034.07533.64-76369.55-139.079999999999NULL-0.00182446730290393
75131216GHI2013-12-16500.0033.5516775.0084028360.50033.762533.55-76369.55-139.079999999999NULL-0.00182446730290393
13131221GHI2013-12-2172.0034.382475.3691230835.860033.8112534.38-76369.55-139.079999999999NULL-0.00182446730290393
65131227GHI2013-12-27376.0033.4912592.24128843428.10033.717468944099433.49-76369.55-139.079999999999NULL-0.00182446730290393
53140102GHI2014-01-02396.0035.5214065.92168457494.020034.141342042755335.52-76369.55-139.079999999999NULL-0.00182446730290393
18140125GHI2014-01-25-78.0035.46-2765.88160654723.46-4.67999999999756-2770.5634.074383561643835.52-79140.11-143.759999999996-0.00169204737732568-0.00181983091623849
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.7070116383.9383.100000000001-4422.623.37218259629124.04-4422.6383.1000000000010.07971783507085360.0797178350708536
59130320XYZ2013-03-20-20.0023.59-471.8068115926.714.5999999999993-457.20000000000123.387224669603524.04-4879.8397.7000000000010.03094531581178310.0753576504026529
35130321XYZ2013-03-21275.0024.836828.2595622754.950023.802248953974924.83-4879.8397.700000000001NULL0.0753576504026529
98130413XYZ2013-04-13243.0024.255892.75119928647.70023.892994161801524.25-4879.8397.700000000001NULL0.0753576504026529
24130418XYZ2013-04-18275.0024.836828.25147435475.950024.067808683853524.83-4879.8397.700000000001NULL0.0753576504026529
60130419XYZ2013-04-19277.0022.836323.91175141799.860023.871993146773322.83-4879.8397.700000000001NULL0.0753576504026529
83130428XYZ2013-04-28142.0023.133284.46189345084.320023.816333861595423.13-4879.8397.700000000001NULL0.0753576504026529
34130507XYZ2013-05-0755.0023.431288.65194846372.970023.805426078028723.43-4879.8397.700000000001NULL0.0753576504026529
99130515XYZ2013-05-1597.0025.132437.61204548810.580023.868254278728625.13-4879.8397.700000000001NULL0.0753576504026529
19130516XYZ2013-05-16315.0023.577424.55236056235.130023.828444915254223.57-4879.8397.700000000001NULL0.0753576504026529
30130519XYZ2013-05-19467.0023.1510811.05282767046.180023.716370711001123.15-4879.8397.700000000001NULL0.0753576504026529
29130602XYZ2013-06-02114.0024.292769.06294169815.240023.73860591635524.29-4879.8397.700000000001NULL0.0753576504026529
91130615XYZ2013-06-15545.0023.8813014.60348682829.840023.76071141709723.88-4879.8397.700000000001NULL0.0753576504026529
23130619XYZ2013-06-19296.0023.466944.163782897740023.73717609730323.46-4879.8397.700000000001NULL0.0753576504026529
47130619XYZ2013-06-19413.0025.1810399.344195100173.340023.879222884386225.18-4879.8397.700000000001NULL0.0753576504026529
74130619XYZ2013-06-19202.0022.954635.904397104809.240023.836534000454922.95-4879.8397.700000000001NULL0.0753576504026529
55130630XYZ2013-06-30-272.0023.45-6378.40412598410.74-20.0999999999858-6398.4999999999923.857149090909122.95-11278.3377.600000000015-0.003151260504199460.0323956108065456
14130705XYZ2013-07-05-277.0025.01-6927.77384891435.88-47.0900000000001-6974.8623.761923076923122.95-18253.16330.510000000015-0.006797281087564990.0177849692767906
5130706XYZ2013-07-06-170.0023.46-3988.20367887334.16-113.520000000016-4101.7200000000223.745013594344822.95-22354.88216.989999999999-0.02846396870769170.00961329300585192
58130722XYZ2013-07-22-88.0024.64-2168.32359085269.68103.840000000004-2064.4823.75222.95-24419.36320.8300000000030.04788961038961230.0129679683139056
57130813XYZ2013-08-13-163.0023.65-3854.95342781420.926.19000000000506-3848.7599999999923.758657718120822.95-28268.12327.0200000000080.00160572770074970.0114362090900764
54130831XYZ2013-08-31-61.0023.23-1417.03336679964.24-39.6500000000076-1456.6800000000123.75645870469422.95-29724.8287.37-0.02798105897546810.00957511569473318
11130906XYZ2013-09-06-13.0023.97-311.61335379653.81.16999999999769-310.44000000000223.75597971965422.95-30035.24288.5399999999980.003754693366700960.00951530449040318
27130906XYZ2013-09-06-147.0023.81-3500.07320676143.44-10.2900000000004-3510.3623.750293200249522.95-33545.6278.249999999998-0.00293994120117610.00822644376675031
36130917XYZ2013-09-1792.0024.602263.20329878406.640023.773996361431224.6-33545.6278.249999999998NULL0.00822644376675031
92130924XYZ2013-09-24-248.0024.58-6095.84305072418.16107.360000000004-5988.4823.743659016393424.6-39534.08385.6100000000020.01761201081393280.0096596441505433
42131011XYZ2013-10-11-250.0023.12-5780.00280066421.13-217.029999999999-5997.0323.721832142857124.6-45531.11168.580000000003-0.03754844290657420.00368886528551951
22131012XYZ2013-10-12596.0023.1613803.36339680224.490023.62323027090723.16-45531.11168.580000000003NULL0.00368886528551951
40131013XYZ2013-10-13359.0023.918583.69375588808.180023.650647137150523.91-45531.11168.580000000003NULL0.00368886528551951
1131019XYZ2013-10-19424.0025.1310655.12417999463.30023.800741804259425.13-45531.11168.580000000003NULL0.00368886528551951
9131025XYZ2013-10-25-153.0024.31-3719.43402695618.41-125.46-3844.8923.750226030799825.13-4937643.1200000000035-0.03373097490744540.000872536783334132
62131027XYZ2013-10-27-248.0024.71-6128.08377889386.17-104.159999999991-6232.2399999999923.659653255690825.13-55608.24-61.0399999999873-0.0169971671388087-0.00109888527234473
45131030XYZ2013-10-30-350.0023.36-8176.00342880989.61-220.560000000012-8396.5600000000123.625907234539125.13-64004.8-281.6-0.0269765166340524-0.00441911266226429
21131103XYZ2013-11-03-326.0023.24-7576.24310273415.452.08000000001084-7574.1599999999923.667134107027725.13-71578.96-279.5199999999890.00027454251713394-0.00392036739699482
97131125XYZ2013-11-25-278.0023.18-6444.04282466976.975.55999999998949-6438.4800000000123.717057365439125.13-78017.44-273.9599999999990.000862812769627359-0.00352389679494665
28131209XYZ2013-12-09315.0024.467704.90313974681.870023.79161197833724.46-78017.44-273.959999999999NULL-0.00352389679494665
79131223XYZ2013-12-23-376.0025.12-9445.12276365564.58327.830000000007-9117.2899999999923.729489685124924.46-87134.7353.8700000000080.03470892905542830.000617856004110721
33140102XYZ2014-01-02-196.0022.98-4504.08256761027.18-33.3199999999943-4537.3999999999923.773735878457324.46-91672.1320.5500000000138-0.007397737162748940.000224118217506717
84140104XYZ2014-01-04261.0024.466384.06282867411.240023.83707213578524.46-91672.1320.5500000000138NULL0.000224118217506717
64140109XYZ2014-01-09-253.0024.46-6188.38257561222.862.72848410531878E-12-6188.3823.775867961165124.46-97860.5120.55000000001654.40904421725683E-160.000209948686702172
78140112XYZ2014-01-12332.0024.288060.96290769283.820023.833443412452724.28-97860.5120.5500000000165NULL0.000209948686702172
80140126XYZ2014-01-26404.0024.239788.92331179072.740023.881830262760524.23-97860.5120.5500000000165NULL0.000209948686702172
82140131XYZ2014-01-31548.0023.6512960.20385992032.940023.848909043793723.65-97860.5120.5500000000165NULL0.000209948686702172
66140203XYZ2014-02-03459.0023.7010878.304318102911.240023.833080129689723.7-97860.5120.5500000000165NULL0.000209948686702172
72140204XYZ2014-02-04-208.0024.36-5066.88411097981.64137.279999999994-4929.6000000000123.839815085158223.7-102790.11157.8300000000110.02709359605911220.00153310498490801
31140205XYZ2014-02-0542.0024.391024.38415299006.020023.84538053949924.39-102790.11157.830000000011NULL0.00153310498490801

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 LIFO 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 #LIFO table
SELECT CAST([ID] as Int) as ID,
       [QTY],
       [EB],
       [GM],
       [COGS],
       [UP],
       [LP],
       [COGSC],
       [GMC],
       [GMP],
       [CGMP]
INTO   #LIFO
  FROM wct.LIFOtvf('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],
       l.[QTY] as [Inventory-On-Hand],
       l.[EB] as [Inventory Cost],
       l.[GM] as [Gross Margin on Sales],
       l.[COGS] as [Cost-of-Goods Sold],
       l.[UP] as [Average Price],
       l.[LP] as [Last Price],
       l.[COGSC] as [Cumulative COGS],
       l.[GMC] as [Cumulative Gross Margin],
       l.[GMP] as [GM Percentage],
       l.[CGMP] as [Cumulative GM Percentage]
  FROM #p2
 INNER JOIN #LIFO l
    ON #p2.ID = l.[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.18-2.670.890.89-9.7900000000000122.660.6983050847457620.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.707679.0810.8-6.899999999999991.040526315789471.15-22.9247.880.6101694915254240.676271186440678
ZYXCoffee MugPlastic8 ozWhite2017-02-22-22.95-5.907476.783.6-2.31.037567567567571.15-25.2251.480.6101694915254240.671186440677966

See Also

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

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

FIFOtvf - Calculate running FIFO (First In, First Out) values in an ordered resultant table.

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

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

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

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