我有下表:
TABLE1
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
以下是输出表:
TABLE2
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。
因此,例如。表1中的第一行具有与%01-01格式匹配的元素ID DTP01-01,因此该元素ID将添加为PARENT_ELEMENT_ID,而DTP01-01的element_val将变为PARENT_ELEMENT_VAL将被添加到同一file_name的所有行中, line_num。
我找不到第一个元素,也不确定如何将其添加为2列。
如何使用SQL实现此目的?
答案 0 :(得分:1)
像这样吗?
我通过删除不相关的列来缩短了列列表,因为您没有提供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>