This might be a doozy of a question or I might be asking too much at once:
In a Nutshell
How can I SELECT rows from a table between two occurrences of “description like ‘B/O INFO %’ or between ‘B/O INFO %’ and the last row of a given order number (WHERE OrderNo = 12345678)
Background
I am attempting to put together an open order line item report for a client of our business. The database I’m working with old, but “tried and true” and its design and the processes that write to it should be considered immutable for the purposes of answering this question (primarily because the processes that write data to this table are difficult to deal with {COBOL on a Unisys mainframe, mainframe to MSSQL replication, no fully qualified Unisys mainframe COBOL programmer available, migrating off the mainframe, etc.}).
What I’ve been trying to accomplish for 2 weeks now is to create a SELECT statement that returns a hierarchy of orders and order line items. This hierarchy represents an order placed by our client(s) and all the spawned “child” orders attempting to fill (wholly or partially) line items from the “root” order.
When an order is placed many automated and manual processes are applied to it. If the system sees an order line item can be wholly fulfilled by shipping from a different warehouse, it’ll create a new child order for those line items. If a line item quantity is unavailable, it’ll partially ship and create a backorder for the remainder. If the client really needs the line item, a human will manually create a new order sourcing the part from a different location.
Problem
The frustrating situation is a non-indexed char(21) column is used to declare what line items in child orders apply to a given parent order and that that linking information is not on the same line as the actual product ordered – it’s a completely separate “comment” line. What I’m having real trouble with is trying to SELECT specific order line items from these child orders. The most troublesome being SELECTing order line items between two instances of a matching description pattern or the end of the order lines.
I’ve tried all manner of JOINs and WHEREs etc. I’m no DBA, but I’m well above the level “knows enough to be dangerous” and I’ve come to the end of my understanding trying to get this together.
What I Need/Want to Do
Create a stored procedure that returns a result set of all order numbers and their respective item numbers in the order hierarchy I can then use to determine quantity fulfillment of the root order’s line items.
Details
The way these comment lines in child orders relate to their parents is as follows:
- A description starting with “@???/” means a child order (see #2 below) has been created with the order number matching the digits between slashes.
- A description starting with “MEO INFO” means all the items in the order (see #1 above) are related to the order matching the numeric portion of the description.
- A description starting with “*S/” means a child order has been created (see #4 below) with the order number matching the digits between slashes.
- A description starting with “B/O #” means the line above is relevant to the order matching the numeric portion of the description. (see #3 above)
- A description starting with “B/O INFO” means at least one line below until the next occurrence of “B/O INFO” or the end of the order line items is relevant to the order matching the numeric portion of the description. The parent will not have a reference to this child order.
- Orders containing line comments like “B/O #” or “B/O INFO” can be “composite” child orders fulfilling order line items from one or more parent orders. Associated line items as detailed in the above list.
- Finding “@???/” and “*S/” lines in an order are easy references to child orders.
With this sample data, the root order number is 12345678 and I need to build a hierarchy of all related child line items.
Sample Data
OrderNo |ItemNo |Description |LineCD ------------------------------------------------- Lines pointing to child orders 12345678 1 @LVL/11223344/001 12345678 2 *S/11335577/12345ELEM All lines relevant to parent order 11223344 1 MEO INFO 12345678 11223344 2 RAIL 11223344 3 BODY OVEN WELD ASM Comment lines indicate above line is for a parent order 55667788 1 PUMP&MOTOR 55667788 2 B/O # 12345678 55667788 3 *S/22446688/12345GLAS 55667788 4 B/O # 77553311 //The above line is NOT related to 12345678 Comment lines indicating line(s) below until end of lines or next comment are for a parent order 98765432 31 B/O INFO 12345678 C 98765432 32 CNTRL-ELEC 98765432 33 STRIKE 98765432 34 CONTROL-ELECTRICAL 98765432 35 B/O INFO 88664422 C
I hope this question is answerable. If not, it’ll at least give your brain a good work-out.
Wow, what a mess.
Although I hate recommending the use of cursors, I don’t see any other way to get the data you want. I don’t think standard set based operations (selects with joins etc) are going to pull the data in any reasonable matter.
Which leaves us with 3 potential solutions.
One, do the processing in your code. This has many obvious drawbacks depending on the size of your data set.
Two, do the processing using cursors. Cursors are not very performant, but with your data structured that way performance isn’t even on the radar.
Three, use some type of CLR based solution. The drawbacks here are your ability to install it and whether your sql server supports that.
Good luck.