Sales by customer for a date range
Net sales per customer (invoices minus credit notes) between two dates, highest first.
Tool
SELECT X.CardCode, X.CardName, SUM(X.NetSales) AS NetSales FROM ( SELECT T0.CardCode, T0.CardName, T0.DocTotal - T0.VatSum AS NetSales FROM OINV T0 WHERE T0.CANCELED = 'N' AND T0.DocDate BETWEEN '[%0]' AND '[%1]' UNION ALL SELECT T1.CardCode, T1.CardName, -(T1.DocTotal - T1.VatSum) FROM ORIN T1 WHERE T1.CANCELED = 'N' AND T1.DocDate BETWEEN '[%0]' AND '[%1]' ) X GROUP BY X.CardCode, X.CardName ORDER BY NetSales DESC
SELECT X."CardCode", X."CardName", SUM(X."NetSales") AS "NetSales" FROM ( SELECT T0."CardCode", T0."CardName", T0."DocTotal" - T0."VatSum" AS "NetSales" FROM OINV T0 WHERE T0."CANCELED" = 'N' AND T0."DocDate" BETWEEN '[%0]' AND '[%1]' UNION ALL SELECT T1."CardCode", T1."CardName", -(T1."DocTotal" - T1."VatSum") FROM ORIN T1 WHERE T1."CANCELED" = 'N' AND T1."DocDate" BETWEEN '[%0]' AND '[%1]' ) X GROUP BY X."CardCode", X."CardName" ORDER BY "NetSales" DESC
What this query answers
Before the monthly sales meeting, the sales manager wants one ranked list of customers by what they actually bought in the period. Returns matter here: a customer who was invoiced 50,000 and then credited 20,000 should show 30,000, not 50,000.
Want this as a chart your team can filter? See how to build an SAP Business One dashboard without Power BI.
Sample output
Illustrative demo values. Your columns are the same; the numbers come from your company.
| CardCode | CardName | NetSales |
|---|---|---|
| C20000 | Acme Trading | 48250 |
| C30000 | Northwind Retail | 31720.5 |
| C40000 | Blue Harbor Foods | 18900 |
| C50000 | Summit Supplies | 6410.75 |
SQL Server vs SAP HANA
The logic is identical; the HANA version only wraps every column and alias in double quotes (T0."DocTotal", AS "NetSales"), because HANA folds unquoted names to upper case. The unnamed negative column in the second half of the UNION is fine in both databases because the column names come from the first SELECT.
Common errors and fixes
- Cancelled invoices still change the totals: Keep CANCELED = 'N' on both halves. In SAP B1 a cancelled invoice gets CANCELED = 'Y' and its cancellation credit note gets CANCELED = 'C'; filtering on 'N' removes both, so they do not net against each other by accident.
- Conversion failed when converting date and/or time from character string: When you test outside SAP B1, replace '[%0]' and '[%1]' with dates in YYYYMMDD format (for example '20261001'), which SQL Server reads the same way regardless of language settings.
- Totals differ from the customer's foreign-currency statement: DocTotal and VatSum are in local currency. Use DocTotalFC - VatSumFC on both halves of the UNION to rank customers in their document currency.
- Totals don't match the Sales Analysis report (header discount): This query reads DocTotal - VatSum from the document header, which already includes the header discount (OINV.DiscPrcnt), plus freight. If you change it to sum INV1.LineTotal, for example for item revenue, the header discount is not inside LineTotal and the line sum overstates sales. Use SUM(T1.LineTotal * (1 - T0.DiscPrcnt / 100.0)) (HANA: SUM(T1."LineTotal" * (1 - T0."DiscPrcnt" / 100.0))), and subtract credit memos (ORIN/RIN1) with the same factor. See SAP B1 sales total mismatch: header discount fix.
Need this report built into SAP Business One?
A saved query answers the question once. We build Crystal Reports and Power BI dashboards for SAP Business One on SQL Server and HANA, and you own the files. 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)) · ORIN table (A/R Credit Memo (header))
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 sales by customer for a date range 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, ORIN 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.
