简体   繁体   中英

Ambiguous Column Name with Group By

I am having Ambiguous Column Name "Item" error for the query below. 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  
      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.

    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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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