【问题标题】:CTE Error : Incorrect syntax near commaCTE 错误:逗号附近的语法不正确
【发布时间】:2016-02-07 15:51:20
【问题描述】:

谁能给点灯:

逗号附近出现错误:

  ;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]

【问题讨论】:

  • 这和C#有什么关系?你只发布了 SQL,对吧?
  • 这适用于哪个 RDBMS?请添加标签以指定您使用的是mysqlpostgresqlsql-serveroracle 还是db2 - 或者完全是其他东西。

标签: c# sql syntax common-table-expression


【解决方案1】:

如果你更好地缩进你的代码也许会有所帮助......

您的代码:

 ;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

【讨论】:

    【解决方案2】:

    只有在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 by 被用于cte,尽管没有在任何列上使用聚合。应该避免这种情况。

    【讨论】:

    • 你能重写查询吗
    • @vkp 。 . . group by 很好。相当于select distinct
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多