Pergunta

So I have a Q that is like this:

with t1 as (
a.col1 as 'c1',
a.col2 as 'c2',
b.col1 as 'c3',
b.col2 as 'c4'

from table1 a left join table2 b
on a.col1 = b.col1

) 

select 

c.c1,
c.c2,
c.c3,
c.c4
from t1 c 

and I want to make this whole thing a with as T2 so I can pull from what is the outer query on the above code. this is needed to perform calculations on data then renaming the column then performing calculation on the renamed columns and then one more time. I can't seem to figure out how to make the whole statement a "table" that I can then make my select statement from.

I have tried nesting another ;with as () and either it's not possible or I'm not doing it right and my guess is the latter.

Thanks in advance!

Foi útil?

Solução

Is this what you want?

with t1 as (
      select a.col1 as c1, a.col2 as c2, b.col1 as c3, b.col2 as c4
      from table1 a left join
           table2 b
           on a.col1 = b.col1
     ),
     t2 as (
      select c.c1, c.c2, c.c3, c.c4
      from t1 c
     )
select *
from t2;

You can define multiple CTEs with a with statement. They are separated by commas.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top