具有分组依据的不明确列名称

时间:2017-05-02 12:58:34

标签: sql-server-2012 group-by ambiguous

我在下面的查询中遇到Ambiguous Column Name "Item"错误。但是,我已经输入了所需的表格,因为参数位于列的开头。

        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]

感谢任何帮助。 谢谢!

2 个答案:

答案 0 :(得分:0)

您需要在item前面加select前缀,并附上其来源表格的名称/别名。

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]

您的from指定了DailyOnHand,但on指定了vwDailyOnhand_v2,我删除了后者并使用了别名。

答案 1 :(得分:0)

    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]