This query is working fine if I use T3.CardCode = ‘AC0109’ in second query for inward values but if I use outward query refrence as (T3.CardCode = owt.CardCode) or (T3.CardCode = t1.CardCode) then it rises error
The multi-part identifier “owt.cardcode” or “t1.cardcode” could not be bound
Plz help me to solve this problem
SELECT TOP (100) PERCENT
owt.CardCode,
owt.CardName,
owt.DocDate,
owt.owtsector,
owt.owtzone,
owt.Dscription,
owt.owtrate,
owt.owtqty,
owt.amount,
ISNULL(inw.inwqty,0) AS Expr1,
ISNULL(inw.inwamount, 0) AS Expr2
FROM (SELECT T1.DocDate,
T1.CardCode,
T1.CardName,
T2.ItemCode,
T2.Dscription,
T1.U_STTYPE,
T1.U_SECTOR AS owtsector,
T1.U_ZONE AS owtzone,
SUM(T2.LineTotal) AS amount,
SUM(T2.Quantity) AS owtqty,
T2.CodeBars,
T2.Price AS owtrate
FROM dbo.OWTR AS T1
INNER JOIN dbo.WTR1 AS T2 ON T1.DocEntry = T2.DocEntry
WHERE (T1.DocDate = CONVERT(DATETIME, '2012-03-16 00:00:00', 102))
AND (T1.CardCode = N'ac0109')
AND (T1.U_STTYPE = N'OUTWARD')
AND (T1.U_SECTOR NOT LIKE 'wr%')
GROUP BY T1.DocDate,
T1.CardCode,
T1.CardName,
T2.ItemCode,
T2.Dscription,
T1.U_STTYPE,
T2.Price,
T1.U_SECTOR,
T1.U_ZONE, T2.CodeBars) AS owt
LEFT OUTER JOIN (SELECT T3.DocDate,
T3.CardCode,
T3.CardName,
T3.U_STTYPE,
T4.ItemCode,
T4.Dscription,
SUM(T4.Quantity) AS inwqty,
SUM(T4.LineTotal) AS inwamount
FROM dbo.OWTR AS T3
INNER JOIN dbo.WTR1 AS T4 ON T3.DocEntry = T4.DocEntry
WHERE (T3.DocDate = CONVERT(DATETIME, '2012-03-16 00:00:00', 102))
AND (T3.CardCode = ***owt.CardCode***)
AND (T3.U_STTYPE = N' inward ')
AND (T3.U_SECTOR NOT LIKE ' wr % ')
GROUP BY T3.DocDate,
T3.CardCode,
T3.CardName,
T4.ItemCode,
T4.Dscription,
T3.U_STTYPE) AS inw
ON owt.CardCode = inw.CardCode
AND owt.DocDate = inw.DocDate
AND owt.ItemCode = inw.ItemCode
The error is within your second subquery (inw) on this line:
I think it should be:
EDIT
On further thought I don’t think you need this predicate at all since it is already contained in the join conditions.