• It looks like you need to look for uncompleted orders in the plan table and tie them by lot number to the temp table in order to get to the line items are being run on.

    I am just guessing. Usually I deal with individual work orders and statuses. The plan, customer demand, and stocking levels drive the creation of work orders.

    There may be some issues like an order closed short, or multiple lots assigned to a line in the plan to the same line at the same time.

    Maybe some more information, and an example or 2 of what you have tried would help fill in some gaps.

    And a larger sample of data, with some completed orders would help get a better answer.