Logo

SQL Server WACtvf Function

Updated 2023-10-12 14:35:27.103000

Description

Use the SQL Server table-valued function WACtvf to calculate running weighted-average cost values in an ordered resultant table. WACtvf calculates balances for each value from the first value to the last value in the ordered group or partition. WACtvf 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.

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

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

WACtvf requires monotonically ascending row numbers within a partition.

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

Syntax

SELECT * FROM [westclintech].[wct].[WACtvf] (
   <@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
GMfloat gross margin on sale
COGSfloat cost of goods sold
UPfloataverage inventory price per unit
LPfloat last item-added price
COGSCfloat cumulative cost-of-goods-sold
GMCfloat cumulative 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 weighted-average cost 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 WAC Values and store in temp table
SELECT CAST([ID] as Int) as ID,
       [QTY],
       [EB],
       [GM],
       [COGS],
       [UP],
       [LP],
       [COGSC],
       [GMC],
       [GMP],
       [CGMP]
INTO #WAC
FROM wct.WACtvf('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,
       w.[QTY] as [Inventory-On-Hand],
       w.[EB] as [Inventory Cost],
       w.[GM] as [Gross Margin on Sales],
       w.[COGS] as [Cost-of-Goods Sold],
       w.[UP] as [Average Price],
       w.[LP] as [Last Price],
       w.[COGSC] as [Cumulative COGS],
       w.[GMC] as [Cumulative Gross Margin],
       w.[GMP] as [GM Percentage],
       w.[CGMP] as [Cumulative GM Percentage]
FROM #c c
    INNER JOIN #WAC w
        ON c.trn = w.[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.692383063.3974177215246.8774177215187-2020.8125822784812.87141772151913.01-2020.8125822784846.87741772151870.02267139548071450.0226713954807145
32130310ABC2013-03-10-63.0012.61-794.431752252.49810126582-16.469316455696-810.89931645569612.87141772151913.01-2831.7118987341830.4081012658227-0.02073098505305190.010624327863899
41130310ABC2013-03-10463.0013.386194.946388447.438101265820013.240498591325713.38-2831.7118987341830.4081012658227NULL0.010624327863899
26130320ABC2013-03-20-92.0013.64-1254.885467229.3122308638536.7541295980318-1218.1258704019713.240498591325713.38-4049.8377691361567.16223086385450.02928895958022420.0163133910283834
25130408ABC2013-04-08298.0012.983868.0484411097.35223086390013.148521600549612.98-4049.8377691361567.1622308638545NULL0.0163133910283834
48130430ABC2013-04-30229.0013.323050.28107314147.63223086390013.185118574896413.32-4049.8377691361567.1622308638545NULL0.0163133910283834
90130430ABC2013-04-30-191.0013.49-2576.5988211629.274583058658.2323521947837-2518.3576478052213.185118574896413.32-6568.19541694136125.3945830586380.02260055041538770.0187335320894525
49130508ABC2013-05-08238.0012.793044.02112014673.29458305860013.101155877730912.79-6568.19541694136125.394583058638NULL0.0187335320894525
20130518ABC2013-05-18298.0013.233942.54141818615.83458305860013.128233133327713.23-6568.19541694136125.394583058638NULL0.0187335320894525
89130611ABC2013-06-11130.0013.021692.60154820308.43458305860013.119143787505613.02-6568.19541694136125.394583058638NULL0.0187335320894525
2130617ABC2013-06-17313.0012.934047.09186124355.52458305860013.087331855485612.93-6568.19541694136125.394583058638NULL0.0187335320894525
12130621ABC2013-06-21-326.0012.73-4149.98153520089.0543981703-116.490184888295-4266.4701848882913.087331855485612.93-10834.66560182978.9043981703436-0.0280700593468630.000821168505422439
93130622ABC2013-06-22-227.0013.47-3057.69130817118.230066975186.8656688047754-2970.8243311952213.087331855485612.93-13805.489933024995.7700669751190.02840891941458270.0068893083774506
7130722ABC2013-07-22599.0013.658176.35190725294.58006697510013.264069253788713.65-13805.489933024995.770066975119NULL0.0068893083774506
71130731ABC2013-07-3179.0013.551070.45198626365.03006697510013.275443135435613.55-13805.489933024995.770066975119NULL0.0068893083774506
10130908ABC2013-09-08-386.0013.65-5268.90160021240.709016697144.578949721856-5124.3210502781413.275443135435613.55-18929.810983303240.3490166969750.02744006333805080.0125376635717686
39130908ABC2013-09-08490.0012.696218.10209027458.8090166970013.138186132390912.69-18929.810983303240.349016696975NULL0.0125376635717686
61130916ABC2013-09-16-202.0012.94-2613.88188824804.895417954-40.0335987429617-2653.9135987429613.138186132390912.69-21583.724582046200.315417954013-0.01531577530068770.00919551276778839
76131009ABC2013-10-09249.0013.473354.03213728158.9254179540013.176848581167113.47-21583.724582046200.315417954013NULL0.00919551276778839
88131009ABC2013-10-09187.0013.362498.32232430657.2454179540013.191585808069713.36-21583.724582046200.315417954013NULL0.00919551276778839
16131107ABC2013-11-0727.0012.68342.36235130999.6054179540013.185710513804312.68-21583.724582046200.315417954013NULL0.00919551276778839
86131114ABC2013-11-14-386.0012.40-4786.40196525909.9211596255-303.284258328478-5089.6842583284813.185710513804312.68-26673.4088403745-102.968840374465-0.0633637511132539-0.00387531559035023
8131231ABC2013-12-31-145.0013.19-1912.55182023997.99313512390.621975498369238-1911.9280245016313.185710513804312.68-28585.3368648761-102.3468648760960.000325207444704315-0.00359326267628841
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.8081028167.1243902439-99.9156097560981-6050.715609756134.774227642276434.48-11367.4256097561-409.465609756097-0.0167902819379072-0.0373669560535079
70130702GHI2013-07-02-162.0035.48-5747.7664822533.6995121951114.335121951221-5633.4248780487834.774227642276434.48-17000.8504878049-295.1304878048770.0198921183123896-0.0176664332818266
96130716GHI2013-07-16-347.0035.29-12245.6330110467.0425203252178.973008130077-12066.656991869934.774227642276434.48-29067.5074796748-116.15747967480.0146152552486133-0.00401216107970095
63130806GHI2013-08-06445.0033.5014907.5074625374.54252032520034.014132064779133.5-29067.5074796748-116.1574796748NULL-0.00401216107970095
77130825GHI2013-08-25272.0033.869209.92101834584.46252032520033.972949430574933.86-29067.5074796748-116.1574796748NULL-0.00401216107970095
95130908GHI2013-09-08103.0035.423648.26112138232.72252032520034.10590768985335.42-29067.5074796748-116.1574796748NULL-0.00401216107970095
6130924GHI2013-09-24328.0034.9511463.60144949696.32252032520034.296978965027734.95-29067.5074796748-116.1574796748NULL-0.00401216107970095
17130924GHI2013-09-24-291.0035.69-10385.79115839715.9016415021405.369121176926-9980.4208788230834.296978965027734.95-39047.9283584979289.2116415021260.03903113014772350.00735212680693425
85131002GHI2013-10-02-295.0032.51-9590.4586329598.2928468189-527.158794683182-10117.608794683234.296978965027734.95-49165.5371531811-237.947153181057-0.0549670552146335-0.00486325104467759
52131014GHI2013-10-14-91.0033.12-3013.9277226477.2677610014-107.105085817524-3121.0250858175234.296978965027734.95-52286.5622389986-345.052238998581-0.0355368044996297-0.00664309218192888
73131028GHI2013-10-28-46.0034.65-1593.9072624899.606728610116.2389676087246-1577.6610323912834.296978965027734.95-53864.2232713899-328.8132713898560.0101881972574971-0.00614197727055525
50131103GHI2013-11-03-246.0035.61-8760.0648016462.5499032133323.003174603173-8437.0568253968334.296978965027734.95-62301.2800967867-5.810096786683520.0368722559666455-9.32667622009035E-05
87131116GHI2013-11-16-320.0034.16-10931.201605487.51663440444-43.8332688088758-10975.033268808934.296978965027734.95-73276.3133655956-49.6433655955593-0.00400992286380963-0.000677941050652164
94131117GHI2013-11-17-92.0032.65-3003.80682332.19456962189-151.522064782552-3155.3220647825534.296978965027734.95-76431.6354303781-201.165430378112-0.0504434598783382-0.00263891105981783
81131123GHI2013-11-23187.0033.866331.822558664.014569621890033.976527724007433.86-76431.6354303781-201.165430378112NULL-0.00263891105981783
51131206GHI2013-12-0685.0033.642859.4034011523.41456962190033.892395793005533.64-76431.6354303781-201.165430378112NULL-0.00263891105981783
75131216GHI2013-12-16500.0033.5516775.0084028298.41456962190033.688588773359433.55-76431.6354303781-201.165430378112NULL-0.00263891105981783
13131221GHI2013-12-2172.0034.382475.3691230773.77456962190033.743173870199434.38-76431.6354303781-201.165430378112NULL-0.00263891105981783
65131227GHI2013-12-27376.0033.4912592.24128843366.01456962190033.669265970203333.49-76431.6354303781-201.165430378112NULL-0.00263891105981783
53140102GHI2014-01-02396.0035.5214065.92168457431.93456962190034.104474209989235.52-76431.6354303781-201.165430378112NULL-0.00263891105981783
18140125GHI2014-01-25-78.0035.46-2765.88160654771.7855812427105.731011620842-2660.1489883791634.104474209989235.52-79091.7844187573-95.43441875726970.038226897631438-0.00120808643383232
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.7070116314.716442953313.91644295302-4491.7835570469823.273489932885924.04-4491.78355704698313.916442953020.06532168944233310.0653216894423331
59130320XYZ2013-03-20-20.0023.59-471.8068115849.24664429536.33020134228246-465.46979865771823.273489932885924.04-4957.2533557047320.2466442953030.01341712874582970.0606815053141265
35130321XYZ2013-03-21275.0024.836828.2595622677.49664429530023.721230799472124.83-4957.2533557047320.246644295303NULL0.0606815053141265
98130413XYZ2013-04-13243.0024.255892.75119928570.24664429530023.828395866801824.25-4957.2533557047320.246644295303NULL0.0606815053141265
24130418XYZ2013-04-18275.0024.836828.25147435398.49664429530024.015262309562624.83-4957.2533557047320.246644295303NULL0.0606815053141265
60130419XYZ2013-04-19277.0022.836323.91175141722.40664429530023.827759362818622.83-4957.2533557047320.246644295303NULL0.0606815053141265
83130428XYZ2013-04-28142.0023.133284.46189345006.86664429530023.775418195612923.13-4957.2533557047320.246644295303NULL0.0606815053141265
34130507XYZ2013-05-0755.0023.431288.65194846295.51664429530023.765665628488423.43-4957.2533557047320.246644295303NULL0.0606815053141265
99130515XYZ2013-05-1597.0025.132437.61204548733.12664429530023.830379777161525.13-4957.2533557047320.246644295303NULL0.0606815053141265
19130516XYZ2013-05-16315.0023.577424.55236056157.67664429530023.795625696735323.57-4957.2533557047320.246644295303NULL0.0606815053141265
30130519XYZ2013-05-19467.0023.1510811.05282766968.72664429530023.688972990553723.15-4957.2533557047320.246644295303NULL0.0606815053141265
29130602XYZ2013-06-02114.0024.292769.06294169737.78664429530023.712270195272124.29-4957.2533557047320.246644295303NULL0.0606815053141265
91130615XYZ2013-06-15545.0023.8813014.60348682752.38664429530023.738493013280423.88-4957.2533557047320.246644295303NULL0.0606815053141265
23130619XYZ2013-06-19296.0023.466944.16378289696.54664429530023.716696627259523.46-4957.2533557047320.246644295303NULL0.0606815053141265
47130619XYZ2013-06-19413.0025.1810399.344195100095.8866442950023.860759629152625.18-4957.2533557047320.246644295303NULL0.0606815053141265
74130619XYZ2013-06-19202.0022.954635.904397104731.7866442950023.81891895480922.95-4957.2533557047320.246644295303NULL0.0606815053141265
55130630XYZ2013-06-30-272.0023.45-6378.40412598253.0406885873-100.345955708059-6478.7459557080623.81891895480922.95-11435.9993114128219.900688587244-0.01573215159100380.018866041111132
14130705XYZ2013-07-05-277.0025.01-6927.77384891655.2001381052329.929449517902-6597.840550482123.81891895480922.95-18033.8398618949549.8301381051460.0476241921307870.0295867359948356
5130706XYZ2013-07-06-170.0023.46-3988.20367887605.9839157876-61.0162223175403-4049.2162223175423.81891895480922.95-22083.0560842124488.813915787606-0.01529918818452940.0216558892013646
58130722XYZ2013-07-22-88.0024.64-2168.32359085509.919047764472.2551319768031-2096.064868023223.81891895480922.95-24179.1209522356561.0690477644090.03332309436651560.0226784453864101
57130813XYZ2013-08-13-163.0023.65-3854.95342781627.4352581305-27.5337896338724-3882.4837896338723.81891895480922.95-28061.6047418695533.535258130536-0.007142450520466520.0186582495532645
54130831XYZ2013-08-31-61.0023.23-1417.03336680174.4812018872-35.924056243354-1452.9540562433523.81891895480922.95-29514.5587981128497.611201887182-0.02535165539427820.0165803139822006
11130906XYZ2013-09-06-13.0023.97-311.61335379864.83525547471.96405358747813-309.64594641252223.81891895480922.95-29824.2047445253499.575255474660.006302922202362360.0164747025428446
27130906XYZ2013-09-06-147.0023.81-3500.07320676363.4541691177-1.31108635692317-3501.3810863569223.81891895480922.95-33325.5858308823498.264169117737-0.0003745886102058440.0147311488525918
36130917XYZ2013-09-1792.0024.602263.20329878626.65416911770023.840707752916224.6-33325.5858308823498.264169117737NULL0.0147311488525918
92130924XYZ2013-09-24-248.0024.58-6095.84305072714.1586463945183.344477276773-5912.4955227232323.840707752916224.6-39238.0813536055681.608646394510.03007698320112940.0170744974821826
42131011XYZ2013-10-11-250.0023.12-5780.00280066753.9817081655-180.176938229051-5960.1769382290523.840707752916224.6-45198.2582918345501.431708165458-0.03117248066246560.0109723218727623
22131012XYZ2013-10-12596.0023.1613803.36339680557.34170816550023.721243141391523.16-45198.2582918345501.431708165458NULL0.0109723218727623
40131013XYZ2013-10-13359.0023.918583.69375589141.03170816550023.739289402973523.91-45198.2582918345501.431708165458NULL0.0109723218727623
1131019XYZ2013-10-19424.0025.1310655.12417999796.15170816550023.880390454215225.13-45198.2582918345501.431708165458NULL0.0109723218727623
9131025XYZ2013-10-25-153.0024.31-3719.43402696142.451968670565.7302605050713-3653.6997394949323.880390454215225.13-48851.9580313295567.161968670530.01767213269373840.0114765695680241
62131027XYZ2013-10-27-248.0024.71-6128.08377890220.1151360252205.743167354622-5922.3368326453823.880390454215225.13-54774.2948639749772.9051360251510.03357383835632390.0139143851719826
45131030XYZ2013-10-30-350.0023.36-8176.00342881861.9784770498-182.136658975331-8358.1366589753323.880390454215225.13-63132.4315229502590.768477049821-0.02227698862222730.00927085389700801
21131103XYZ2013-11-03-326.0023.24-7576.24310274076.9711889757-208.767288074165-7785.0072880741623.880390454215225.13-70917.4388110243382.001188975656-0.02755552728981190.00535770251457313
97131125XYZ2013-11-25-278.0023.18-6444.04282467438.2226427038-194.708546271832-6638.7485462718323.880390454215225.13-77556.1873572962187.292642703824-0.03021529138115720.00240911061228316
28131209XYZ2013-12-09315.0024.467704.90313975143.12264270380023.938554521409324.46-77556.1873572962187.292642703824NULL0.00240911061228316
79131223XYZ2013-12-23-376.0025.12-9445.12276366142.2261426539444.2234999501-9000.896500049923.938554521409324.46-86557.0838573461631.5161426539230.04703206523052110.00724310451886971
33140102XYZ2014-01-02-196.0022.98-4504.08256761450.2694564577-187.876686196223-4691.9566861962223.938554521409324.46-91249.0405435423443.6394564577-0.04171255532677550.00483833013123512
84140104XYZ2014-01-04261.0024.466384.06282867834.32945645770023.986679440048724.46-91249.0405435423443.6394564577NULL0.00483833013123512
64140109XYZ2014-01-09-253.0024.46-6188.38257561765.6995581254119.750101667679-6068.6298983323223.986679440048724.46-97317.6704418746563.3895581253790.01935079967094440.00575585877518469
78140112XYZ2014-01-12332.0024.288060.96290769826.65955812540024.020178726565324.28-97317.6704418746563.389558125379NULL0.00575585877518469
80140126XYZ2014-01-26404.0024.239788.92331179615.57955812540024.045780597440524.23-97317.6704418746563.389558125379NULL0.00575585877518469
82140131XYZ2014-01-31548.0023.6512960.20385992575.77955812540023.98957749627523.65-97317.6704418746563.389558125379NULL0.00575585877518469
66140203XYZ2014-02-03459.0023.7010878.304318103454.0795581250023.958795636434823.7-97317.6704418746563.389558125379NULL0.00575585877518469
72140204XYZ2014-02-04-208.0024.36-5066.88411098470.650065746983.4505076215673-4983.4294923784323.958795636434823.7-102301.099934253646.8400657469460.01646980145998470.0062831763874726
31140205XYZ2014-02-0542.0024.391024.38415299495.03006574690023.963157530285924.39-102301.099934253646.840065746946NULL0.0062831763874726

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 WAC Values and store in temp table
SELECT CAST([ID] as Int) as ID,
       [QTY],
       [EB],
       [GM],
       [COGS],
       [UP],
       [LP],
       [COGSC],
       [GMC],
       [GMP],
       [CGMP]
INTO #WAC
FROM wct.WACtvf('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,
       w.[QTY] as [GBP Position],
       w.[EB] as [USD Position],
       w.[GM] as [P/L],
       w.[COGS] as [Cost-of-Goods Sold],
       w.[UP] as [Average Price],
       w.[LP] as [Last Price],
       w.[COGSC] as [Cumulative COGS],
       w.[GMC] as [Cumulative P/L],
       w.[GMP] as [GM Percentage],
       w.[CGMP] as [Cumulative GM Percentage]
FROM #fx c
    INNER JOIN #WAC w
        ON c.trn = w.[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.0020000003244571.42857143-21428.5714285718-8111428.571428571.622285714285711.623-11371428.5714286-29428.5714285718-0.00264877273529936-0.00259465450789736
107GBP-10000000.001.602USD16020000.00-8000000-12816000-40571.4285714291-3244571.428571431.6021.602-14616000-70000.0000000009-0.0126627429998218-0.00481231953801739
108GBP2000000.001.608USD-3216000.00-6000000-9612000-1200032040001.6021.602-11412000-82000.00000000090.00373134328358209-0.00723742277140344
109GBP-2000000.001.602USD3204000.00-8000000-12816000001.6021.602-11412000-82000.0000000009NULL-0.00723742277140344
110GBP10000000.001.626USD-16260000.0020000003252000-192000128160001.6261.6261404000-274000.0000000010.0147601476014760.163289630512515

Example #3

Using the #c temp table created in Example #1, we will insert the necessary input for the WACtvf 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 WAC Values and store in temp table
SELECT CAST([ID] as Int) as ID,
       [QTY],
       [EB],
       [GM],
       [COGS],
       [UP],
       [LP],
       [COGSC],
       [GMC],
       [GMP],
       [CGMP]
INTO #WAC
FROM wct.WACtvf('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,
       w.[QTY] as [Inventory-On-Hand],
       w.[EB] as [Inventory Cost],
       w.[GM] as [Gross Margin on Sales],
       w.[COGS] as [Cost-of-Goods Sold],
       w.[UP] as [Average Price],
       w.[LP] as [Last Price],
       w.[COGSC] as [Cumulative COGS],
       w.[GMC] as [Cumulative Gross Margin],
       w.[GMP] as [GM Percentage],
       w.[CGMP] as [Cumulative GM Percentage]
FROM #inv i
    INNER JOIN #WAC w
        ON i.ID = w.[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.692383063.3974177215246.8774177215187-2020.8125822784812.87141772151913.01-2020.8125822784846.87741772151870.02267139548071450.0226713954807145
32130310ABC2013-03-10-63.0012.61-794.431752252.49810126582-16.469316455696-810.89931645569612.87141772151913.01-2831.7118987341830.4081012658227-0.02073098505305190.010624327863899
41130310ABC2013-03-10463.0013.386194.946388447.438101265820013.240498591325713.38-2831.7118987341830.4081012658227NULL0.010624327863899
26130320ABC2013-03-20-92.0013.64-1254.885467229.3122308638536.7541295980318-1218.1258704019713.240498591325713.38-4049.8377691361567.16223086385450.02928895958022420.0163133910283834
25130408ABC2013-04-08298.0012.983868.0484411097.35223086390013.148521600549612.98-4049.8377691361567.1622308638545NULL0.0163133910283834
48130430ABC2013-04-30229.0013.323050.28107314147.63223086390013.185118574896413.32-4049.8377691361567.1622308638545NULL0.0163133910283834
90130430ABC2013-04-30-191.0013.49-2576.5988211629.274583058658.2323521947837-2518.3576478052213.185118574896413.32-6568.19541694136125.3945830586380.02260055041538770.0187335320894525
49130508ABC2013-05-08238.0012.793044.02112014673.29458305860013.101155877730912.79-6568.19541694136125.394583058638NULL0.0187335320894525
20130518ABC2013-05-18298.0013.233942.54141818615.83458305860013.128233133327713.23-6568.19541694136125.394583058638NULL0.0187335320894525
89130611ABC2013-06-11130.0013.021692.60154820308.43458305860013.119143787505613.02-6568.19541694136125.394583058638NULL0.0187335320894525
2130617ABC2013-06-17313.0012.934047.09186124355.52458305860013.087331855485612.93-6568.19541694136125.394583058638NULL0.0187335320894525
12130621ABC2013-06-21-326.0012.73-4149.98153520089.0543981703-116.490184888295-4266.4701848882913.087331855485612.93-10834.66560182978.9043981703436-0.0280700593468630.000821168505422439
93130622ABC2013-06-22-227.0013.47-3057.69130817118.230066975186.8656688047754-2970.8243311952213.087331855485612.93-13805.489933024995.7700669751190.02840891941458270.0068893083774506
7130722ABC2013-07-22599.0013.658176.35190725294.58006697510013.264069253788713.65-13805.489933024995.770066975119NULL0.0068893083774506
71130731ABC2013-07-3179.0013.551070.45198626365.03006697510013.275443135435613.55-13805.489933024995.770066975119NULL0.0068893083774506
10130908ABC2013-09-08-386.0013.65-5268.90160021240.709016697144.578949721856-5124.3210502781413.275443135435613.55-18929.810983303240.3490166969750.02744006333805080.0125376635717686
39130908ABC2013-09-08490.0012.696218.10209027458.8090166970013.138186132390912.69-18929.810983303240.349016696975NULL0.0125376635717686
61130916ABC2013-09-16-202.0012.94-2613.88188824804.895417954-40.0335987429617-2653.9135987429613.138186132390912.69-21583.724582046200.315417954013-0.01531577530068770.00919551276778839
76131009ABC2013-10-09249.0013.473354.03213728158.9254179540013.176848581167113.47-21583.724582046200.315417954013NULL0.00919551276778839
88131009ABC2013-10-09187.0013.362498.32232430657.2454179540013.191585808069713.36-21583.724582046200.315417954013NULL0.00919551276778839
16131107ABC2013-11-0727.0012.68342.36235130999.6054179540013.185710513804312.68-21583.724582046200.315417954013NULL0.00919551276778839
86131114ABC2013-11-14-386.0012.40-4786.40196525909.9211596255-303.284258328478-5089.6842583284813.185710513804312.68-26673.4088403745-102.968840374465-0.0633637511132539-0.00387531559035023
8131231ABC2013-12-31-145.0013.19-1912.55182023997.99313512390.621975498369238-1911.9280245016313.185710513804312.68-28585.3368648761-102.3468648760960.000325207444704315-0.00359326267628841
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.8081028167.1243902439-99.9156097560981-6050.715609756134.774227642276434.48-11367.4256097561-409.465609756097-0.0167902819379072-0.0373669560535079
70130702GHI2013-07-02-162.0035.48-5747.7664822533.6995121951114.335121951221-5633.4248780487834.774227642276434.48-17000.8504878049-295.1304878048770.0198921183123896-0.0176664332818266
96130716GHI2013-07-16-347.0035.29-12245.6330110467.0425203252178.973008130077-12066.656991869934.774227642276434.48-29067.5074796748-116.15747967480.0146152552486133-0.00401216107970095
63130806GHI2013-08-06445.0033.5014907.5074625374.54252032520034.014132064779133.5-29067.5074796748-116.1574796748NULL-0.00401216107970095
77130825GHI2013-08-25272.0033.869209.92101834584.46252032520033.972949430574933.86-29067.5074796748-116.1574796748NULL-0.00401216107970095
95130908GHI2013-09-08103.0035.423648.26112138232.72252032520034.10590768985335.42-29067.5074796748-116.1574796748NULL-0.00401216107970095
6130924GHI2013-09-24328.0034.9511463.60144949696.32252032520034.296978965027734.95-29067.5074796748-116.1574796748NULL-0.00401216107970095
17130924GHI2013-09-24-291.0035.69-10385.79115839715.9016415021405.369121176926-9980.4208788230834.296978965027734.95-39047.9283584979289.2116415021260.03903113014772350.00735212680693425
85131002GHI2013-10-02-295.0032.51-9590.4586329598.2928468189-527.158794683182-10117.608794683234.296978965027734.95-49165.5371531811-237.947153181057-0.0549670552146335-0.00486325104467759
52131014GHI2013-10-14-91.0033.12-3013.9277226477.2677610014-107.105085817524-3121.0250858175234.296978965027734.95-52286.5622389986-345.052238998581-0.0355368044996297-0.00664309218192888
73131028GHI2013-10-28-46.0034.65-1593.9072624899.606728610116.2389676087246-1577.6610323912834.296978965027734.95-53864.2232713899-328.8132713898560.0101881972574971-0.00614197727055525
50131103GHI2013-11-03-246.0035.61-8760.0648016462.5499032133323.003174603173-8437.0568253968334.296978965027734.95-62301.2800967867-5.810096786683520.0368722559666455-9.32667622009035E-05
87131116GHI2013-11-16-320.0034.16-10931.201605487.51663440444-43.8332688088758-10975.033268808934.296978965027734.95-73276.3133655956-49.6433655955593-0.00400992286380963-0.000677941050652164
94131117GHI2013-11-17-92.0032.65-3003.80682332.19456962189-151.522064782552-3155.3220647825534.296978965027734.95-76431.6354303781-201.165430378112-0.0504434598783382-0.00263891105981783
81131123GHI2013-11-23187.0033.866331.822558664.014569621890033.976527724007433.86-76431.6354303781-201.165430378112NULL-0.00263891105981783
51131206GHI2013-12-0685.0033.642859.4034011523.41456962190033.892395793005533.64-76431.6354303781-201.165430378112NULL-0.00263891105981783
75131216GHI2013-12-16500.0033.5516775.0084028298.41456962190033.688588773359433.55-76431.6354303781-201.165430378112NULL-0.00263891105981783
13131221GHI2013-12-2172.0034.382475.3691230773.77456962190033.743173870199434.38-76431.6354303781-201.165430378112NULL-0.00263891105981783
65131227GHI2013-12-27376.0033.4912592.24128843366.01456962190033.669265970203333.49-76431.6354303781-201.165430378112NULL-0.00263891105981783
53140102GHI2014-01-02396.0035.5214065.92168457431.93456962190034.104474209989235.52-76431.6354303781-201.165430378112NULL-0.00263891105981783
18140125GHI2014-01-25-78.0035.46-2765.88160654771.7855812427105.731011620842-2660.1489883791634.104474209989235.52-79091.7844187573-95.43441875726970.038226897631438-0.00120808643383232
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.7070116314.716442953313.91644295302-4491.7835570469823.273489932885924.04-4491.78355704698313.916442953020.06532168944233310.0653216894423331
59130320XYZ2013-03-20-20.0023.59-471.8068115849.24664429536.33020134228246-465.46979865771823.273489932885924.04-4957.2533557047320.2466442953030.01341712874582970.0606815053141265
35130321XYZ2013-03-21275.0024.836828.2595622677.49664429530023.721230799472124.83-4957.2533557047320.246644295303NULL0.0606815053141265
98130413XYZ2013-04-13243.0024.255892.75119928570.24664429530023.828395866801824.25-4957.2533557047320.246644295303NULL0.0606815053141265
24130418XYZ2013-04-18275.0024.836828.25147435398.49664429530024.015262309562624.83-4957.2533557047320.246644295303NULL0.0606815053141265
60130419XYZ2013-04-19277.0022.836323.91175141722.40664429530023.827759362818622.83-4957.2533557047320.246644295303NULL0.0606815053141265
83130428XYZ2013-04-28142.0023.133284.46189345006.86664429530023.775418195612923.13-4957.2533557047320.246644295303NULL0.0606815053141265
34130507XYZ2013-05-0755.0023.431288.65194846295.51664429530023.765665628488423.43-4957.2533557047320.246644295303NULL0.0606815053141265
99130515XYZ2013-05-1597.0025.132437.61204548733.12664429530023.830379777161525.13-4957.2533557047320.246644295303NULL0.0606815053141265
19130516XYZ2013-05-16315.0023.577424.55236056157.67664429530023.795625696735323.57-4957.2533557047320.246644295303NULL0.0606815053141265
30130519XYZ2013-05-19467.0023.1510811.05282766968.72664429530023.688972990553723.15-4957.2533557047320.246644295303NULL0.0606815053141265
29130602XYZ2013-06-02114.0024.292769.06294169737.78664429530023.712270195272124.29-4957.2533557047320.246644295303NULL0.0606815053141265
91130615XYZ2013-06-15545.0023.8813014.60348682752.38664429530023.738493013280423.88-4957.2533557047320.246644295303NULL0.0606815053141265
23130619XYZ2013-06-19296.0023.466944.16378289696.54664429530023.716696627259523.46-4957.2533557047320.246644295303NULL0.0606815053141265
47130619XYZ2013-06-19413.0025.1810399.344195100095.8866442950023.860759629152625.18-4957.2533557047320.246644295303NULL0.0606815053141265
74130619XYZ2013-06-19202.0022.954635.904397104731.7866442950023.81891895480922.95-4957.2533557047320.246644295303NULL0.0606815053141265
55130630XYZ2013-06-30-272.0023.45-6378.40412598253.0406885873-100.345955708059-6478.7459557080623.81891895480922.95-11435.9993114128219.900688587244-0.01573215159100380.018866041111132
14130705XYZ2013-07-05-277.0025.01-6927.77384891655.2001381052329.929449517902-6597.840550482123.81891895480922.95-18033.8398618949549.8301381051460.0476241921307870.0295867359948356
5130706XYZ2013-07-06-170.0023.46-3988.20367887605.9839157876-61.0162223175403-4049.2162223175423.81891895480922.95-22083.0560842124488.813915787606-0.01529918818452940.0216558892013646
58130722XYZ2013-07-22-88.0024.64-2168.32359085509.919047764472.2551319768031-2096.064868023223.81891895480922.95-24179.1209522356561.0690477644090.03332309436651560.0226784453864101
57130813XYZ2013-08-13-163.0023.65-3854.95342781627.4352581305-27.5337896338724-3882.4837896338723.81891895480922.95-28061.6047418695533.535258130536-0.007142450520466520.0186582495532645
54130831XYZ2013-08-31-61.0023.23-1417.03336680174.4812018872-35.924056243354-1452.9540562433523.81891895480922.95-29514.5587981128497.611201887182-0.02535165539427820.0165803139822006
11130906XYZ2013-09-06-13.0023.97-311.61335379864.83525547471.96405358747813-309.64594641252223.81891895480922.95-29824.2047445253499.575255474660.006302922202362360.0164747025428446
27130906XYZ2013-09-06-147.0023.81-3500.07320676363.4541691177-1.31108635692317-3501.3810863569223.81891895480922.95-33325.5858308823498.264169117737-0.0003745886102058440.0147311488525918
36130917XYZ2013-09-1792.0024.602263.20329878626.65416911770023.840707752916224.6-33325.5858308823498.264169117737NULL0.0147311488525918
92130924XYZ2013-09-24-248.0024.58-6095.84305072714.1586463945183.344477276773-5912.4955227232323.840707752916224.6-39238.0813536055681.608646394510.03007698320112940.0170744974821826
42131011XYZ2013-10-11-250.0023.12-5780.00280066753.9817081655-180.176938229051-5960.1769382290523.840707752916224.6-45198.2582918345501.431708165458-0.03117248066246560.0109723218727623
22131012XYZ2013-10-12596.0023.1613803.36339680557.34170816550023.721243141391523.16-45198.2582918345501.431708165458NULL0.0109723218727623
40131013XYZ2013-10-13359.0023.918583.69375589141.03170816550023.739289402973523.91-45198.2582918345501.431708165458NULL0.0109723218727623
1131019XYZ2013-10-19424.0025.1310655.12417999796.15170816550023.880390454215225.13-45198.2582918345501.431708165458NULL0.0109723218727623
9131025XYZ2013-10-25-153.0024.31-3719.43402696142.451968670565.7302605050713-3653.6997394949323.880390454215225.13-48851.9580313295567.161968670530.01767213269373840.0114765695680241
62131027XYZ2013-10-27-248.0024.71-6128.08377890220.1151360252205.743167354622-5922.3368326453823.880390454215225.13-54774.2948639749772.9051360251510.03357383835632390.0139143851719826
45131030XYZ2013-10-30-350.0023.36-8176.00342881861.9784770498-182.136658975331-8358.1366589753323.880390454215225.13-63132.4315229502590.768477049821-0.02227698862222730.00927085389700801
21131103XYZ2013-11-03-326.0023.24-7576.24310274076.9711889757-208.767288074165-7785.0072880741623.880390454215225.13-70917.4388110243382.001188975656-0.02755552728981190.00535770251457313
97131125XYZ2013-11-25-278.0023.18-6444.04282467438.2226427038-194.708546271832-6638.7485462718323.880390454215225.13-77556.1873572962187.292642703824-0.03021529138115720.00240911061228316
28131209XYZ2013-12-09315.0024.467704.90313975143.12264270380023.938554521409324.46-77556.1873572962187.292642703824NULL0.00240911061228316
79131223XYZ2013-12-23-376.0025.12-9445.12276366142.2261426539444.2234999501-9000.896500049923.938554521409324.46-86557.0838573461631.5161426539230.04703206523052110.00724310451886971
33140102XYZ2014-01-02-196.0022.98-4504.08256761450.2694564577-187.876686196223-4691.9566861962223.938554521409324.46-91249.0405435423443.6394564577-0.04171255532677550.00483833013123512
84140104XYZ2014-01-04261.0024.466384.06282867834.32945645770023.986679440048724.46-91249.0405435423443.6394564577NULL0.00483833013123512
64140109XYZ2014-01-09-253.0024.46-6188.38257561765.6995581254119.750101667679-6068.6298983323223.986679440048724.46-97317.6704418746563.3895581253790.01935079967094440.00575585877518469
78140112XYZ2014-01-12332.0024.288060.96290769826.65955812540024.020178726565324.28-97317.6704418746563.389558125379NULL0.00575585877518469
80140126XYZ2014-01-26404.0024.239788.92331179615.57955812540024.045780597440524.23-97317.6704418746563.389558125379NULL0.00575585877518469
82140131XYZ2014-01-31548.0023.6512960.20385992575.77955812540023.98957749627523.65-97317.6704418746563.389558125379NULL0.00575585877518469
66140203XYZ2014-02-03459.0023.7010878.304318103454.0795581250023.958795636434823.7-97317.6704418746563.389558125379NULL0.00575585877518469
72140204XYZ2014-02-04-208.0024.36-5066.88411098470.650065746983.4505076215673-4983.4294923784323.958795636434823.7-102301.099934253646.8400657469460.01646980145998470.0062831763874726
31140205XYZ2014-02-0542.0024.391024.38415299495.03006574690023.963157530285924.39-102301.099934253646.840065746946NULL0.0062831763874726

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 WAC 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 #WAC table
SELECT CAST([ID] as Int) as ID,
       [QTY],
       [EB],
       [GM],
       [COGS],
       [UP],
       [LP],
       [COGSC],
       [GMC],
       [GMP],
       [CGMP]
INTO   #WAC
  FROM wct.WACtvf('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],
       w.[QTY] as [Inventory-On-Hand],
       w.[EB] as [Inventory Cost],
       w.[GM] as [Gross Margin on Sales],
       w.[COGS] as [Cost-of-Goods Sold],
       w.[UP] as [Average Price],
       w.[LP] as [Last Price],
       w.[COGSC] as [Cumulative COGS],
       w.[GMC] as [Cumulative Gross Margin],
       w.[GMP] as [GM Percentage],
       w.[CGMP] as [Cumulative GM Percentage]
  FROM #p2
 INNER JOIN #WAC w
    ON #p2.ID = w.[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.6983050847457620.698305084745762
ZYXCoffee MugPlastic8 ozWhite2017-01-23-22.95-5.904338.274.12-1.780.890.89-6.2314.420.6983050847457630.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.688780487804911.4087804878049-6.291219512195131.048536585365851.15-22.311219512195148.48878048780490.6445638693675070.684869780901199
ZYXCoffee MugPlastic8 ozWhite2017-02-22-22.95-5.907477.59170731707323.8029268292683-2.09707317073171.048536585365851.15-24.408292682926852.29170731707320.6445638693675080.681769326167838

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.

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

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