Search code examples
sqloracle-databasewith-clause

Create table using two with clauses


I want to create table and use two with, here is my code:

Create Table profitTry
As
With ctsWithSplit as (select c.Tdate, c.Symbol, c.close * coalesce(s.post/s.pre, 1) as new_close
 from ctsTry c left join splits s 
on c.Tdate = s.Tdate and c.symbol = s.symbol),

delta as
( select a.Tdate as TDate,  a.Symbol as Symbol, a.price-b.price as Pdelta, b.price as oldPrice
   from ctsWithSplit a, ctsWithSplit b
   where a.TDate-b.TDate=1 and a.Symbol=b.Symbol)

select  a.TDate,a.Symbol, (a.delta-coalesce(b.dividend,0))/delta.oldPrice as percentage
From delta a left join dividend b
On a.Tdate=b.Tdate and a.Symbol=b.Symbol

There is an error saying "table not existed", is it because my 2nd with clause please?


Solution

  • One definite problem is in your outer select:

    select  a.TDate,a.Symbol, (a.delta-coalesce(b.dividend,0))/delta.oldPrice as percentage
    -----------------------------------------------------------^
    From delta a left join dividend b
    On a.Tdate=b.Tdate and a.Symbol=b.Symbol
    

    There is no delta. You mean a.