简体   繁体   English

分组依据的列名称不明确

[英]Ambiguous Column Name with Group By

I am having Ambiguous Column Name "Item" error for the query below. 我在下面的查询中出现Ambiguous Column Name "Item"错误。 However, I already type in the desired form as parameters are at the beginning of columns. 但是,由于参数位于列的开头,因此我已经输入了所需的形式。

        SELECT  
          [Country Code],
          Item, 
          [FE SSO], 
          [Newest Job Number],
          [Newest Transaction Date],
          Z.ConsignDate AS [ConsignDate],
        FROM DailyOnhand

        LEFT JOIN 
            (SELECT 
              [Job Number], 
              [Item],
              Min([Transaction Day]) AS ConsignDate
            FROM vwAllTxns
            GROUP BY [Job Number], [Item]) Z 
                  ON vwDailyOnhand_v2.[Newest Job Number] = Z.[Job Number] 
                     AND vwDailyOnhand_v2.[Item] = Z.[Item]

Any help is appreciated. 任何帮助表示赞赏。 Thank you! 谢谢!

You need to prefix item in your select with the name/alias of the table it is sourced from. 您需要在select item加上其来源表的名称/别名。

SELECT  
      d.[Country Code],
      d.Item, 
      d.[FE SSO], 
      d.[Newest Job Number],
      d.[Newest Transaction Date],
      Z.ConsignDate AS [ConsignDate],
    FROM DailyOnHand d
      LEFT JOIN 
        (SELECT 
          v.[Job Number], 
          v.[Item],
          Min(v.[Transaction Day]) AS ConsignDate
        FROM vwAllTxns v
        GROUP BY v.[Job Number], v.[Item]
        ) Z 
              ON d.[Newest Job Number] = Z.[Job Number] 
                 AND d.[Item] = Z.[Item]

Your from specifies DailyOnHand but your on specifies vwDailyOnhand_v2 , I removed the later and used an alias instead. 您的from指定DailyOnHand但您的on指定vwDailyOnhand_v2 ,我删除了后者,并改用了别名。

    SELECT  
      [Country Code],
      Z.[Item], -- Need to specify which item is source 
      [FE SSO], 
      [Newest Job Number],
      [Newest Transaction Date],
      Z.ConsignDate AS [ConsignDate],
    FROM DailyOnhand

    LEFT JOIN 
        (SELECT 
          [Job Number], 
          [Item],
          Min([Transaction Day]) AS ConsignDate
        FROM vwAllTxns
        GROUP BY [Job Number], [Item]) Z 
              ON vwDailyOnhand_v2.[Newest Job Number] = Z.[Job Number] 
                 AND vwDailyOnhand_v2.[Item] = Z.[Item]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM