SQL: Copy Rows and Descending Integrate

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

Answers


The following will produce the output you specified.

WITH cte 
     AS (SELECT ph.IC_PO_HEADER, 
                ph.PO_NUMBER, 
                ph.revision_number, 
                pl.ic_line_key, 
                pl.ic_po_line, 
                Row_number() 
                  OVER ( 

                    ORDER BY ph.IC_PO_HEADER, IC_LINE_KEY) Rn 
         FROM   PO_HEADER ph 
                INNER JOIN PO_LINE pl 
                        ON ph.po_number = pl.po_number 
         ) 
SELECT t1.IC_PO_HEADER, 
       t1.PO_NUMBER, 
       t1.revision_number, 
       t1.ic_line_key,
       t2.ic_line_key + t1.rn - 1, 
       t2.IC_PO_LINE + t1.rn - 1
FROM   cte t1
       INNER JOIN cte t2 
       ON t2.rn = 1

ORDER  BY IC_PO_HEADER

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


Something like this maybe, I didn't test it:

SELECT po_header.*
      , 'ic_line_key' = (SELECT max(ic_line_key) FROM PO_LINE) + rank() OVER (ORDER BY newid() ASC)
      , 'ic_po_line' = (SELECT max(ic_po_line) FROM PO_LINE) + rank() OVER (ORDER BY newid() ASC)
FROM
    PO_HEADER
CROSS JOIN
    PO_LINE
WHERE
    po_header.po_number = po_line.po_number
    AND po_header.ic_po_header <> po_line.ic_po_header

Need Your Help

How can I tie a WPF Command to a MouseEnter event?

wpf command mouseenter

I need to tie a WPF-Command to the MouseEnter event of a Stackpanel. How can I do that?

How to generate CSR for SSL that works with Nginx & Apache?

apache ssl https nginx openssl

I want to generate the CSR file for requesting SSL (wildcard) certificate. This certificate and private key will be used on multiple machines with both Apache and Nginx.