Search code examples
sqlxampp

How can I resolve the column in field list is ambiguous in SQL?


I wanted to join my tables product_sales and product. However, the product number seemed to be ambiguous. The following are my codes, kindly help me where I got it wrong. Thank you

INSERT INTO HSD_DW.PRODUCT_SALES
SELECT UNIX_TIMESTAMP(TimeID) AS TimeID, CustomerID, ProductNumber,
       SUM(Quantity) AS TotalQty, UnitPrice, SUM(Quantity) * UnitPrice AS Total
FROM HSD.PRODUCT_SALES AS PS
INNER JOIN hsd.product AS P ON PS.ProductNumber = P.ProductNumber
GROUP BY TimeID, CustomerID, ProductNumber, UnitPrice
ORDER BY TimeID;

Solution

  • Place "PS." before reference "ProductNumber" column.

        INSERT INTO HSD_DW.PRODUCT_SALES
        SELECT UNIX_TIMESTAMP(TimeID) AS TimeID, CustomerID, PS.ProductNumber,
               SUM(Quantity) AS TotalQty, UnitPrice, SUM(Quantity) * UnitPrice AS Total
        FROM HSD.PRODUCT_SALES AS PS
        INNER JOIN hsd.product AS P ON PS.ProductNumber = P.ProductNumber
        GROUP BY TimeID, CustomerID, PS.ProductNumber, UnitPrice
        ORDER BY TimeID;