473,412 Members | 5,714 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

Hopefully SQL can handle processing alternative part numbers?

geolemon
I'm building a small-scale MRP system for an electronics manufacturing business. There are assemblies that are built, and parts that go into those assemblies. The individual component parts that go into each assembly have their own component part numbers, and since many manufacturers make those basic components, often times alternative part numbers (and quantities) exist in the inventory system for a given component part.

Fortunately, we assign internal part numbers, so there's a one-to-many relationship between the internal part numbers that we use to build assemblies by and the actual manufacturer's part numbers that we've purchased to maintain inventory for that internal part number.

So, we could have more than one MfgPN for any given internal PN (for example):

Internal PN | Manufacturer PN | Quantity
RES-000293-R | CRCW0603330RJNEA | 150
RES-000293-R | RM06J301CT-ND-TR | 5000

So, in other words, either of those MfgPN's is completely appropriate to use wherever the assembly calls out RES-000293-R.

And our operators will use up existing, opened reels of parts before mounting new reels. So, in this example, we'll use up the stock of 150 CRCW0603330RJNEA before mounting the unopened reel of RM06J301CT-ND-TR.
Parts don't have expiration dates, so we use up small quantities before moving on to larger ones.

The dillema this creates is in creating SQL code to decrement inventory appropriately. Consider building 500 assemblies where this part is used once per assembly:
In reality, we'd use up the 150 first, then use 350 of the RM06J301CT-ND-TR.
So, after running, I'd have this:

Internal PN | Manufacturer PN | Quantity
RES-000293-R | CRCW0603330RJNEA | 0
RES-000293-R | RM06J301CT-ND-TR | 4650

From a SQL standpoint, I have:
Inventory table has MfgPN and Qty.
Parts table has IntPN and MfgPN.
Assemblies table has data on each assembly location, including IntPN.

I created a stored query (used as a view) to provide IntPN, count(*) for any given assembly, so I know how many IntPN's I need to decrement from inventory.

How could I then process that against [functionally] potentially multiple instances of that IntPN in inventory, so that it subtracts from the smallest inventory item first, then the next smallest, and so on until the full decrement quantity has been reached?

I'd like to stay within SQL if possible to do this in a query.
Feb 26 '09 #1
5 2335
Stewart Ross
2,545 Expert Mod 2GB
Hi Geolemon. You do not mention the name of the primary key of your parts table; it is either a field you haven't told us about so far, or a compound key based on internal PN and manufacturers PN. We need to know the PK so that it is possible to identify uniquely a row to update, by joining back your quantity-finding query to the table. If you could advise what field this is it would be helpful.

Also note that it is potentially possible to have equal quantities of parts in stock from more than one manufacturer. This can be dealt with, for example by selecting just the one row arbitrarily, but as you may not yet have considered this possibility you need to decide what to do in those circumstances.

However, I do not think it is possible in SQL to decrement partial totals across rows as you are suggesting (i.e. if amount > what is in stock, subtract all from what is in stock and decrement remainder for next item). It is most likely that a code-based loop solution would be much simpler to achieve, and indeed may be the only feasible way to accomplish what you ask.

-Stewart
Feb 26 '09 #2
Primary key is MfgPN in the parts table, since it has a many-to-one relationship to CustPN. In that table, CustPN is just an attribute associated with MfgPN because of that relationship.

I think what I'm going to do is create two stored queries...
--- One to identify the min Qty and min MfgPN (since I at least need it returned but need the 'group' by to work) where Qty>0
---The second to identify the same thing, where Qty <0.

I can then process my decrementing against the first view, ensuring I have only one CustPN, and it's the one that we have the lowest Qty of all CustPN's.

If that particular MfgPN doesn't have enough parts, it'll turn into a negative number in my inventory.

I can then repeatedly process querying the second view to find negative numbers, and using those numbers (multiplied by -1) to process against the first view again, iterating until I no longer have any negative numbers in my Inventory table. And I'll probably need a temporary table to track the numbers I find as negatives so I can zero just those after each iteration...

Sound reasonable? It keeps me in queryland, other than that I'll need some VBA to control the looping.
Feb 26 '09 #3
Stewart Ross
2,545 Expert Mod 2GB
Hi. I'd go the code solution - I do not think you will find it feasible to do what you suggest in SQL. Good luck if you want to try it, however.

Unless there is something else in your parts table that you haven't told us about there seems to be little reason for having a separate table. There is a 1-1 relationship between the inventory table and the parts table, and the norm when faced with this is to merge the two together. In this case, it is just transferring your IntPN field into your inventory table - which is indeed what you show in post 1 from your view. You would not then have to maintain two tables with manufacturers part numbers in your application.

I will come back to you after investigating a code solution.

-Stewart

PS you are referring to CustPN in post 3 - I presume this is a synonym for IntPN?
Feb 26 '09 #4
Stewart Ross
2,545 Expert Mod 2GB
Here is a VBA function which implements a selective decrement on the stock items. It uses an internal SQL statement based on the parts and inventory tables, which as I mentioned in my previous post I think can be simplified if the inventory table is altered to contain the internal part number.

Expand|Select|Wrap|Line Numbers
  1. Public Function fDecrementInventory(ByVal strIntPN As String, ByVal lngQty As Long)
  2.     'Decrements the stock quantities in the Inventory table where the
  3.     'internal part number matches the argument provided
  4.     'If the quantity concerned is greater than any single amount in stock
  5.     'the stock quantity is decremented across multiple stock items until the
  6.     'total quantity has been accounted for.
  7.     '
  8.     'Assumptions:
  9.     ' 1. the internal part number does exist already in the dataset
  10.     ' 2. there is sufficient stock in hand to issue the quantity specified
  11.     '
  12.     Dim strSQL As String
  13.     Dim lngQtyInStock As Long
  14.     Dim RS As DAO.Recordset
  15.     strSQL = "SELECT Parts.IntPN, Inventory.MfgPN, Inventory.Qty"
  16.     strSQL = strSQL & " FROM Inventory INNER JOIN Parts ON Inventory.MfgPN = Parts.MfgPN"
  17.     strSQL = strSQL & " WHERE IntPN = '" & strIntPN & "'"
  18.     strSQL = strSQL & " ORDER BY Parts.IntPN, Inventory.Qty;"
  19.     Set RS = CurrentDb.OpenRecordset(strSQL)
  20.     Do While Not RS.EOF And lngQty > 0
  21.         RS.Edit
  22.         lngQtyInStock = RS!Qty
  23.         If lngQtyInStock >= lngQty Then
  24.             RS!Qty = lngQtyInStock - lngQty
  25.             lngQty = 0
  26.         Else
  27.             RS!Qty = 0
  28.             lngQty = lngQty - lngQtyInStock
  29.         End If
  30.         RS.Update
  31.         RS.MoveNext
  32.     Loop
  33.     RS.Close
  34. End Function
This function requires that a reference to the DAO object library is set; if it fails to compile, from the VB editor select Tools, References and tick Microsoft DAO 3.6 object library (or later).

Sample function call in VBA code, from command button or similar (replace the arguments with suitable variables or form field references):

Call fDecrementInventory (internal_partnumber, qty_issued)

Tested in VB immediate window like this:

? fDecrementInventory("RES-000293-R", 500)

Test Data
Expand|Select|Wrap|Line Numbers
  1. Before
  2. IntPN         MfgPN              Qty
  3. RES-000293-R  RM06J301CT-ND-TR   150
  4. RES-000293-R  CRCW0603330RJNEA  5000
  5. RES-000293-R  ABCDEFG-1234      6000
  6. RES-000293-S  12345-FGH          100
  7. RES-000293-S  12345-ABCDEFG      100
  8.  
  9. After
  10. IntPN         MfgPN              Qty
  11. RES-000293-R  RM06J301CT-ND-TR     0
  12. RES-000293-R  CRCW0603330RJNEA  4650
  13. RES-000293-R  ABCDEFG-1234      6000
  14. RES-000293-S  12345-FGH          100
  15. RES-000293-S  12345-ABCDEFG      100
-Stewart
Feb 26 '09 #5
I appreciate it -

There isn't actually a 1:1 relationship... I just described the scenario simplistically trying to keep the thread simple and the issue spotlighted.

We're actually a contract manufacturer serving many customers - you could substitute "customer part number" for "internal part number", and the inventory table reflects what actual, real, physical parts we actually have in inventory (by Manufacturer part number), where the Parts table tracks what parts we are allowed to have for any given Customer Part Number - including the possibility of approved alternate or substitute Manufacturer Part Numbers. In many cases, the same Manufacturer Part Number in inventory is common (if not actually physically shared) across multiple products, if not multiple customers.

If it weren't, I'd love to get rid of the MfgPN altogether and just run on CustPN!

I'm going to check out the code you put up here, I think there's a lot I can use there -thanks!

I'll probably post what I end up with.
Feb 26 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

28
by: Grant Edwards | last post by:
I finally figured out why one of my apps sometimes fails under Win32 when it always works fine under Linux: Under Win32, the pickle module only works with a subset of floating point values. In...
0
by: GreenFROG Design | last post by:
Data Processing and SQL INSERTING Hello guy's i'm faced with a horrible dilemma i have whole dictionary that i have to enter into a database. I have all the text and i would like to convert it...
2
by: Simon Niederberger | last post by:
Hi I've written a Windows Service which has - several (0-100) listeners threads which spawn worker threads based on events, timers etc - several (0-300) worker threads which handle data...
7
by: Arnold | last post by:
I need to read a binary file and store it into a buffer in memory (system has large amount of RAM, 2GB+) then pass it to a function. The function accepts input as 32 bit unsigned longs (DWORD). I...
3
by: GGG | last post by:
I have a situation where at tool is passing me a large array of strings that I need to process in a particular type of data. Each item in the array gets to me as a pair of std::strings, basically,...
4
by: gl | last post by:
I have just started a project that's going to do very heavy credit card processing through asp.net and i had some questions. I've never really done any cc processing through code and I wasn't sure...
2
by: | last post by:
Hi, we are planning to rewrite an extisting C++ image processing application/library in C#. Now several question arouse where I hope you can help me: So far we allocated a block of memory as...
5
by: Phuff | last post by:
Hey all, I need some direction help. I have a switch case statement that is seemingly my only option right now, but its too large and not easy to maintain the code. Here goes... I have part...
1
by: lilsn0opy04 | last post by:
I am pretty new to Python. I wanted to create a program so that if a user entered a number, then that amount of random numbers will appear. I wanted my random.randrange(100)+1 "How many numbers...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.