查询错误中使用“ with”语句进行数据提取

问题描述

我写了一个查询提取一些数据,但出现错误,在使用多个WITH语句时我是sql的新手,出于隐私原因,我删除了一些列,如下所示,但是错误我如下所示。请给一些建议。谢谢

Database: Terradata 
Query: sql

错误

SELECT Failed [ 3706] Syntax error: expected something between 'Study_Ongoing' and the 'AS' keyword 

查询

 WITH Study_Ongoing AS
(
SELECT
    Clnc_Actv_Id
    
FROM
P_Clnc_Actv_Lif_Cyc
where Actv_Ongo_Fl=1
),Study_Ongoing_Group AS 
(
SELECT
    Clnc_Actv_Id as Actv_Id,Actv_Ongo_Fl
 
FROM
Study_Ongoing
GROUP BY
    Clnc_Actv_Id,Actv_Ongo_Fl,SO_Diff
)
Select 
    Actv_Id as "Protocol Id",FROM
Study_Ongoing_Group
 inner join X_Calendar Cal
 on Cal.CAL_DT >= Vldt_Strt_Dttm and Cal.CAL_DT < Vldt_End_Dttm
 where CAL_DT >= Date '2019-01-01'


WITH Study_Ongoing AS              --> ERROR Points here
(
SELECT
    Clnc_Actv_Id,Ptcol_Sts_Ctms
FROM
P_Clnc_Actv_Lif_Cyc
where 
(Ptcol_Sts_Ctms ='Open' or Ptcol_Sts_Ctms='Execution' or Fst_Site_Open_Actl is not null or Fst_Subj_Scrnd_Actl is not null or Fst_Subj_Enrld_Actl is not null)
and
Last_Subj_Last_Visit_Extd_Actl is null
and
Last_Site_Clse_Actl is null
and
Ptcol_Sts_Ctms<>'Not Selected' and Ptcol_Sts_Ctms<>'Not required' and Ptcol_Sts_Ctms<>'Completed' and Ptcol_Sts_Ctms<>'Closed' and Ptcol_Sts_Ctms<>'Cancelled' and Ptcol_Sts_Ctms<>'Stopped' and Ptcol_Sts_Ctms<>'Terminated'
),Ptcol_Sts_Ctms
    
FROM
Study_Ongoing
GROUP BY
    Clnc_Actv_Id,Ptcol_Sts_Ctms
  
)
Select 
    Actv_Id as "Protocol Id",Ptcol_Sts_Ctms
  
FROM
Study_Ongoing_Group
 inner join X_Calendar Cal
 on Cal.CAL_DT >= Vldt_Strt_Dttm and Cal.CAL_DT < Vldt_End_Dttm
 where CAL_DT >= Date '2019-01-01')

解决方法

Study_Ongoing AS ( 选择 Clnc_Actv_Id

FROM
P_Clnc_Actv_Lif_Cyc
where Actv_Ongo_Fl=1
),Study_Ongoing_Group AS 
(
SELECT
    Clnc_Actv_Id as Actv_Id,Actv_Ongo_Fl
 
FROM
Study_Ongoing
GROUP BY
    Clnc_Actv_Id,Actv_Ongo_Fl,SO_Diff
)
Select 
    Actv_Id as "Protocol Id",FROM
Study_Ongoing_Group
 inner join X_Calendar Cal
 on Cal.CAL_DT >= Vldt_Strt_Dttm and Cal.CAL_DT < Vldt_End_Dttm
 where CAL_DT >= Date '2019-01-01';   --> fixed with semicolon


WITH Study_Ongoing AS              --> ERROR Points here
(
SELECT
    Clnc_Actv_Id,Ptcol_Sts_Ctms
FROM
P_Clnc_Actv_Lif_Cyc
where 
(Ptcol_Sts_Ctms ='Open' or Ptcol_Sts_Ctms='Execution' or Fst_Site_Open_Actl is not null or Fst_Subj_Scrnd_Actl is not null or Fst_Subj_Enrld_Actl is not null)
and
Last_Subj_Last_Visit_Extd_Actl is null
and
Last_Site_Clse_Actl is null
and
Ptcol_Sts_Ctms<>'Not Selected' and Ptcol_Sts_Ctms<>'Not Required' and Ptcol_Sts_Ctms<>'Completed' and Ptcol_Sts_Ctms<>'Closed' and Ptcol_Sts_Ctms<>'Cancelled' and Ptcol_Sts_Ctms<>'Stopped' and Ptcol_Sts_Ctms<>'Terminated'
),Ptcol_Sts_Ctms
    
FROM
Study_Ongoing
GROUP BY
    Clnc_Actv_Id,Ptcol_Sts_Ctms
  
)
Select 
    Actv_Id as "Protocol Id",Ptcol_Sts_Ctms
  
FROM
Study_Ongoing_Group
 inner join X_Calendar Cal
 on Cal.CAL_DT >= Vldt_Strt_Dttm and Cal.CAL_DT < Vldt_End_Dttm
 where CAL_DT >= Date '2019-01-01')