Get the BALANCE for each item for every Row

℡╲_俬逩灬. 提交于 2021-02-11 14:19:35

问题


I'm trying to perform a VBA Function or 1 solutions that minus row by row based on stock (available quantity), order by date.
I'm actually using ADO to import item list & quantity, use SQL command to Group by STOCK Quantity from another sheet, and I planned to put all information to the same array, start to minus rows by row OR use SQL command do anything in recordset (just an idea, I don't know whether is it possible/ good way or not).
I hope to have any example that similarly to this or any keyword that I can find and learn. Also, any idea and comment will be greatly appreciated.

This is the stock:

Item  Quantity
A     10
B     9
C     1
D     5

The result will be look like this:

Item  Date    Quantity  Balance
A     25 Dec  4         6
B     24 Dec  4         -4 
A     27 Dec  5         1
B     23 Dec  9         0
A     29 Dec  3         -2
D     21 Dec  4         1
D     25 Dec  3         -2
C     22 Dec  2         -1

回答1:


Both in SQL and in Excel you can solve this:

Access:

I used Access but the SQL is pretty much standard; here is the DB design:

ItemList:

ItemConsumption:

TotalConsumption is the projection of ItemConsumption table added of two new fields:

  1. calculated Balance field [OriginalAmount]-[Consumption] AS Balance
  2. OriginalAmount is a SubQuery that queries the ItemList table (SELECT ItemList.Quantity FROM ItemList WHERE ItemList.Item=ItemConsumption.Item) AS OriginalAmount

SQL of this last table:

SELECT ItemConsumption.Item, ItemConsumption.DateOutofStock, ItemConsumption.Quantity, 
    DSum("[Quantity]","ItemConsumption","[Item]='" & [Item] & "' AND [DateOutofStock]<=#" & [DateOutofStock] & "#") AS Consumption, 
    (SELECT ItemList.Quantity FROM ItemList WHERE ItemList.Item=ItemConsumption.Item) AS OriginalAmount, 
[OriginalAmount]-[Consumption] AS Balance
        FROM ItemConsumption
        ORDER BY ItemConsumption.Item, ItemConsumption.DateOutofStock;

EXCEL:

First we need to order the Results Table by Item and Date, both in crescent order:

I coupled the G column formula to the equality of D column itens; if above item is different from current item, it should VLOOKUP over original Item Quantity; if is equal, Balance of previous date result has the True value of Quantity; this logic translate itself in Balance Formula at D5 cell:

=IF(D5=D4; G4-F5; VLOOKUP(D5;$A$2:$B$5;2;FALSE)-F5)

Sorry about images in Portuguese, but layout helps because is pretty similar. Anyway, the above formula is translated to English; on figure SE = IF, PROCV = VLOOKUP, FALSO = FALSE...

P.S.: Some useful links to start are the excellent Allen Browne's site, AccessProgrammers.uk and here in SO and in a brother SO

P.S. 2: I did not touch VBA to provide these solutions; although certainly it is possible through convoluted procedural code with arrays, or using OOP in a more clean way, I really don't think that is the right tool for this job.



来源:https://stackoverflow.com/questions/65457525/get-the-balance-for-each-item-for-every-row

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!