在oracle查询中重用子查询

时间:2017-03-06 17:04:51

标签: sql oracle

我在较大的Oracle SQL查询中多次将子查询列为相同的查询。有没有办法定义一次并使用较短的形式重复使用它?

1 个答案:

答案 0 :(得分:3)

您可以使用公用表表达式。

从此处复制查询:Creating a CTE in Oracle

with RTG_YEARS (YR) as (
  select to_date('2013-01-01', 'yyyy-mm-dd') from dual
  union all select to_date('2013-12-31', 'yyyy-mm-dd') from dual
  union all select to_date('2014-01-01', 'yyyy-mm-dd') from dual
  union all select to_date('2014-12-31', 'yyyy-mm-dd') from dual
  union all select to_date('2015-01-01', 'yyyy-mm-dd') from dual
  union all select to_date('2015-12-31', 'yyyy-mm-dd') from dual
)
select *
from RTG_YEARS
cross join RTG_YEARS;