Click here to Skip to main content
16,019,614 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a question about inventory project database design

Instead of creating a [ProductBalance] column in products table, I left the balance undefined and created another table [Log]
Log_ID || Product_ID || OperationType_ID   || Input || Output
1            29           1 (Purchases)         5        0
2            24           4 (Sales)             0        2
3            24           5 (SaleReturn)        1        0

* OperationType_ID is related to another table[OperationTypes]

Now when user select some product the query will SUM(Input) - SUM(Output) for this product to get the current balance.


What I have tried:

This worked well till now, but i want to know is this design is bad?
For example when i wanted to get total sales and saleReturn for products i tried this:
SELECT SUM(A.Output) AS Sales, SUM(B.Input) AS SaleReturn
FROM
(SELECT * from Log WHERE Log.OperationType_ID = 2) A
FULL JOIN
(SELECT * FROM Log WHERE Log.OperationType_ID = 5) B ON A.Stock_ID = B.Stock_ID
Posted
Updated 17-Nov-18 9:24am
v2

1 solution

First thing is that the definition you give in the "content" section does not match the condition you give in the query
OperationTypeID = 4 (Sales)

SUM(A.Output) AS Sales = (SELECT * from Log WHERE Log.OperationType_ID = 2)

Second thing was that your query seems quite wordy. You could replace that with something a lot simpler
SQL
SELECT Sales = Sum(Output), SalesReturn = Sum(Input)
FROM @log
WHERE OperationTypeID IN (4, 5)

As for the log table design, I would have additional columns for items such as LogDateTime & TransactionID. I also have an aversion to column names that are either keywords or reserved words; notice that Output is blue in the query, so I would probably rename to something like InventoryRemoved and then change the input column to be similarly named like InventoryAdded
 
Share this answer
 
Comments
XRushdy 17-Nov-18 16:04pm    
Sir i'm so grateful, it worked.
my old query that joining the table to itself was too much complexed and i was worried about it,
the table already contains more columns, i just tried to show a simple example of the idea and know experts opinions about the database design, that idea of (SUM(InventoryAdded) - SUM(InventoryRemoved)) will affect on performance in the future when the table gets larger?
MadMyche 17-Nov-18 18:25pm    
I was just giving my thoughts- seemed like you had that already in mind.
Naturally as there are more rows the performance will be proportionately reduced, but I really would't sweat it. I would probably not be doing the whole log at once, rather I would be be throwing in a GROUP by ProductID and doing a monthly report by product for the time period- would be much more useful for trend analysis etc

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900