首页 > 解决方案 > SQL - 获取父值

问题描述

我有下表:

表格1

FILE_NAME       C_TYPE          H_LEVEL     HOOP    ELEMENT_ID  ELEMENT_VAL  LINE_NUM   
text1.txt       74774592        Header      2000    DTP01-01    472          42
text1.txt       74774592        Header      2000    DTP02-01    34567        42
text1.txt       74774592        Header      2000    DTP03-01    RET          42
text1.txt       74774592        Header      2000    DTP01-01    473          58
text1.txt       74774592        Header      2000    DTP02-01    7567         58
text1.txt       74774592        Header      2000    DTP03-01    QET          58
text2.txt       74774592        Header      2000    CR01-01     33           42
text2.txt       74774592        Header      2000    CR02-01     TYES         42
text2.txt       74774592        Header      2000    CR03-01     VBFG         42
text2.txt       74774592        Header      2000    CR01-01     65           58
text2.txt       74774592        Header      2000    CR02-01     RET          58
text2.txt       74774592        Header      2000    CR03-01     QQQ          58

下面是输出表:

表2

FILE_NAME       C_TYPE          H_LEVEL     HOOP    ELEMENT_ID  ELEMENT_VAL  LINE_NUM   PARENT_ELEMENT_ID   PARENT_EMENT_VAL
text1.txt       74774592        Header      2000    DTP01-01    472          42         DTP01-01            472
text1.txt       74774592        Header      2000    DTP02-01    34567        42         DTP01-01            472
text1.txt       74774592        Header      2000    DTP03-01    RET          42         DTP01-01            472
text1.txt       74774592        Header      2000    DTP01-01    473          58         DTP01-01            473
text1.txt       74774592        Header      2000    DTP02-01    7567         58         DTP01-01            473
text1.txt       74774592        Header      2000    DTP03-01    QET          58         DTP01-01            473
text2.txt       74774592        Header      2000    CR01-01     33           42         CR01-01             33
text2.txt       74774592        Header      2000    CR02-01     TYES         42         CR01-01             33
text2.txt       74774592        Header      2000    CR03-01     VBFG         42         CR01-01             33
text2.txt       74774592        Header      2000    CR01-01     65           58         CR01-01             65
text2.txt       74774592        Header      2000    CR02-01     RET          58         CR01-01             65
text2.txt       74774592        Header      2000    CR03-01     QQQ          58         CR01-01             65

输出表应包含 2 个附加列:

PARENT_ELEMENT_ID 和 PARENT_ELEMENT_VAL 是 FILE_NAME 和 LINE_NUM 格式 (%01-01) 的第一个 ELEMENT_ID。

所以,例如。Table1 中的第一行的元素 ID DTP01-01 与 %01-01 格式匹配,因此该元素 ID 将添加为 PARENT_ELEMENT_ID 并且 DTP01-01 的 element_val 将变为 PARENT_ELEMENT_VAL 将添加到相同文件名的所有行中,并且行号。

我找不到第一个元素,也不知道如何将它添加为 2 列。

如何使用 SQL 实现这一点?

标签: sqloracle

解决方案


像这样的东西?

我通过删除不相关的列来缩短列列表,因为您没有提供CREATE TABLE & INSERT INTO语句(而且我不想输入那么多)。另外,我想说你在期望的输出中犯了一个错误——你从哪里得到ELEMENT_VAL = 473的?源数据中没有。

SQL> with test (file_name, element_id, element_val, line_num) as
  2    (select 'text1.txt', 'DTP01-01', '472'  , 42 from dual union all
  3     select 'text1.txt', 'DTP02-01', '34567', 42 from dual union all
  4     select 'text1.txt', 'DTP03-01', 'RET'  , 42 from dual union all
  5     select 'text1.txt', 'DTP01-01', '472'  , 58 from dual union all
  6     select 'text1.txt', 'DTP02-01', '7567' , 58 from dual union all
  7     select 'text1.txt', 'DTP03-01', 'QET'  , 58 from dual union all
  8     --
  9     select 'text2.txt', 'CR01-01', '33'  , 42 from dual union all
 10     select 'text2.txt', 'CR02-01', 'TYES', 42 from dual union all
 11     select 'text2.txt', 'CR03-01', 'VBFG', 42 from dual union all
 12     select 'text2.txt', 'CR01-01', '65'  , 58 from dual union all
 13     select 'text2.txt', 'CR02-01', 'RET' , 58 from dual union all
 14     select 'text2.txt', 'CR03-01', 'QQQ' , 58 from dual
 15    ),
 16  src as
 17    (select file_name, line_num, element_id, element_val
 18     from test
 19     where instr(element_id, '01-01') > 0
 20    )
 21  select t.file_name, t.element_id, t.element_val, t.line_num,
 22    s.element_id parent_element_id,
 23    s.element_val parent_element_val
 24  from test t join src s on s.file_name = t.file_name
 25                        and s.line_num = t.line_num
 26  order by t.file_name, t.line_num, t.element_id;

FILE_NAME ELEMENT_ ELEME   LINE_NUM PARENT_E PAREN
--------- -------- ----- ---------- -------- -----
text1.txt DTP01-01 472           42 DTP01-01 472
text1.txt DTP02-01 34567         42 DTP01-01 472
text1.txt DTP03-01 RET           42 DTP01-01 472
text1.txt DTP01-01 472           58 DTP01-01 472
text1.txt DTP02-01 7567          58 DTP01-01 472
text1.txt DTP03-01 QET           58 DTP01-01 472
text2.txt CR01-01  33            42 CR01-01  33
text2.txt CR02-01  TYES          42 CR01-01  33
text2.txt CR03-01  VBFG          42 CR01-01  33
text2.txt CR01-01  65            58 CR01-01  65
text2.txt CR02-01  RET           58 CR01-01  65
text2.txt CR03-01  QQQ           58 CR01-01  65

12 rows selected.

SQL>

推荐阅读