如何将SQL Server CTE查询转换为Oracle 10G

时间:2016-09-06 04:34:21

标签: sql-server oracle tsql oracle10g sqlplus

我有问题,因为我得到错误不支持的列别名,我不知道原因在哪里。有谁知道答案,可以提供帮助吗?

我试过了,但是我收到了一个错误:

  

ORA-32033:不支持的列别名。

我只是想在Oracle中开始使用CTE(我来自T-SQL)。

;with cte as
(
    select 
        ITP_NO, FROM_DATE,
        DATEADD(YEAR,cast(BILL_AMOUNT/ 58 as int)-1, FROM_DATE) as lastfromdate,
        BILL_AMOUNT
    from 
        test123 

    union all

    select 
        c.ITP_NO, DATEADD(YEAR,1, c.FROM_DATE) as fromdate,
        c.lastfromdate,
        t.BILL_AMOUNT
    from 
        cte c, test123 t
    where 
        c.ITP_NO = t.ITP_NO 
        and DATEADD(YEAR,1, c.FROM_DATE) <= c.lastfromdate 
)
,ctee as
(
      select CAST(t.ITP_NO as varchar(15)) ITP_NO,
      c.FROM_DATE, CAST( DATEADD(d,-1, DATEADD(YEAR, 1,c.FROM_DATE)) as date) as todate
      ,t.BILL_AMOUNT
      from test123 t, cte c
      where t.ITP_NO = c.ITP_NO
)
select  ITP_NO,FROM_DATE,todate,
left(datename(month, FROM_DATE),3)+' '+CAST(year(FROM_DATE)as varchar(10))+' - '+left(datename(month, todate),3)+' '+CAST(year(todate)as varchar(10)) ExpiryDate 
,BILL_AMOUNT
from ctee
order by 1 desc

我试过下面的代码,但是收到错误:

  

ORA-32033:不支持的列别名。

;with cte
as
(
      select ITP_NO,
             FROM_DATE,
             add_months(FROM_DATE, (cast(BILL_AMOUNT/ 58 as int)-1)* 12) as lastfromdate 
              ,BILL_AMOUNT
      from test123 
      union all
      select c.ITP_NO, 
              add_months(c.FROM_DATE,12) as lastfromdate 
              ,c.lastfromdate 
              ,t.BILL_AMOUNT
      from cte c,test123 t
      where c.ITP_NO = t.ITP_NO and  add_months(c.FROM_DATE,12) <= c.lastfromdate 
)
,ctee
as
(
      select CAST(t.ITP_NO as varchar(15)) ITP_NO,
      c.FROM_DATE, 
      (add_months(c.FROM_DATE,12)-1)
      ,t.BILL_AMOUNT
      from test123 t, cte c
      where t.ITP_NO = c.ITP_NO
)
select  *
--ITP_NO,FROM_DATE,todate,
--left(datename(month, FROM_DATE),3)+' '+CAST(year(FROM_DATE)as varchar(10))+' - '+left(datename(month, todate),3)+' '+CAST(year(todate)as varchar(10)) ExpiryDate 
--,BILL_AMOUNT
from ctee
order by 1 desc

0 个答案:

没有答案