CTE错误:逗号附近的语法不正确

本文关键字:语法 不正确 错误 CTE | 更新日期: 2023-09-27 18:20:48

有人能给一些光吗:

我在逗号附近出错:

  ;with cteClaims as (
    select a.LoanId, a.ID, a.ClaimType, a.ClaimStatus, a.ClaimTypeDescription                   
      FROM [spd].[claims].[Population] a                    
      group by a.LoanId, a.ID, a.ClaimType, a.ClaimStatus, a.ClaimTypeDescription                   
      having a.ClaimStatus not in ('Closed', 'Denied', 'Paid')                  
      ), ctePopulation as (  
      select a.LoanId, a.ID, a.ClaimType, a.ClaimStatus, a.ClaimTypeDescription                 
      FROM [spd].[claims].[Population] a                    
      join cteClaims b on a.LoanId = b.LoanId                   
      group by a.LoanId, a.ID, a.ClaimType, a.ClaimStatus, a.ClaimTypeDescription)  
       select loanid as cteLoanId 
        from [spd].[claims].[Population] where id in (select id from ctePopulation)
   /// Error is here near this comma  below 
     , cteLoanBase as (
        select 
               a.LoanId as [Loan#]
             , a.AcquisitionDt as [AcquisitionDt]
             , a.CorpRecoverableBalanceAmt as [CorpRecoverableBalanceAmt]
             , a.EscrowBalance as [EscrowBalance]
             , a.FirstPaymentDueDt as [FirstPaymentDueDt]
             , a.InvestorLoanId as [InvestorLoanId]
             , a.InvestorPoolId as [InvestorPoolId]
             , a.LoanStatusId as [LoanStatusId]

CTE错误:逗号附近的语法不正确

只有在cte定义完成后,才应该使用select。该代码在cte定义之间有一个select

 ;with cteClaims as (
select a.LoanId, a.ID, a.ClaimType, a.ClaimStatus, a.ClaimTypeDescription                   
  FROM [spd].[claims].[Population] a                    
  group by a.LoanId, a.ID, a.ClaimType, a.ClaimStatus, a.ClaimTypeDescription                   
  having a.ClaimStatus not in ('Closed', 'Denied', 'Paid')                  
  ), ctePopulation as (  
  select a.LoanId, a.ID, a.ClaimType, a.ClaimStatus, a.ClaimTypeDescription                 
  FROM [spd].[claims].[Population] a                    
  join cteClaims b on a.LoanId = b.LoanId                   
  group by a.LoanId, a.ID, a.ClaimType, a.ClaimStatus, a.ClaimTypeDescription)  
 , cteLoanBase as ( ...)
 select loanid as cteLoanId 
 from [spd].[claims].[Population] where id in (select id from ctePopulation)

这里还要提到的一件事是:group bycte中使用,尽管在任何列上都没有使用聚合。应该避免这种情况。

如果您能更好地缩进代码,也许会有所帮助。。。

您的代码:

 ;with 
    cteClaims as (
        SELECT a.LoanId, a.ID, a.ClaimType, a.ClaimStatus, a.ClaimTypeDescription                   
        FROM [spd].[claims].[Population] a                    
            GROUP BY a.LoanId, a.ID, a.ClaimType, a.ClaimStatus, a.ClaimTypeDescription                   
              HAVING a.ClaimStatus not in ('Closed', 'Denied', 'Paid')),
    ctePopulation as (  
        SELECT a.LoanId, a.ID, a.ClaimType, a.ClaimStatus, a.ClaimTypeDescription                 
        FROM [spd].[claims].[Population] a                    
        JOIN cteClaims b on a.LoanId = b.LoanId                   
        GROUP BY a.LoanId, a.ID, a.ClaimType, a.ClaimStatus, a.ClaimTypeDescription)  
    SELECT loanid as cteLoanId 
        FROM [spd].[claims].[Population] 
    WHERE id in (select id from ctePopulation)
   /// Error is here near this comma  below 

     , cteLoanBase as (
        select 
               a.LoanId as [Loan#]
             , a.AcquisitionDt as [AcquisitionDt]
             , a.CorpRecoverableBalanceAmt as [CorpRecoverableBalanceAmt]
             , a.EscrowBalance as [EscrowBalance]
             , a.FirstPaymentDueDt as [FirstPaymentDueDt]
             , a.InvestorLoanId as [InvestorLoanId]
             , a.InvestorPoolId as [InvestorPoolId]
             , a.LoanStatusId as [LoanStatusId]

我的猜测:

 ;with 
    cteClaims as (
        SELECT a.LoanId, a.ID, a.ClaimType, a.ClaimStatus, a.ClaimTypeDescription                   
        FROM [spd].[claims].[Population] a                    
            GROUP BY a.LoanId, a.ID, a.ClaimType, a.ClaimStatus, a.ClaimTypeDescription                   
              HAVING a.ClaimStatus not in ('Closed', 'Denied', 'Paid')),
    ctePopulation as (  
        SELECT a.LoanId, a.ID, a.ClaimType, a.ClaimStatus, a.ClaimTypeDescription                 
        FROM [spd].[claims].[Population] a                    
        JOIN cteClaims b on a.LoanId = b.LoanId                   
        GROUP BY a.LoanId, a.ID, a.ClaimType, a.ClaimStatus, a.ClaimTypeDescription),
    cteLoanBase as (
        SELECT 
                   a.LoanId as [Loan#]
                 , a.AcquisitionDt as [AcquisitionDt]
                 , a.CorpRecoverableBalanceAmt as [CorpRecoverableBalanceAmt]
                 , a.EscrowBalance as [EscrowBalance]
                 , a.FirstPaymentDueDt as [FirstPaymentDueDt]
                 , a.InvestorLoanId as [InvestorLoanId]
                 , a.InvestorPoolId as [InvestorPoolId]
                 , a.LoanStatusId as [LoanStatusId]
    FROM ???  )

    SELECT loanid as cteLoanId 
        FROM [spd].[claims].[Population] 
    WHERE id in (select id from ctePopulation)

很难猜测你想要什么。。。

  • 你末尾少了一个括号
  • 您应该在进行主选择之前定义"With"子句(cteLoadBase是在您的主选择之后定义的)
  • 您缺少一个FROM