如何在主查询中实现with子句

时间:2020-05-07 16:19:13

标签: sql oracle

我有查询,为了获得更好的性能,我编写了一个with子句。现在的主要问题是我不知道如何在主选择查询中实现with子句?我可以加入条款吗?任何人都可以在这里指导我。感谢您的帮助。

Begin
OPEN p_resultset FOR
with inputs  as (  
  select s.sid, t.number, s.state, t.city "city"
  from student s
  join dpartment t on t.dept_id = s.sid
  where s.act_status in ('A, 'B')
  and t.city in (select city from empCity g)
     )
     
     //how to implement above with clause in my below main query 
     
     select s.sid, s.state, get_name("name", e.fullname) "name", e.endDate
     
     from All_Emp e
     join student s on e.id = s.sid and s.act_status = 'A'
     join student ss on s.sid=ss.sid and ss.act_status = 'D'
     join department t on s.sid = t.dept_id
     join performance l on l.pid = s.sid
     join empcity g on s.city = g.city
     
     where 
     
     where e.endDate > s.endDate

2 个答案:

答案 0 :(得分:0)

您就像引用表或视图一样引用CTE。因此,在FROM子句中的某处您将拥有:

from . . .
     input i
     . . .

然后您可以适当地加入它。

答案 1 :(得分:0)

您可以像使用某些表名一样使用给定的别名“ inputs”。

WITH inputs AS
(
       SELECT s.sid,
              t.number,
              s.state,
              t.city "city"
       FROM   student s
       join   dpartment t
       ON     t.dept_id = s.sid
       WHERE  s.act_status IN ('A',
                               'B')
       AND    t.city IN
              (
                     SELECT city
                     FROM   empcity g))
SELECT s.sid,
       s.state,
       Get_name("name", e.fullname) "name",
       e.enddate
FROM   all_emp e  
join   performance l
ON     l.pid = s.sid
join   empcity g
ON     s.city = g.city
join   inputs i
ON     i.sid=e.id --here is your "with select"
WHERE
where  e.enddate > s.enddate