0

我正在写这个查询,我遇到了一个心理障碍。我一直在自己周围循环,这令人沮丧。

这是表格:

PO_HEADER(IC_PO_HEADER 唯一)

IC_PO_HEADER  |  po_number | revision_number
123              p123              0
456              p123              2         
789              p123              48

PO_LINE(IC_LINE_KEY & IC_PO_LINE 唯一)

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

每列大约有 150 个表,但这些是唯一真正重要的表,因为这是所有内容都在检查的,我想复制整行以显示如下:

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

我打算使用带有选定巢穴的插入物,但我一直在绕圈跑。这是示例数据,在某处大约有 15,000 条记录PO_HEADER,目前在 40,000 条记录中PO_LINE

伪代码:

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
4

2 回答 2

1

以下将产生您指定的输出。

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

使用 ROW_NUMBER,您可以生成一个 AUTONUMBER。通过自加入 CTE,我们可以找到 IC_LINE_KEY 和 IC_PO_LINE (t2.rn = 1) 的初始值

演示

于 2012-06-13T16:58:17.550 回答
0

可能是这样的,我没有测试它:

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
于 2012-06-13T17:17:57.933 回答