除非我遗漏了什么,否则您正在尝试对数据进行透视。有几种方法可以做到这一点。
您可以使用聚合函数和CASE表达式:
select x.id,
  max(case when y.identifier = 1 then y.value end) Value1,
  max(case when y.identifier = 2 then y.value end) Value2
from tablex x
left join tabley y
  on x.id = y.id
group by x.id
请参阅带有演示的 SQL Fiddle
根据您的 Oracle 版本,您可以使用以下PIVOT功能:
select id,
  Value1,
  Value2
from
(
  select x.id, y.identifier, y.value
  from tablex x
  left join tabley y
    on x.id = y.id
) 
pivot
(
  max(value)
  for identifier in ('1' as Value1, '2' as Value2)
) piv
请参阅带有演示的 SQL Fiddle
您可以多次加入:
select x.id,
  y1.value Value1,
  y2.value Value2
from tablex x
left join tabley y1
  on x.id = y1.id
  and y1.identifier = 1
left join tabley y2
  on x.id = y2.id
  and y2.identifier = 2
请参阅SQL Fiddle with Demo。
如果您正在寻找动态解决方案,那么您可以使用 sys_refcursor 创建一个过程:
CREATE OR REPLACE procedure dynamic_pivot(p_cursor in out sys_refcursor)
as
    sql_query varchar2(8000) := 'select x.id ';
    begin
        for x in (select distinct identifier from tabley order by 1)
        loop
            sql_query := sql_query ||
                ' , max(case when y.identifier = '||x.identifier||' then y.value else null end) as Value'||x.identifier;
                dbms_output.put_line(sql_query);
        end loop;
        sql_query := sql_query || ' from tablex x
                                    left join tabley y
                                      on x.id = y.id
                                    group by x.id';
        dbms_output.put_line(sql_query);
        open p_cursor for sql_query;
    end;
/
这些解决方案在每个值的单独列中为您提供结果。如果您想要单列中的数据,那么您可以使用LISTAGG():
select x.id,
  listagg(y.value, ' ') within group (order by y.id) as Value
from tablex x
left join tabley y
  on x.id = y.id
group by x.id
请参阅带有演示的 SQL Fiddle