I have a database which has an orders table and inventory table.
The order-items table has a 1 record per 1 qty layout, so if a person places an order for 7 'ABC's, and 4 'XYZ's, I get 11 records in the table.
id, item, qtyNum 01 ABC 1 02 ABC 2 03 ABC 3 04 ABC 4 05 ABC 5 06 ABC 6 07 ABC 7 08 XYZ 1 09 XYZ 2 10 XYZ 3 11 XYZ 4
The inventory table has a quantity/item per-location layout, so I can have 20 of something in stock, but it can be (in the worst case) in 20 separate locations. So for our example, I might have the following inventory:
Qty, Item, Loc, Date 3 'ABC' in Location L1 with date 1990 2 'ABC' in Location L2 with date 1992 5 'ABC' in Location L3 with date 2003 4 'ABC' in Location LH with date 2004 1 'XYZ' in Location L4 with date 1990 2 'XYZ' in Location L5 with date 1993 9 'XYZ' in Location L6 with date 2001 2 'XYZ' in Location LJ with date 2004
*The H and J have no special significance! Just driving the point home that they are the newest
The result set should pull as many as possible from the oldest locations first, so for this example I end up with the following 'pick queue':
Pick 3 'ABC' from L1 Pick 2 'ABC' from L2 Pick 2 'ABC' from L3 Pick 1 'XYZ' from L4 Pick 2 'XYZ' from L5 Pick 1 'XYZ' from L6
I do have a solution which involves a lot of views which are joined to multiple times with outer joins and crazy stuff like that and I'm just curious if there is a simple/elegant solution for this problem? I could do it in code no problem, but in SQL I'm no guru.
MSSQL 2008