SQL - 无法绑定多部分标识符

SQL - The multi-part identifier could not be bound

我有一个要编辑的 SQL 查询。它 returns 错误 :

"The multi-part identifier "i.LastPurPrc" could not be bound." when I try to add a column 'Amount1'. (Error on 2nd line of query code)

查询:

Select a.Itemcode, max(a.Dscription) as ItemName,
sum(a.OpeningBalance) as OpeningBalance, sum(a.OpeningBalance) * i.LastPurPrc AS 'Amount1', sum(a.INq) as 'IN', sum(a.OUT) as OUT,
((sum(a.OpeningBalance) + sum(a.INq)) - sum(a.OUT)) as Closing,
(Select i.InvntryUom from OITM i 
Where i.ItemCode = a.Itemcode) as UOM
from 
(Select N1.Warehouse, N1.Itemcode, N1.Dscription, (sum(N1.inqty)-sum(n1.outqty))
as OpeningBalance, 0 as INq, 0 as OUT 
from dbo.OINM N1
Where N1.DocDate < '04-01-2015' and N1.Warehouse = 'WNR02' 
Group By N1.Warehouse,N1.ItemCode,
N1.Dscription 
Union All 
Select N1.Warehouse, N1.Itemcode, N1.Dscription, 0 as OpeningBalance,
sum(N1.inqty), 0 as OUT 
from dbo.OINM N1 
Where N1.DocDate >= '04-01-2015' and N1.DocDate <= '04-30-2015'
and N1.Inqty > 0 and N1.Warehouse = 'WNR02' 
Group By N1.Warehouse, N1.ItemCode, N1.Dscription
Union All 
Select N1.Warehouse, N1.Itemcode, N1.Dscription, 0 as OpeningBalance, 0 , sum(N1.outqty) as OUT
From dbo.OINM N1 
Where N1.DocDate >= '04-01-2015' and N1.DocDate <= '04-30-2015' and N1.OutQty > 0
and N1.Warehouse = 'WNR02' 
Group By N1.Warehouse,N1.ItemCode,N1.Dscription) a, dbo.OITM I1
where a.ItemCode = I1.ItemCode
Group By a.Itemcode Having sum(a.OpeningBalance) + sum(a.INq) + sum(a.OUT) > 0 Order By a.Itemcode

我该如何解决?

你的 i table 在子选择中,你不能在外部字段列表中引用 table。

您需要加入 table 而不是将其添加为子选择才能引用它。

由于您是按 itemid 分组的,因此您可能还应该对其求和。

由于我们没有演示数据,所以我不确定每个 itemid 有多少 IUOM 记录,但如果是 1:1,这应该可行。如果不是,您将不得不加入别名查询而不是 table 本身。

SELECT a.Itemcode
    ,max(a.Dscription) AS ItemName
    ,sum(a.OpeningBalance) AS OpeningBalance
    ,sum(a.OpeningBalance) * sum(i.LastPurPrc) AS 'Amount1'
    ,sum(a.INq) AS 'IN'
    ,sum(a.OUTPUT) AS OUTPUT
    ,((sum(a.OpeningBalance) + sum(a.INq)) - sum(a.OUTPUT)) AS Closing

FROM (
    SELECT N1.Warehouse
        ,N1.Itemcode
        ,N1.Dscription
        ,(sum(N1.inqty) - sum(n1.outqty)) AS OpeningBalance
        ,0 AS INq
        ,0 AS OUTPUT
    FROM dbo.OINM N1
    WHERE N1.DocDate < '04-01-2015'
        AND N1.Warehouse = 'WNR02'
    GROUP BY N1.Warehouse
        ,N1.ItemCode
        ,N1.Dscription
    
    UNION ALL
    
    SELECT N1.Warehouse
        ,N1.Itemcode
        ,N1.Dscription
        ,0 AS OpeningBalance
        ,sum(N1.inqty)
        ,0 AS OUTPUT
    FROM dbo.OINM N1
    WHERE N1.DocDate >= '04-01-2015'
        AND N1.DocDate <= '04-30-2015'
        AND N1.Inqty > 0
        AND N1.Warehouse = 'WNR02'
    GROUP BY N1.Warehouse
        ,N1.ItemCode
        ,N1.Dscription
    
    UNION ALL
    
    SELECT N1.Warehouse
        ,N1.Itemcode
        ,N1.Dscription
        ,0 AS OpeningBalance
        ,0
        ,sum(N1.outqty) AS OUTPUT
    FROM dbo.OINM N1
    WHERE N1.DocDate >= '04-01-2015'
        AND N1.DocDate <= '04-30-2015'
        AND N1.OutQty > 0
        AND N1.Warehouse = 'WNR02'
    GROUP BY N1.Warehouse
        ,N1.ItemCode
        ,N1.Dscription
    ) a

JOIN OITM i 
ON i.itemcode = a.itemcode
JOIN dbo.OITM I1
ON  a.ItemCode =I1.ItemCode

GROUP BY a.Itemcode
HAVING sum(a.OpeningBalance) + sum(a.INq) + sum(a.OUTPUT) > 0
ORDER BY a.Itemcode

您还应该尝试格式化您的查询并更好地缩进它们。它会帮助你更好地发现逻辑。 如果它是继承代码,只需通过任何在线 sql 格式化程序将其拉出,我使用 poorsql

你也应该no longer use old-style joins