我在较大的Oracle SQL查询中多次将子查询列为相同的查询。有没有办法定义一次并使用较短的形式重复使用它?
答案 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;