The AG
The AG

Reputation: 690

Oracle SQL: Multiple With statement

I am creating a giant SQL query:

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.

Say above table is A then

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.

It is a massive Query and I know we can not have Nested WITH statement. Does anyone have any easy way to complete this? OR any suggestion? I only need to accomplish this using oracle SQL queries.

Upvotes: 1

Views: 857

Answers (1)

The Impaler
The Impaler

Reputation: 48770

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 ...

Upvotes: 2

Related Questions