简体   繁体   English

为什么在尝试执行此 SQL 语句时会收到 Ambiguous Column name CountryID 错误消息:

[英]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.

相关问题 尝试将COM引用添加到“ Windows脚本宿主对象模型”时,为什么会收到此错误消息? - Why am I getting this error message when trying to add a COM reference to “Windows Script Host Object Model”? 为什么在尝试创建用户时出现无效对象名称错误? - Why am I getting an Invalid Object Name error when trying to create a user? 为什么在尝试将数据插入行时出现 SQL 语法错误? - Why am I getting an SQL Syntax error when trying to insert data into a row? 为什么在这里出现“歧义匹配”错误? - Why am I getting an “Ambiguous Match” error here? 为什么在尝试删除单个 object 时出现此错误? - Why am I getting this error when trying to delete a single object? 尝试进行外部联接时列名不明确 - Ambiguous column name when trying to outer join 我尝试执行Web应用程序时收到此错误消息 - i am getting this error message when i try to execute my web application 即使我使用完全限定的名称,也收到了模棱两可的引用错误 - I am getting an ambiguous reference error even though I am using fully qualified name 为什么我收到错误 CS0246:尝试使用 OpenHtmlToPdf 时找不到类型或命名空间名称? - Why am I getting error CS0246: The type or namespace name could not be found when trying to use OpenHtmlToPdf? 当我尝试订阅活动时,为什么会出现转换错误? - Why am I getting an error with conversion when I am trying to subscribe to event?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM