如何在Select语句中使用Aggregate

时间:2017-05-03 03:11:45

标签: sql sql-server

我的代码有问题,似乎它在我的select查询中没有接受聚合函数。我想用DATEDIFF(MONTH,MIN(LoanDateStart),MAX(LoanPaymentDue))实现的目标是,我希望获得总月数,然后使用剩余的查询来计算月数。

我收到了这个错误:

  

Msg 130,Level 15,State 1,Line 11   无法对包含聚合或子查询的表达式执行聚合函数。

无论如何我可以通过相反的方式实现目标吗?

查询

SELECT 
ISNULL(SUM((CAST(((((lt.InterestRate/100) * lc.LoanAmount) +
 lc.LoanAmount) / ((dbo.fnNumberOfYears(CONVERT(VARCHAR(15), LoanDateStart, 
101), CONVERT(VARCHAR(15), LoanPaymentDue, 101)) * DATEDIFF(MONTH, 
MIN(LoanDateStart),  MAX(LoanPaymentDue)))  * 2)) AS DECIMAL(18,2)))),0)
FROM LoanContract lc 
INNER JOIN LoanType lt ON lt.LoanTypeID = lc.LoanTypeID 
WHERE lc.LoanTypeID = 1 AND lc.EmployeeID = 5

2 个答案:

答案 0 :(得分:0)

无论如何,我现在修正了这个问题,我使用LEFT OUTER JOIN

这是我的代码:

SELECT
ISNULL(SUM((CAST(((((lt.InterestRate/100) * lc.LoanAmount) + lc.LoanAmount) / ((dbo.fnNumberOfYears(CONVERT(VARCHAR(15), LoanDateStart, 101), CONVERT(VARCHAR(15), LoanPaymentDue, 101)) * x.NumberOfMonths)  * 2)) AS DECIMAL(18,2)))),0)
FROM LoanContract lc 
INNER JOIN LoanType lt ON lt.LoanTypeID = lc.LoanTypeID
LEFT OUTER JOIN(SELECT
lc.LoanTypeID AS 'LoanTypeID',
lc.EmployeeID AS 'EmployeeID', 
(DATEDIFF(MONTH, MIN(LoanDateStart),  MAX(LoanPaymentDue))) AS 'NumberOfMonths'
FROM LoanContract lc 
INNER JOIN LoanType lt ON lt.LoanTypeID = lc.LoanTypeID
GROUP BY lc.EmployeeID, lc.LoanTypeID) x ON x.EmployeeID = lc.EmployeeID AND x.LoanTypeID = lc.LoanTypeID 
WHERE lc.LoanTypeID = 1 AND lc.EmployeeID = 5

答案 1 :(得分:0)

请尝试以下方法......

SELECT ISNULL( SUM( ( CAST( ( ( ( ( lt.InterestRate / 100 ) *
                                  LoanContract.LoanAmount ) +
                                LoanContract.LoanAmount ) /
                              ( ( dbo.fnNumberOfYears( CONVERT( VARCHAR( 15 ),
                                                                LoanDateStart,
                                                                101 ),
                                                       CONVERT( VARCHAR( 15 ),
                                                                LoanPaymentDue,
                                                                101 ) ) *
                                  monthsDifference ) *
                                2 ) ) AS DECIMAL( 18,
                                                  2 ) ) ) ),
               0 )
FROM ( SELECT LoanContractID AS LoanContractID,
              DATEDIFF( MONTH,
                        MIN( LoanDateStart ),
                        MAX( LoanPaymentDue ) ) AS monthsDifference
       FROM LoanContract
       GROUP BY LoanContractID
     ) AS monthsDifferenceFinder
INNER JOIN LoanContract ON LoanContract.LoanContractID = monthsDifferenceFinder.LoanContractID
INNER JOIN LoanType lt ON lt.LoanTypeID = LoanContract.LoanTypeID
WHERE lc.LoanTypeID = 1
  AND lc.EmployeeID = 5

请注意,我已使用LoanContractID代替LoanContract的主键,因为您尚未说明这是什么。

问题的原因是SUM()(聚合函数)对MIN()MAX()的结果进行了操作,这些结果本身就是聚合函数。这困惑SQL-Server

我通过编写一个子查询来解决这个问题,该子查询根据LoanContract的唯一标识符确定每个LoanContract的两个值之间的差异,而不是每个记录的主键。 (您的数据是否为3NF(第三范式)?)

然后,子查询的结果将基于LoanContract加入LoanContractID,有效地将每个LoanContract的{​​{1}}值附加到monthsDifference的每条记录中}。

然后

LoanContract可以处理检索到的值,而不必尝试聚合正确的聚合值。

如果您有任何问题或意见,请随时发表评论。