How do I get every AP reserve invoice for a purchase order using the SQL version of SAP Business One?
My current query:
SELECT
OPOR.CardCode,
OPOR.CardName,
OPOR.U_ReferenceNo,
OPOR.NumAtCard,
OPOR.DocNum,
OPOR.U_ShipmentTime,
OPOR.U_DealType,
OPOR.DocDate,
OPOR.U_Origin,
OPOR.U_DealQuantity,
OPOR.U_ContainerNo,
POR1.ItemCode,
POR1.Dscription,
POR1.Price,
por1.U_CopperRate,
POR1.U_AluminiumRate,
POR1.U_IronRate,
POR1.U_CopperPer,
por1.U_AluminiumPer,
POR1.U_LeadPer,
POR1.U_IronPer,
POR1.U_PlasticPer
FROM
OPOR
INNER JOIN POR1 ON OPOR.DocEntry = POR1.DocEntry
WHERE
OPOR.DocStatus = 'o'
You can use sap.erpref.com to get more details about every table on a SAP Business one SQL schema.
The A/P invoice table is OPCH
, and the field reserve
means it is a A/P reserve invoice.
So your select should be like this:
SELECT
OPOR.CardCode,
MAX(OPOR.CardName),
MAX(OPOR.U_ReferenceNo),
MAX(OPOR.NumAtCard),
MAX(OPOR.DocNum),
MAX(OPOR.U_ShipmentTime),
MAX(OPOR.U_DealType),
MAX(OPOR.DocDate),
MAX(OPOR.U_Origin),
MAX(OPOR.U_DealQuantity),
MAX(OPOR.U_ContainerNo),
MAX(POR1.ItemCode),
MAX(POR1.Dscription),
MAX(POR1.Price),
MAX(POR1.U_CopperRate),
MAX(POR1.U_AluminiumRate),
MAX(POR1.U_IronRate),
MAX(POR1.U_CopperPer),
MAX(POR1.U_AluminiumPer),
MAX(POR1.U_LeadPer),
MAX(POR1.U_IronPer),
MAX(POR1.U_PlasticPer),
COUNT(OPCH. DocNum)
FROM
OPOR
LEFT JOIN POR1 ON OPOR.DocEntry = POR1.DocEntry
LEFT JOIN OPCH ON OPOR.DocEntry = OPCH.DocEntry
WHERE
OPOR.DocStatus = 'o'
AND OPCH.reserve = 'Y'
GROUP BY
OPOR.CardCode