简体   繁体   English

Oracle SQL:多个With语句

[英]Oracle SQL: Multiple With statement

I am creating a giant SQL query:我正在创建一个巨大的 SQL 查询:

Select * from (With tab1 AS ( Select * from abc)
,tab2 AS (select * from cde)
,tab3 AS (select * from tab2)
.
.
.
,tabz AS (select a, b from xyz
          UNION
          select a, b from cde)

Select tab1.*, tab3.* from 
tab1
LEFT OUTER JOIN tab2 ON tab1.a = tab2.b
...
LEFT OUTER JOIN tabz on tab1.a = tabz.a) A

Now using the above as 1 table I need to create another long SQL to calculate percentages and other things with other tables tables.现在使用上面的表作为 1 个表,我需要创建另一个长 SQL 来计算百分比和其他表的其他内容。

Say above table is A then说上表是 A 那么

Select bbb.a,bbb.b from bbb
JOIN A ON bbb.a = A.a and then name it as B

And then finally join A LEFT OUTER JOIN B .然后最后加入A LEFT OUTER JOIN B

It is a massive Query and I know we can not have Nested WITH statement.这是一个庞大的查询,我知道我们不能有嵌套的 WITH 语句。 Does anyone have any easy way to complete this?有没有人有任何简单的方法来完成这个? OR any suggestion?或者有什么建议吗? I only need to accomplish this using oracle SQL queries.我只需要使用 oracle SQL 查询来完成此操作。

I think you can rephrase your query as:我认为您可以将查询改写为:

WITH
 tab1 AS (select * from abc)
,tab2 AS (select * from cde)
,tab3 AS (select * from tab2)
.
.
.
,tabz AS (select a, b from xyz
          UNION
          select a, b from cde)
,a as (
  Select tab1.*, tab3.* 
  from tab1
  LEFT OUTER JOIN tab2 ON tab1.a = tab2.b
  ...
  LEFT OUTER JOIN tabz on tab1.a = tabz.a
),
b as (
  Select bbb.a,bbb.b from bbb JOIN A ON bbb.a = A.a
)
select * 
from a 
left join b on ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM