Linking Sales Orders, Delivery Documents, and Invoices via HANA Queries

Have you ever tried to write a query linking marketing documents together, the way that B1’s “Relationship Map” does?

It’s not as straightforward as it seems. SAP B1 doesn’t link these documents on the Doc Number, as that would be too intuitive.

The following query will display the three stages of an “order”, and the posting date for each document:

SELECT S0."SalesOrderNumber", S0."DocDate" AS "Sales Order Date", S1."DeliveryNumber", S1."DocDate" AS "Delivery Posting Date", S2."DocNum" AS "InvoiceNumber", S2."DocDate" AS "Invoice Posting Date", S0."Rows", S2."DocTotal" AS "Invoice Total"
FROM (SELECT T0."DocDate", T0."DocEntry" AS "SO_DE", T0."DocNum" AS "SalesOrderNumber", T1."TrgetEntry" AS "SO_TE", COUNT(T0."DocNum") AS "Rows"
FROM ORDR T0
LEFT JOIN RDR1 T1 ON T0."DocEntry" = T1."DocEntry"
GROUP BY T0."DocEntry", T0."DocNum", T1."TrgetEntry", T0."DocDate") S0
LEFT JOIN (SELECT T0."DocEntry" AS "DN_DE", T0."DocNum" AS "DeliveryNumber", T1."TrgetEntry" AS "DN_TE", T0."DocDate"
FROM ODLN T0
LEFT JOIN DLN1 T1 ON T0."DocEntry" = T1."DocEntry"
GROUP BY T0."DocEntry", T0."DocNum", T1."TrgetEntry", T0."DocDate") S1 ON S0."SO_TE" = S1."DN_DE"
LEFT JOIN OINV S2 ON S1."DN_TE" = S2."DocEntry"

B1 uses DocEntry and TargetEntry fields to link these documents, so requires a sort of nested query approach.

The above query can be modified however you’d like, and used to track documents from the initial SO, through the invoices phase.

By:


Leave a Reply

Your email address will not be published. Required fields are marked *