【问题标题】:Subquery factoring: Inner Join with each other子查询分解:相互内连接
【发布时间】:2012-05-31 13:38:46
【问题描述】:

我有四个 WITH 子句。我想知道是否可以在它们之间使用内连接。

我在网上搜索,我找不到与此相关的任何内容。

Oracle 版本:11g

** 编辑 **

WITH
    GETDATABYDAY AS
    (
        select column1, column2
        from table1 
        where sales > 2000
     )
    SELECT
      column1,
      column2
    FROM
      GETDATABYDAY;

WITH
    GETDATABYDAY1 AS
    (
        select column3, column2
        from table1 
        where date between 'date1' and 'date2'
    )
    SELECT
        column3,
        column2
    FROM
        GETDATABYDAY1;

Assume that there are two more WITH named: GETDATABYDAY2 and GETDATABYDAY3

是否可以在所有 GETDATABYDAY、GETDATABYDAY1、GETDATABYDAY2 和 GETDATABYDAY3 上使用内连接?

【问题讨论】:

  • @A.B.Cade 因为我不想冒任何风险
  • 从尝试 2 WITH 子句开始,使用非常简单的 SQL。
  • 那我就是勇敢的人!这是一个小提琴sqlfiddle.com/#!4/d41d8/765。这是你的意思吗?
  • 跑步的“风险”在哪里? with a as (select * from emp), b as (select * from dept) select * from a join b on b.deptno = a.deptno;?
  • 好的,所以再添加 2 个 WITH 并再试一次。你不能用 select 语句破坏任何东西!

标签: oracle view oracle11g subquery-factoring


【解决方案1】:

这样的事情会起作用:

with first_cte as ( 
    select ...
    from ...
), second_cte as (
    select ...
    from first_cte
      join some_table on ...
), third_cte as (
    select ...
    from ...
) fourth_cte as (
    select ...
    from some_other_table
      join second_cte on ...
) 
select ..
from fourth_cte
   join third_cte on ....

【讨论】:

  • @sameer:当使用多个 WITH 子句时,您只有关键字 once 并且它们之间没有 ;。将我的示例与您的代码进行比较。
  • 我不是在说一个 WITH 子句。我有多个 WITH 子句!可以内联吗??您的代码只有一个 WITH 子句
  • @sameer:不,我的语句中有四个公用表表达式(这是“with 子句”的正确名称)。 关键字 WITH 可能只出现一次,但允许多个 CTE(在我的声明中,它们在各个级别上是内部连接的)
【解决方案2】:

我认为它行不通,因为每个 with clause 都是完全不同的查询的一部分。

您可以创建 db views,并使用它们代替 with 子句。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-10
    • 2021-05-13
    • 1970-01-01
    • 2020-03-01
    • 2021-08-30
    • 2013-04-26
    相关资源
    最近更新 更多