[英]Why am I Getting an Ambiguous Column name CountryID error message when trying to execute this SQL statement:
SELECT
CD.CountryID, CD.GrossLimit, CD.UnsecuredLimit,
SUM(LT1.Amount), SUM(LT1.Unsecured), (100*SUM(LT1.Unsecured) / CD.UnsecuredLimit) as PercOverCountryLimit
FROM CountryDetail CD
INNER JOIN
(
SELECT CompanyName AS Company, CollateralSName as Collateral, SUM(Amount) AS Amount,
SUM(Usecured) AS Unsecured, LT.Date as Date, Max(LT.CountryID) as CountryID
FROM Loanstotal LT
WHERE YearMonth = @YearMonth
GROUP BY CompanyName, CollateralSName, LT.Date
) LT1
ON CD.CountryID = LT1.CountryID
GROUP BY CountryID, GrossLimit, UnsecuredLimit
Because you're grouping by CountryID
which is being returned by both tables: CountryDetail
and your sub-query.因为您按
CountryID
分组,这两个表都返回: CountryDetail
和您的子查询。 Change your GROUP BY
to GROUP BY CD.CountryID, GrossLimit, UnsecuredLimit
.将您的
GROUP BY
更改为GROUP BY CD.CountryID, GrossLimit, UnsecuredLimit
。
In fact, I think as a best practice you should change all your column references so they're qualified with the proper aliases:事实上,我认为作为最佳实践,您应该更改所有列引用,以便它们使用正确的别名进行限定:
GROUP BY CD.CountryID, CD.GrossLimit, CD.UnsecuredLimit
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.