如何在 main select 中使用嵌套 select 中的列值?

How can I use columns value from nested select in main select?

我正在尝试在 Oracle 中执行此 SQL:

select z.*
  from (select (CASE
                 WHEN trunc(to_date('01.02.2015', 'DD.MM.YY'), 'MM') =
                      to_date('01.02.2015', 'DD.MM.YY') THEN
                  trunc(ADD_MONTHS(sysdate, -1), 'MM')
                 ELSE
                  trunc(sysdate - 1)
               END) as sd,
               trunc(sysdate) ed
          from dual) t,
       (SELECT *
          FROM table(SOMEOWNERUSER.SOMEPACKAGE.getPipelinedTable(to_char(t.sd,
                                                                       'dd.mm.yyyy'),
                                                               to_char(t.ed,
                                                                       'dd.mm.yyyy')))) z

但是我收到错误 ORA-00904: "T"."SD": invalid identifier。我做错了什么?

您只需要在与 t 子查询相同级别的连接中直接引用 table():

create type dt_type as object (dt date);
/

create type dt_tab as table of dt_type;
/

create or replace function getpipelinedtable (p_sd date, p_ed date)
return dt_tab
as
  l_tab dt_tab := dt_tab();
begin
  for i in 1 .. (p_ed - p_sd) + 1
  loop
    l_tab.extend;
    l_tab(l_tab.last) := dt_type(p_sd -1 + i);
  end loop;

  return l_tab;
end getpipelinedtable;
/

SELECT z.*
FROM   (SELECT (CASE WHEN TRUNC (TO_DATE ('01.02.2015', 'DD.MM.YY'), 'MM') = TO_DATE ('01.02.2015', 'DD.MM.YY') 
                          THEN TRUNC (ADD_MONTHS (SYSDATE, -1), 'MM') 
                     ELSE TRUNC (SYSDATE - 1) 
                END) AS sd,
                TRUNC (SYSDATE) ed
        FROM   DUAL) t,
        TABLE (  getpipelinedtable (t.sd, t.ed) ) z;

DT        
----------
01/01/2015
02/01/2015
03/01/2015
04/01/2015
05/01/2015
<snip>
06/02/2015
07/02/2015
08/02/2015
09/02/2015

drop function getpipelinedtable;
drop type dt_tab;
drop type dt_type;