Gross profit by invoice
Sales, gross profit and margin percent per A/R invoice for a date range, from the gross profit SAP B1 stores on each row.
Tool
SELECT T0.DocNum, T0.DocDate, T0.CardCode, T0.CardName,
SUM(T1.LineTotal) AS Sales, SUM(T1.GrssProfit) AS GrossProfit,
CASE WHEN SUM(T1.LineTotal) = 0 THEN 0
ELSE ROUND(SUM(T1.GrssProfit) * 100.0 / SUM(T1.LineTotal), 2) END AS MarginPct
FROM OINV T0
INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry
WHERE T0.CANCELED = 'N' AND T0.DocDate BETWEEN '[%0]' AND '[%1]'
GROUP BY T0.DocNum, T0.DocDate, T0.CardCode, T0.CardName
ORDER BY T0.DocDate, T0.DocNum SELECT T0."DocNum", T0."DocDate", T0."CardCode", T0."CardName",
SUM(T1."LineTotal") AS "Sales", SUM(T1."GrssProfit") AS "GrossProfit",
CASE WHEN SUM(T1."LineTotal") = 0 THEN 0
ELSE ROUND(SUM(T1."GrssProfit") * 100.0 / SUM(T1."LineTotal"), 2) END AS "MarginPct"
FROM OINV T0
INNER JOIN INV1 T1 ON T0."DocEntry" = T1."DocEntry"
WHERE T0."CANCELED" = 'N' AND T0."DocDate" BETWEEN '[%0]' AND '[%1]'
GROUP BY T0."DocNum", T0."DocDate", T0."CardCode", T0."CardName"
ORDER BY T0."DocDate", T0."DocNum" What this query answers
Finance wants to see the profit on each customer invoice, not just the revenue. This returns the net sales, the gross profit and the margin percent per A/R invoice in a date range. The gross profit comes from the GrssProfit value that SAP Business One stores on every invoice row, so it follows the company's gross profit settings.
Sample output
Illustrative demo values. Your columns are the same; the numbers come from your company.
| DocNum | DocDate | CardCode | CardName | Sales | GrossProfit | MarginPct |
|---|---|---|---|---|---|---|
| 1450 | 2026-10-02 | C20000 | Acme Trading | 12400 | 3720 | 30 |
| 1451 | 2026-10-02 | C30000 | Northwind Retail | 5300 | 1060 | 20 |
| 1452 | 2026-10-03 | C40000 | Blue Harbor Foods | 860 | -40 | -4.65 |
| 1453 | 2026-10-04 | C20000 | Acme Trading | 2150 | 774 | 36 |
SQL Server vs SAP HANA
CASE and ROUND work the same on HANA. Quote T1."GrssProfit" exactly: the column name has a double s and no o, while the header field on OINV is spelled GrosProfit.
Common errors and fixes
- Margin differs from the gross profit shown on the document: Sales here is the sum of the row totals, which do not include the header discount. If the invoice has a header discount, compare with the header field OINV.GrosProfit instead.
- Negative margin on a normal sale: Row gross profit depends on the item cost at posting time and on the base price your company uses for gross profit. Check the item cost before you change prices.
- Division by zero: The CASE returns 0 when the invoice total is zero, for example a fully free invoice. Keep it if you add more ratio columns.
Need this report built into SAP Business One?
A saved query answers the question once. We do SAP Business One development: add-ons, integrations and reports, from the database to the screen. Or hire a remote SAP Business One developer for the whole job, fixed price per phase.
Related queries
Table reference: OINV table (A/R Invoice (header)) · INV1 table (A/R Invoice (lines))
Three steps, no signup
Copy the right version
Use the SQL Server tab for MS SQL databases and the HANA tab for SAP B1 on HANA.
Open Query Generator
In SAP Business One go to Tools → Queries → Query Generator, paste and click Execute.
Save and share
Click Save, pick a category and give the query a name. Your team can run it from Query Manager.
Questions about this tool
How do I run the gross profit by invoice query in SAP Business One?
Copy the version that matches your database, open Tools → Queries → Query Generator, paste it in the SQL box and click Execute. Save it to reuse it later.
Which tables does this query use?
It reads OINV, INV1 from the standard SAP Business One schema. No user-defined fields are needed.
What should I enter when the selection window opens?
This query uses parameters: [%0] = from date, [%1] = to date. SAP B1 shows a selection window for each one when you run it.
Can this be turned into a dashboard or alert?
Yes. We turn queries into Crystal reports, Excel reports that refresh themselves, SAP B1 alerts or WhatsApp messages for managers.
Built for SAP Business One teams
Need this inside SAP Business One?
We build add-ons, reports and integrations for SAP B1 on SQL Server and HANA. Tell us what you need and get a fixed price, usually within 24 hours.
