Sales by item group
Net sales and quantity per item group (invoices minus credit memos, after header discount) for a date range.
Tool
SELECT X.ItmsGrpNam, SUM(X.Qty) AS Quantity, SUM(X.NetSales) AS NetSales FROM ( SELECT T3.ItmsGrpNam, T1.Quantity AS Qty, T1.LineTotal * (1 - T0.DiscPrcnt / 100.0) AS NetSales FROM OINV T0 INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry INNER JOIN OITM T2 ON T1.ItemCode = T2.ItemCode INNER JOIN OITB T3 ON T2.ItmsGrpCod = T3.ItmsGrpCod WHERE T0.CANCELED = 'N' AND T0.DocDate BETWEEN '[%0]' AND '[%1]' UNION ALL SELECT T3.ItmsGrpNam, -T1.Quantity, -(T1.LineTotal * (1 - T0.DiscPrcnt / 100.0)) FROM ORIN T0 INNER JOIN RIN1 T1 ON T0.DocEntry = T1.DocEntry INNER JOIN OITM T2 ON T1.ItemCode = T2.ItemCode INNER JOIN OITB T3 ON T2.ItmsGrpCod = T3.ItmsGrpCod WHERE T0.CANCELED = 'N' AND T0.DocDate BETWEEN '[%0]' AND '[%1]' ) X GROUP BY X.ItmsGrpNam ORDER BY NetSales DESC
SELECT X."ItmsGrpNam", SUM(X."Qty") AS "Quantity", SUM(X."NetSales") AS "NetSales" FROM ( SELECT T3."ItmsGrpNam", T1."Quantity" AS "Qty", T1."LineTotal" * (1 - T0."DiscPrcnt" / 100.0) AS "NetSales" FROM OINV T0 INNER JOIN INV1 T1 ON T0."DocEntry" = T1."DocEntry" INNER JOIN OITM T2 ON T1."ItemCode" = T2."ItemCode" INNER JOIN OITB T3 ON T2."ItmsGrpCod" = T3."ItmsGrpCod" WHERE T0."CANCELED" = 'N' AND T0."DocDate" BETWEEN '[%0]' AND '[%1]' UNION ALL SELECT T3."ItmsGrpNam", -T1."Quantity", -(T1."LineTotal" * (1 - T0."DiscPrcnt" / 100.0)) FROM ORIN T0 INNER JOIN RIN1 T1 ON T0."DocEntry" = T1."DocEntry" INNER JOIN OITM T2 ON T1."ItemCode" = T2."ItemCode" INNER JOIN OITB T3 ON T2."ItmsGrpCod" = T3."ItmsGrpCod" WHERE T0."CANCELED" = 'N' AND T0."DocDate" BETWEEN '[%0]' AND '[%1]' ) X GROUP BY X."ItmsGrpNam" ORDER BY "NetSales" DESC
What this query answers
The sales manager wants to see which product categories carry the business this quarter. This returns one row per item group with the net quantity and net sales after the invoice header discount, with credit memos subtracted, so a group that sold a lot but took many returns does not look better than it is.
Sample output
Illustrative demo values. Your columns are the same; the numbers come from your company.
| ItmsGrpNam | Quantity | NetSales |
|---|---|---|
| Printers | 412 | 96400 |
| Consumables | 2380 | 41250.5 |
| Accessories | 905 | 18730 |
| Services | 64 | 6400 |
SQL Server vs SAP HANA
The UNION ALL and the joins stay the same. HANA needs every column and alias in double quotes, for example T3."ItmsGrpNam" and AS "NetSales", and the decimal 100.0 keeps the discount division from turning into integer math.
Common errors and fixes
- The total is higher than the sales analysis report: LineTotal is before the header discount. The query multiplies it by (1 - DiscPrcnt / 100) on both halves; keep that factor if you change the select list.
- Service invoices are missing: Service-type invoices have no ItemCode, so the inner join to OITM drops them. They have no item group, which is why they cannot be grouped here.
- Cancelled invoices change the totals: Keep CANCELED = 'N' on both halves. The cancellation document carries CANCELED = 'C' and is excluded the same way.
Need this report built into SAP Business One?
A saved query answers the question once. We do custom SAP Business One development: add-ons, integrations and reports, from the database to the screen. Or hire a 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)) · ORIN table (A/R Credit Memo (header)) · RIN1 table (A/R Credit Memo (lines)) · OITM table (Items (item master data)) · OITB table (Item Groups)
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 item group 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, ORIN, RIN1, OITM, OITB 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.
