Hi Doug,
The issue here is that the batch allocation is done on a Goods Issue transaction and not directly from the production order. The link between these two is that any Goods Issue lines that have been created by issuing stock to production will have a BaseType of 202 and a BaseEntry value that matches the DocEntry of the production order. Also, there's a zero-to-many relationship between these tables as you can issue stock multiple times to the same production order.
Something like the following should do the job:
select
T0.DocEntry,
T0.DocNum,
isnull(T5.CardCode, '') as CardCode,
T1.Quantity,
T1.ItemCode,
T3.AllocQty as BatchSerialQty,
T4.DistNumber as BatchorSerialNum,
T4.sysnumber
from
OIGE T0
inner join IGE1 T1 on T0.DocEntry = T1.DocEntry
inner join OITM T2 on T1.ItemCode = T2.ItemCode
inner join (select S0.DocEntry, S0.DocLine, S1.SysNumber, -sum(S1.Quantity) as AllocQty from OITL S0 inner join ITL1 S1 on S0.LogEntry = S1.LogEntry where S0.DocType = 60 group by S0.DocEntry, S0.DocLine, S1.SysNumber) T3 on T1.DocEntry = T3.DocEntry and T1.LineNum = T3.DocLine
inner join OBTN T4 on T3.SysNumber = T4.SysNumber and T1.ItemCode = T4.ItemCode
inner join OWOR T5 on T1.BaseEntry = T5.DocEntry
where
T1.BaseType = 202 and T1.BaseEntry = 175
Kind Regards
Owen