Trey -
Without warranty or assurance (please validate your data), here is a script to build a view I believe accomplishes what you're looking for. It starts by pulling all the data from the Inventory Lot Transaction Table. Then Joins Continuing Table Expressions, which pull data from PO Receipt Line work and history tables and Purchases Serial / Lot work tables. It also pulls Sales Serial / Lot Work & History Table, joining in SOP Header Tables Work and History. In the end, for Lot Items It should give you the customer/vendor (Master_Name), Document Number (PO or Invoice), Item_Number, Item_Description, Location, Lot, and Quantities Received, Sold and currently On_Hand. My Fabrikam demo environment ties out.
CREATE View [dbo].[_Lots_Breakdown]
as
with
CTE_POs
as
(
select POP10310.PONUMBER PO,POP10310.ITEMNMBR ITEM,POP10330.SERLTNUM LOT,POP10330.RCPTLNNM LINE,POP10310.POPRCTNM RCPT from POP10310
left join POP10330 on POP10330.POPRCTNM = POP10310.POPRCTNM and POP10310.RCPTLNNM = POP10330.RCPTLNNM
where SERLTNUM is not null
Union
select POP30310.PONUMBER PO,POP30310.ITEMNMBR ITEM,POP30330.SERLTNUM LOT,POP30330.RCPTLNNM LINE,POP30310.POPRCTNM RCPT from POP30310
left join POP30330 on POP30310.POPRCTNM = POP30330.POPRCTNM and POP30310.RCPTLNNM = POP30330.RCPTLNNM
where SERLTNUM is not null
)
,
CTE_Receipts
as
(
select POP10330.POPRCTNM,ITEMNMBR,SERLTNUM,VNDDOCNM from POP10330
left join POP10300 on POP10330.POPRCTNM = POP10300.POPRCTNM
Union All
select POP30330.POPRCTNM,ITEMNMBR,SERLTNUM,VNDDOCNM from POP30330
left join POP30300 on POP30330.POPRCTNM = POP30300.POPRCTNM
)
,
CTE_Lot_Sales
as
(
select ITEMNMBR ITEM,SOP10201.SERLTNUM LOT, SERLTQTY Qty_Sold, SOP10201.SOPNUMBE Invoice_Number
,isnull(SOP10100.CUSTNMBR,SOP30200.CUSTNMBR) CUSTOMER_ID
,isnull(SOP10100.CUSTNAME,SOP30200.CUSTNAME) CUSTOMER_NAME
from SOP10201
left Join SOP10100 ON SOP10201.SOPTYPE = SOP10100.SOPTYPE and SOP10201.SOPNUMBE = SOP10100.SOPNUMBE
left Join SOP30200 on SOP10201.SOPTYPE = SOP30200.SOPTYPE and SOP10201.SOPNUMBE = SOP30200.SOPNUMBE
)
select distinct
isnull(CTE_Lot_Sales.CUSTOMER_NAME,VEND.VENDNAME) Master_Name,
isnull(CTE_POs.PO,CTE_Lot_Sales.Invoice_Number) as Doc_Number,
LOTS.[ITEMNMBR] AS Item_Number,
ITEMS.[ITEMDESC] AS Item_Description,
LOTS.[LOCNCODE] AS Location_Code,
LOTS.[LOTNUMBR] AS Lot_Number,
LOTS.[QTYRECVD] AS QTY_Received,
isnull(CTE_Lot_Sales.Qty_Sold,0) Qty_Sold,
LOTS.[QTYRECVD]-LOTS.[QTYSOLD]-LOTS.[ATYALLOC] AS QTY_ON_HAND
from IV00300 as LOTS
inner join IV00101 as items on LOTS.ITEMNMBR = ITEMS.ITEMNMBR
left join PM00200 as VEND on LOTS.VNDRNMBR = VEND.VENDORID
Left Join CTE_Receipts on LOTS.ITEMNMBR = CTE_Receipts.ITEMNMBR and LOTS.LOTNUMBR = CTE_Receipts.SERLTNUM
left join CTE_POs on LOTS.ITEMNMBR = CTE_POs.ITEM and LOTS.LOTNUMBR = CTE_POs.LOT
left join CTE_Lot_Sales on LOTS.ITEMNMBR = CTE_Lot_Sales.ITEM and LOTS.LOTNUMBR = CTE_Lot_Sales.LOT
where (CTE_Lot_Sales.Invoice_Number is not null OR CTE_POs.PO is not null)
Order by Lot_Number
go
Grant Select on _Lots_Breakdown to DYNGRP