I am running into a problem because I am trying to do an explicit join in an inner join where I am trying to select certain values from another table.
My SQL:
SELECT A.OrderID
, A.ItemID
, A.Line
, B.LID
, B.Quantity
FROM Sales A
INNER JOIN (SELECT X.OrderID
, X.Line
, SUM(X.Quantity) AS Quantity
, X.DestinationID
, X.SerialNum
FROM SalesDetail X
WHERE X.OrderID = A.OrderID
GROUP BY X.OrderID, X.Line, X.DestinationID, X.SerialNum
) AS C
ON A.Line = C.Line
AND B.Quantity = C.Quantity
AND B.SerialNum = C.SerialNum
INNER JOIN SalesContents B
ON A.OrderID = B.OrderID
AND A.ItemID = B.ItemID
WHERE A.OrderID = '12345'
AND A.ItemID = 'ABC123';
Now I can remove the WHERE clause within the X table and filter it within the inner join but I would like to filter it right when I am querying the values. Is there a way to rewrite this statement so that the unbound identifier can be worked around?
AND B.Quantity = C.Quantity AND B.SerialNum = C.SerialNum
after youjoin
SalesContents B (alias). – Vamsi Prabhalas
forsales
rather thana
). If you fix the query to order the conditions correctly and remove thewhere
clause in the subquery, your might be able to fix the query yourself. – Gordon Linoff