Oracle SQL - 在 LEFT JOIN 子句中重用第一个 table 中的列
Oracle SQL - Reusing column from the first table on LEFT JOIN clause
请考虑以下 db<>fiddle:
https://dbfiddle.uk/?rdbms=oracle_11.2&fiddle=8d3223f66c3f88294f84645756420836
我有一个名为 sample_data
的(简化的)table,看起来像这样:
key values separator fields
--------------------------------------------------------
10791701 16;20; ; FIELD1;FIELD2
10791702 17|18|19| | FIELD3|FIELD4|FIELD5
我想在这里做的是执行一个查询,其输出是这样的:
key value field
--------------------------------------------------------
10791701 16 FIELD1
10791701 20 FIELD2
10791702 17 FIELD3
10791702 18 FIELD4
10791702 19 FIELD5
也就是说,我想以某种方式拆分 values
列中的值,并在 fields
列中为它们分配相应的字段名称。
这是我尝试过的方法:
select *
from sample_data t1
left join (select t1.key, column_value from table(apex_string.split(t1.fields, t1.separator))) t2
on t1.key = t2.key;
不幸的是,这似乎不起作用,因为 Oracle 显示:ORA-00904: "T1"."FIELDS": invalid identifier
。在我看来,当使用 left join
子句时,它不允许我使用来自上面 t1
别名的列。
那么,如何获得所需的输出?
11g 中没有 lateral
子句,但是可以使用普通的旧逗号表示法连接来自 table
函数的关系。
在您的情况下,恕我直言,问题是您将 "fields"
声明为带引号的标识符,因此您也必须在查询中引用它。
所以最终的解决方案看起来像
select *
from sample_data t1,
table(apex_string.split(t1."fields", t1."separator")) t2
(未在数据库中验证 fiddle - 那里不支持顶点函数。)
我设法使用一种“分而治之”的策略解决了我的问题。这是怎么回事:
create table SAMPLE_DATA
(
key VARCHAR2(8),
the_values VARCHAR2(10),
separator VARCHAR2(1),
the_fields VARCHAR2(20)
);
然后我像这样插入数据:
insert into sample_data (KEY, THE_VALUES, SEPARATOR, THE_FIELDS)
values ('10791701', '16;20;', ';', 'FIELD1;FIELD2');
insert into sample_data (KEY, THE_VALUES, SEPARATOR, THE_FIELDS)
values ('10791702', '18|17|19|', '|', 'FIELD3|FIELD4|FIELD5');
insert into sample_data (KEY, THE_VALUES, SEPARATOR, THE_FIELDS)
values ('10791703', '77,99,88', ',', 'FIELD6,FIELD7,FIELD8');
然后,最后,使用以下查询:
with t1 as
(select * from sample_data),
t2 as
(select key, row_number() over(partition by key order by null) value_index, column_value
from t1, table(apex_string.split(the_values, separator))
where column_value is not null),
t3 as
(select key, row_number() over(partition by key order by null) value_index, column_value
from t1, table(apex_string.split(the_fields, separator))
where column_value is not null)
select t1.key, t2.column_value the_value, t3.column_value the_field
from t1
left join t2
on t1.key = t2.key
left join t3
on t2.key = t3.key
and t2.value_index = t3.value_index;
输出
KEY THE_VALUE THE_FIELD
---------------------------------
10791701 16 FIELD1
10791701 20 FIELD2
10791702 18 FIELD3
10791702 17 FIELD4
10791702 19 FIELD5
10791703 77 FIELD6
10791703 99 FIELD7
10791703 88 FIELD8
说明
基本上,tables t2
和 t3
拆分 the_values
和 the_fields
列,同时还检索基于 1 的索引(row_number() over(partition by key order by null)
) 对于发生的每个拆分。剩下的就是加入关系 table 和其他两个。
请考虑以下 db<>fiddle:
https://dbfiddle.uk/?rdbms=oracle_11.2&fiddle=8d3223f66c3f88294f84645756420836
我有一个名为 sample_data
的(简化的)table,看起来像这样:
key values separator fields
--------------------------------------------------------
10791701 16;20; ; FIELD1;FIELD2
10791702 17|18|19| | FIELD3|FIELD4|FIELD5
我想在这里做的是执行一个查询,其输出是这样的:
key value field
--------------------------------------------------------
10791701 16 FIELD1
10791701 20 FIELD2
10791702 17 FIELD3
10791702 18 FIELD4
10791702 19 FIELD5
也就是说,我想以某种方式拆分 values
列中的值,并在 fields
列中为它们分配相应的字段名称。
这是我尝试过的方法:
select *
from sample_data t1
left join (select t1.key, column_value from table(apex_string.split(t1.fields, t1.separator))) t2
on t1.key = t2.key;
不幸的是,这似乎不起作用,因为 Oracle 显示:ORA-00904: "T1"."FIELDS": invalid identifier
。在我看来,当使用 left join
子句时,它不允许我使用来自上面 t1
别名的列。
那么,如何获得所需的输出?
11g 中没有 lateral
子句,但是可以使用普通的旧逗号表示法连接来自 table
函数的关系。
在您的情况下,恕我直言,问题是您将 "fields"
声明为带引号的标识符,因此您也必须在查询中引用它。
所以最终的解决方案看起来像
select *
from sample_data t1,
table(apex_string.split(t1."fields", t1."separator")) t2
(未在数据库中验证 fiddle - 那里不支持顶点函数。)
我设法使用一种“分而治之”的策略解决了我的问题。这是怎么回事:
create table SAMPLE_DATA
(
key VARCHAR2(8),
the_values VARCHAR2(10),
separator VARCHAR2(1),
the_fields VARCHAR2(20)
);
然后我像这样插入数据:
insert into sample_data (KEY, THE_VALUES, SEPARATOR, THE_FIELDS)
values ('10791701', '16;20;', ';', 'FIELD1;FIELD2');
insert into sample_data (KEY, THE_VALUES, SEPARATOR, THE_FIELDS)
values ('10791702', '18|17|19|', '|', 'FIELD3|FIELD4|FIELD5');
insert into sample_data (KEY, THE_VALUES, SEPARATOR, THE_FIELDS)
values ('10791703', '77,99,88', ',', 'FIELD6,FIELD7,FIELD8');
然后,最后,使用以下查询:
with t1 as
(select * from sample_data),
t2 as
(select key, row_number() over(partition by key order by null) value_index, column_value
from t1, table(apex_string.split(the_values, separator))
where column_value is not null),
t3 as
(select key, row_number() over(partition by key order by null) value_index, column_value
from t1, table(apex_string.split(the_fields, separator))
where column_value is not null)
select t1.key, t2.column_value the_value, t3.column_value the_field
from t1
left join t2
on t1.key = t2.key
left join t3
on t2.key = t3.key
and t2.value_index = t3.value_index;
输出
KEY THE_VALUE THE_FIELD
---------------------------------
10791701 16 FIELD1
10791701 20 FIELD2
10791702 18 FIELD3
10791702 17 FIELD4
10791702 19 FIELD5
10791703 77 FIELD6
10791703 99 FIELD7
10791703 88 FIELD8
说明
基本上,tables t2
和 t3
拆分 the_values
和 the_fields
列,同时还检索基于 1 的索引(row_number() over(partition by key order by null)
) 对于发生的每个拆分。剩下的就是加入关系 table 和其他两个。