我正在尝试加入以下两个查询:
InventoryItemID似乎无法使其发挥作用。我已经尝试了很多解决方案,但无法绕过它。我正在使用Microsoft SQL。
select t1.itemcode, t1.Description, t1.MinimumQuantity, t1.QuantityInStore, t2.qty as OnOrder, t1.InventoryItemID
from
(
select i.InventoryItemID, i.ItemCode, i.Description, MinimumQuantity, ii.QuantityInStore
from InventoryInfo i
inner join InventoryStoreInfo ii
on i.InventoryItemID = ii.inventoryitemid
inner join PurchaseOrderItemInfo p
on ii.InventoryItemID = p.InventoryItemID
inner join PurchaseOrderInfo po
on p.PurchaseOrderID = po.PurchaseOrderID
inner join VendorInfo v
on po.VendorID = v.VendorID
where InventoryCategoryID = '1'
and i.IsActive = '1'
) as t1
left join
(
select InventoryItemID, sum(Quantity) as QTY, Description
from PurchaseOrderItemInfo p
inner join PurchaseOrderInfo po
on p.PurchaseOrderID = po.PurchaseOrderID
where p.status = '0'
group by InventoryItemID, Description
) as t2
on t1.inventoryitemid = t2.inventoryitemid
和
select pi.InventoryItemID, v.VendorName, max(p.PODateTime) as latest
from PurchaseOrderItemInfo pi
inner join PurchaseOrderInfo p
on pi.PurchaseOrderID = p.PurchaseOrderID
inner join InventoryInfo i
on pi.InventoryItemID = i.InventoryItemID
inner join VendorInfo v
on p.VendorID = v.VendorID
group by pi.inventoryitemid, p.PODateTime,i.description, v.VendorName
order by latest desc
答案 0 :(得分:2)
您在括号之间包装每个查询以创建子查询(queryA)
和(queryB)
然后为每个子查询分配别名并执行连接
SELECT aliasA.*, aliasB.*
FROM
(queryA) aliasA
INNER JOIN (queryB) aliasB
ON aliasA.ID = aliasB.ID
使用您的查询
SELECT FIRSTQUERY.*, SECONDQUERY.*
FROM
(
select t1.itemcode, t1.Description, t1.MinimumQuantity, t1.QuantityInStore, t2.qty as OnOrder, t1.InventoryItemID
from
(
select i.InventoryItemID, i.ItemCode, i.Description, MinimumQuantity, ii.QuantityInStore
from InventoryInfo i
inner join InventoryStoreInfo ii
on i.InventoryItemID = ii.inventoryitemid
inner join PurchaseOrderItemInfo p
on ii.InventoryItemID = p.InventoryItemID
inner join PurchaseOrderInfo po
on p.PurchaseOrderID = po.PurchaseOrderID
inner join VendorInfo v
on po.VendorID = v.VendorID
where InventoryCategoryID = '1'
and i.IsActive = '1'
) as t1
left join
(
select InventoryItemID, sum(Quantity) as QTY, Description
from PurchaseOrderItemInfo p
inner join PurchaseOrderInfo po
on p.PurchaseOrderID = po.PurchaseOrderID
where p.status = '0'
group by InventoryItemID, Description
) as t2
on t1.inventoryitemid = t2.inventoryitemid
) FIRSTQUERY
INNER JOIN
(
select pi.InventoryItemID, v.VendorName, max(p.PODateTime) as latest
from PurchaseOrderItemInfo pi
inner join PurchaseOrderInfo p
on pi.PurchaseOrderID = p.PurchaseOrderID
inner join InventoryInfo i
on pi.InventoryItemID = i.InventoryItemID
inner join VendorInfo v
on p.VendorID = v.VendorID
group by pi.inventoryitemid, p.PODateTime,i.description, v.VendorName
order by latest desc
) SECONDQUERY
ON FIRSTQUERY.InventoryItemID = SECONDQUERY.InventoryItemID