聚合函数出错,但解析得很好

时间:2017-01-03 15:20:36

标签: sql-server

我在查询时遇到问题

CREATE VIEW qryMonthlyFeesToCollectSummary1
AS
  SELECT ContractID,
         Round(Sum([CreditAmount] * ( ( 1
                                        + COALESCE((SELECT Max(MonthlyRate) 
                                                    FROM tblCases 
                                                    WHERE LEFT(CaseID, 4) = [ContractID] AND CaseStatus = 'Open Case'), 
                                                    0.0199) ) / 30 ) * Exp(30 - 1)), 2) AS InterestCredit
  FROM   tblCredits
  GROUP  BY tblCredits.ContractID,
            (SELECT Count(*)
             FROM   tblInterestPayments
             WHERE  ContractMainID = [ContractID]
                    AND ( PaymentDate IS NULL
                           OR ( PaymentDate IS NOT NULL
                                AND Bounced <> 0
                                AND RetrySuccessful IS NULL ) ))
  HAVING (SELECT Count(*)
          FROM   tblInterestPayments
          WHERE  ContractMainID = [ContractID]
                 AND ( PaymentDate IS NULL
                        OR ( PaymentDate IS NOT NULL
                             AND Bounced <> 0
                             AND RetrySuccessful IS NULL ) )) = 0; 

它出错如下所示,即使它解析得很好。请帮忙!非常感谢!

  

Msg 144,Level 15,State 1,Procedure qryMonthlyFeesToCollectSummary1,   第11行不能在使用的表达式中使用聚合或子查询   对于GROUP BY子句列表的组。

     

Msg 130,Level 15,State 1,Procedure qryMonthlyFeesToCollectSummary1,   第5行无法对表达式执行聚合函数   包含聚合或子查询。

4 个答案:

答案 0 :(得分:0)

我认为这就是你要找的东西:

CREATE VIEW qryMonthlyFeesToCollectSummary1
AS
  SELECT ContractID,
         Round(Sum([CreditAmount] * ( ( 1
                                        + COALESCE((SELECT Max(MonthlyRate) 
                                                    FROM tblCases 
                                                    WHERE LEFT(CaseID, 4) = [ContractID] AND CaseStatus = 'Open Case'), 
                                                    0.0199) ) / 30 ) * Exp(30 - 1)), 2) AS InterestCredit
  FROM   tblCredits
  WHERE (SELECT Count(*)
          FROM   tblInterestPayments
          WHERE  ContractMainID = tblCredits.[ContractID]
                 AND ( PaymentDate IS NULL
                        OR ( PaymentDate IS NOT NULL
                             AND Bounced <> 0
                             AND RetrySuccessful IS NULL ) )) = 0
  GROUP  BY tblCredits.ContractID;

答案 1 :(得分:0)

我想我做得对。尝试正确别名查询中使用的表名和每个列。

CREATE VIEW qryMonthlyFeesToCollectSummary1
AS
  SELECT   tblCredits.ContractID
         , Round(Sum([CreditAmount] * ( ( 1
              + COALESCE(Max(MonthlyRate) , 0.0199) ) / 30 ) 
                * Exp(30 - 1)), 2) AS InterestCredit
  FROM   tblCredits
  INNER JOIN tblCases            ON LEFT(CaseID, 4) = [ContractID] 
                                AND CaseStatus = 'Open Case'
  INNER JOIN tblInterestPayments ON  ContractMainID = [ContractID]
  WHERE PaymentDate IS NULL
       OR ( PaymentDate IS NOT NULL
            AND Bounced <> 0
            AND RetrySuccessful IS NULL ) 
  GROUP  BY tblCredits.ContractID
  HAVING Count(*) = 0; 

答案 2 :(得分:0)

您可以尝试以下方法:

SELECT 
    Credits.ContractID,
    ROUND(SUM(Credits.CreditAmount * ( ( 1 + COALESCE(Cases.MaxMonthlyRate,0.0199) ) / 30 ) * Exp(30 - 1)), 2) AS InterestCredit
FROM   
    tblCredits Credits
    OUTER APPLY(
        SELECT 
            Max(Cases.MonthlyRate) MaxMonthlyRate
        FROM 
            tblCases Cases 
        WHERE 
            LEFT(Cases.CaseID, 4) = Credits.ContractID
            AND Cases.CaseStatus = 'Open Case'
    )Cases
WHERE 
    NOT EXISTS(
        SELECT NULL
        FROM tblInterestPayments Payments
        WHERE  
            Payments.ContractMainID = Credits.ContractID
            AND ( 
                Payments.PaymentDate IS NULL
                OR ( 
                    Payments.PaymentDate IS NOT NULL
                    AND Payments.Bounced <> 0
                    AND Payments.RetrySuccessful IS NULL 
                ) 
            )
    )
GROUP BY
    Credits.ContractID

当您尝试在已使用聚合函数的表达式内的子查询中使用聚合函数(MAX,MIN,AVG等)时,会遇到您遇到的错误。删除它们,我将该子查询从select子句中移出并进入外部apply。我使用了外部应用,因为您将问题标记为SQL服务器并且子查询正在执行聚合。在其他数据库中,您将使用左连接。

此外,我将tblInterestPayments子查询移动到NOT EXISTS子句中。由于您正在检查符合条件的表中是否存在行,这更有意义。

答案 3 :(得分:0)

尝试类似:

CREATE VIEW qryMonthlyFeesToCollectSummary1
AS
  SELECT 
    ContractID,
    ROUND(SUM([CreditAmount] * ( ( 1  + COALESCE(max_monthlyRate, 0.0199) ) / 30 ) * 
EXP(30 - 1)), 2) AS InterestCredit
      FROM   
        tblCredits c LEFT OUTER JOIN
        (SELECT 
            ContractMainID 
        FROM 
            tblInterestPayments  
        WHERE 
            PaymentDate IS NULL  OR 
            (  Bounced <> 0   AND RetrySuccessful IS NULL )) i ON
        c.ContractID = i.ContractMainID LEFT OUTER JOIN
        (SELECT 
            LEFT(CaseID, 4) AS left_caseID4,
            MAX(MonthlyRate) AS max_monthlyRate
         FROM 
            tblCases 
         WHERE 
            CaseStatus = 'Open Case'
        GROUP BY
            LEFT(caseid,4)) s ON
        c.contractID = s.left_caseID4
      WHERE
        i.ContractMainID IS NULL
      GROUP  BY 
        ContractID