I’m writing this query and I’m running into a mental road block. I keep looping around myself and it’s frustrating.
Here’s the tables:
PO_HEADER(IC_PO_HEADER Unique)
IC_PO_HEADER | po_number | revision_number
123 p123 0
456 p123 2
789 p123 48
PO_LINE(IC_LINE_KEY & IC_PO_LINE Unique)
IC_PO_HEADER | po_number | line_rev_no | ic_line_key | ic_po_line
123 p123 0 8246321 214578
123 p123 0 8246322 214579
123 p123 0 8246322 214580
There are roughly 150 tables on each column, but these are the only ones that actually matter, as that’s what everything is checking against, and I want to copy the entire line to display like this:
IC_PO_HEADER | po_number | line_rev_no | ic_line_key | ic_po_line
123 p123 0 8246321 214578
123 p123 0 8246322 214579
123 p123 0 8246323 214580
456 p123 2 8246324 214581
456 p123 2 8246325 214582
456 p123 2 8246326 214583
789 p123 48 8246327 214584
789 p123 48 8246328 214585
789 p123 48 8246329 214586
I was going to use an insert with selected nests but I keep running myself in a circle. This is sample data, there are somewhere in the order of 15,000 records in PO_HEADER and currently 40,000 in PO_LINE.
Pseudo-codey:
If PO_HEADER.ic_po_header does not exist in PO_LINE.ic_po_header AND
PO_HEADER.po_number does exist in PO_LINE.po_number
COPY PO_LINE.* where the above is true AND
SET PO_LINE.line_rev_no to PO_HEADER.revision_number (do this until no more PO_HEADER.revision_number's exist that do not meet the criteria)
AUTONUMBER/INCREMENT PO_LINE.ic_line_key AND PO_LINE.ic_po_line
The following will produce the output you specified.
Using the ROW_NUMBER you can generate an AUTONUMBER. By self joining to CTE we can find the initial values for IC_LINE_KEY and IC_PO_LINE (t2.rn = 1)
DEMO