If your company operates with SAP Business One but you are not clear about which products generate the best profit margins, you are making commercial decisions with incomplete information. The profit margin analysis by product in SAP Business One it is not an accessory report: it is the basis for management to prioritize what to sell, how to negotiate prices, and which product lines need to be strategically readjusted.
In this tutorial, we will build SQL queries that extract the gross margin by item directly from SAP B1 tables, with complete versions for SQL Server and SAP HANA. The result is an actionable report that any analyst or manager can run from the Query Manager in minutes, without the need for additional developments.

Why product margin analysis is a strategic necessity
First of all, the reality for most companies implementing SAP B1 is that they accumulate sales data for years but do not transform it into profitability intelligence. As a result, the sales force tends to sell what is easiest to place or what the customer asks for, not necessarily what generates the highest profit for the company.
In addition, SAP Business One allows you to manage multiple price lists, customer discounts, special conditions, and negotiation agreements. Therefore, the actual margin of the same product can vary enormously depending on the sales channel or the specific client. Without a query that captures the effective net income—the field Line Total after all discounts—any analysis based solely on the price list will be structurally incorrect.
With this analysis properly implemented, you will be able to answer questions that truly move the needle for the business: What are the top 20 products with the highest absolute gross margin of the year? Which product groups have margins below the minimum profitability threshold? Has your flagship product's margin dropped over the last six months? Which salesperson is eroding the margin with excessive discounts?
Prerequisites before implementing these queries
Before starting, verify the following elements in your SAP B1 instance:
- SAP Business One Version9.x or higher, including SAP B1 Cloud version 10.x
- Access to Query ManagerAdministration → Queries → User Queries
- Active costing methodYou need to know if your company uses Weighted Average Cost, Standard Cost, or FIFO. This determines which cost field to use in the OITM table.
- User permissionsReading on the Sales, Inventory, and Finance modules
An important technical detail: these queries use the field Average Price from the table OITM As a cost reference. This field corresponds to updated weighted average cost of the article. If your company operates with standard costing, replace Average Price by Standard Price in all queries. For the FIFO method, the historical cost calculation is more complex and requires consulting the layer history in the ITL and OINM tables. Consult with your SAP B1 consultant which method is configured before implementing.
How margin calculation works in SAP Business One
Here is the calculation logic that we will implement. SAP B1 records each sales invoice in the table OINV (document header) and the detail lines in the table INV1. The field Line Total of INV1 represents the net income per line, already incorporating the discounts applied to the document and to the specific line.
Gross Margin = LineTotal (Net Sales) − (Quantity × AvgPrice)
% Margin = (Gross Margin ÷ LineTotal) × 100
However, there is one aspect that many implementations omit: customer credit notes—recorded in the table ORIN, with lines in RIN1— reduce actual sales. For companies with significant return rates, the basic query can overestimate margins. The advanced variation we include later correctly discounts these returns.

Step-by-step tutorial: implement the query in SAP B1
Thus, we proceed with the implementation. Follow these steps to add the queries to the system:
- Access Administration → Queries → User Queries and click on New
- Write a descriptive name: for example, Product Margins – Selected Period
- Assign the corresponding accounting or commercial category in your organization
- Paste the complete SQL code into the query field
- When running, the system will prompt for the parameters: [%0] Start Date and [%1] End Date in YYYY-MM-DD format
- Save and grant access to users who need to run it from their profiles
SAP Business One profit margins by product
This is the base query. It shows the gross margin, total cost, net sales, and margin percentage for each item, ordered from highest to lowest absolute profitability. It is the ideal starting point for any commercial review.
SQL Server
-- Profit Margins by Product | SAP B1 SQL Server
-- [%0]: Start Date | [%1]: End Date (format YYYY-MM-DD)
SELECT
T1.[ItemCode] AS [Code],
T1.[Description] AS [Description],
T2.[ItmsGrpNam] AS [Item Group],
SUM(T1.[Quantity]) AS [Units Sold],
ROUND(AVG(T1.[Price]), 2) AS [Average Price],
ROUND(AVG(T0.[AvgPrice]), 2) AS [Average Cost],
ROUND(SUM(T1.[LineTotal]), 2) AS [Net Sales],
ROUND(SUM(T1.[Quantity] * T0.[AvgPrice]), 2) AS [Total Cost],
ROUND(SUM(T1.[LineTotal])
- SUM(T1.[Quantity] * T0.[AvgPrice]), 2) AS [Gross Margin],
CASE
WHEN SUM(T1.[LineTotal]) = 0 THEN 0
ELSE ROUND(
(SUM(T1.[LineTotal]) - SUM(T1.[Quantity] * T0.[AvgPrice]))
/ SUM(T1.[LineTotal]) * 100, 2)
END AS [% Margin]
FROM OINV T3
INNER JOIN INV1 T1 ON T1.[DocEntry] = T3.[DocEntry]
INNER JOIN OITM T0 ON T0.[ItemCode] = T1.[ItemCode]
INNER JOIN OITB T2 ON T2.[ItmsGrpCod] = T0.[ItmsGrpCod]
WHERE (T3.[DocStatus] = 'O' OR T3.[DocStatus] = 'C')
AND T3.[CANCELED] = 'N'
AND T3.[DocDate] BETWEEN '[%0]' AND '[%1]'
GROUP BY T1.[ItemCode], T1.[Description], T2.[ItemsGroupName]
ORDER BY SUM(T1.[LineTotal]) - SUM(T1.[Quantity] * T0.[AvgPrice]) DESCSAP HANA
-- Profit Margins by Product | SAP B1 HANA
-- [%0]: Start Date | [%1]: End Date
SELECT
T1."ItemCode",
T1."Description" AS "Description",
T2."ItmsGrpNam" AS "Item Group",
SUM(T1."Quantity") AS "Units Sold",
ROUND(AVG(T1."Price"), 2) AS "Average Price",
ROUND(AVG(T0."AvgPrice"), 2) AS "Average Cost",
ROUND(SUM(T1."LineTotal"), 2) AS "Net Sales",
ROUND(SUM(T1."Quantity" * T0."AvgPrice"), 2) AS "Total Cost",
ROUND(SUM(T1."LineTotal")
- SUM(T1."Quantity" * T0."AvgPrice"), 2) AS "Gross Margin",
CASE
WHEN SUM(T1."LineTotal") = 0 THEN 0
ELSE ROUND(
(SUM(T1."LineTotal") - SUM(T1."Quantity" * T0."AvgPrice"))
/ SUM(T1."LineTotal") * 100, 2)
END AS "Margin Percentage"
FROM OINV T3
INNER JOIN INV1 T1 ON T1."DocEntry" = T3."DocEntry"
INNER JOIN OITM T0 ON T0."ItemCode" = T1."ItemCode"
INNER JOIN OITB T2 ON T2."ItmsGrpCod" = T0."ItmsGrpCod"
WHERE (T3."DocStatus" = 'O' OR T3."DocStatus" = 'C')
AND T3."CANCELED" = 'N'
AND T3."DocDate" BETWEEN '[%0]' AND '[%1]'
GROUP BY T1."ItemCode", T1."Description", T2."ItemsGrpName"
ORDER BY SUM(T1."LineTotal") - SUM(T1."Quantity" * T0."AvgPrice") DESCAdvanced variations according to the analysis context
However, the main query can be enriched with variations depending on the type of decision you need to make. Below are three high-value variations for different business situations.
Variation 1: Top 20 most profitable products of the current year
This variation is especially useful for executive reports and business reviews. It automatically shows the 20 products with the highest absolute gross margin of the current year, without the need to enter date parameters.
SQL Server
-- Top 20 Products by Gross Margin - Current Year | SQL Server
SELECT TOP 20
T1.[ItemCode] AS [Code],
T1.[Description] AS [Description],
T2.[ItmsGrpNam] AS [Group],
ROUND(SUM(T1.[LineTotal]), 2) AS [Net Sales],
ROUND(SUM(T1.[Quantity] * T0.[AvgPrice]), 2) AS [Total Cost],
ROUND(SUM(T1.[LineTotal])
- SUM(T1.[Quantity] * T0.[AvgPrice]), 2) AS [Gross Margin],
CASE
WHEN SUM(T1.[LineTotal]) = 0 THEN 0
ELSE ROUND(
(SUM(T1.[LineTotal]) - SUM(T1.[Quantity] * T0.[AvgPrice]))
/ SUM(T1.[LineTotal]) * 100, 2)
END AS [% Margin]
FROM OINV T3
INNER JOIN INV1 T1 ON T1.[DocEntry] = T3.[DocEntry]
INNER JOIN OITM T0 ON T0.[ItemCode] = T1.[ItemCode]
INNER JOIN OITB T2 ON T2.[ItmsGrpCod] = T0.[ItmsGrpCod]
WHERE T3.[CANCELED] = 'N'
AND YEAR(T3.[DocDate]) = YEAR(GETDATE())
GROUP BY T1.[ItemCode], T1.[Description], T2.[ItemsGroupName]
ORDER BY SUM(T1.[LineTotal]) - SUM(T1.[Quantity] * T0.[AvgPrice]) DESCSAP HANA
-- Top 20 Products by Gross Margin - Current Year | SAP HANA
SELECT
T1."ItemCode",
T1."Description" AS "Description",
T2."ItmsGrpNam" AS "Group",
ROUND(SUM(T1."LineTotal"), 2) AS "Net Sales",
ROUND(SUM(T1."Quantity" * T0."AvgPrice"), 2) AS "Total Cost",
ROUND(SUM(T1."LineTotal")
- SUM(T1."Quantity" * T0."AvgPrice"), 2) AS "Gross Margin",
CASE
WHEN SUM(T1."LineTotal") = 0 THEN 0
ELSE ROUND(
(SUM(T1."LineTotal") - SUM(T1."Quantity" * T0."AvgPrice"))
/ SUM(T1."LineTotal") * 100, 2)
END AS "Margin Percentage"
FROM OINV T3
INNER JOIN INV1 T1 ON T1."DocEntry" = T3."DocEntry"
INNER JOIN OITM T0 ON T0."ItemCode" = T1."ItemCode"
INNER JOIN OITB T2 ON T2."ItmsGrpCod" = T0."ItmsGrpCod"
WHERE T3."CANCELED" = 'N'
AND YEAR(T3."DocDate") = YEAR(CURRENT_DATE)
GROUP BY T1."ItemCode", T1."Description", T2."ItemsGroupName"
ORDER BY SUM(T1."LineTotal") - SUM(T1."Quantity" * T0."AvgPrice") DESC
LIMIT 20Variation 2: Margin by item group (portfolio view)
Consequently, if what you need is a strategic portfolio view, this variation groups the results by product category. It allows you to identify which business lines are structurally more profitable and which ones need to be reviewed in terms of pricing or discontinued.
SQL Server
-- Gross Margin by Item Group | SQL Server
-- [%0]: Start Date | [%1]: End Date
SELECT
T2.[ItmsGrpNam] AS [Item Group],
COUNT(DISTINCT T1.[ItemCode]) AS [Active Products],
ROUND(SUM(T1.[LineTotal]), 2) AS [Net Sales],
ROUND(SUM(T1.[Quantity] * T0.[AvgPrice]), 2) AS [Total Cost],
ROUND(SUM(T1.[LineTotal])
- SUM(T1.[Quantity] * T0.[AvgPrice]), 2) AS [Gross Margin],
CASE
WHEN SUM(T1.[LineTotal]) = 0 THEN 0
ELSE ROUND(
(SUM(T1.[LineTotal]) - SUM(T1.[Quantity] * T0.[AvgPrice]))
/ SUM(T1.[LineTotal]) * 100, 2)
END AS [% Group Margin]
FROM OINV T3
INNER JOIN INV1 T1 ON T1.[DocEntry] = T3.[DocEntry]
INNER JOIN OITM T0 ON T0.[ItemCode] = T1.[ItemCode]
INNER JOIN OITB T2 ON T2.[ItmsGrpCod] = T0.[ItmsGrpCod]
WHERE T3.[CANCELED] = 'N'
AND T3.[DocDate] BETWEEN '[%0]' AND '[%1]'
GROUP BY T2.[ItemsGroupName]
ORDER BY SUM(T1.[LineTotal]) - SUM(T1.[Quantity] * T0.[AvgPrice]) DESCSAP HANA
-- Gross Margin by Item Group | SAP HANA
-- [%0]: Start Date | [%1]: End Date
SELECT
T2."ItmsGrpNam" AS "Item Group",
COUNT(DISTINCT T1."ItemCode") AS "Active Products",
ROUND(SUM(T1."LineTotal"), 2) AS "Net Sales",
ROUND(SUM(T1."Quantity" * T0."AvgPrice"), 2) AS "Total Cost",
ROUND(SUM(T1."LineTotal")
- SUM(T1."Quantity" * T0."AvgPrice"), 2) AS "Gross Margin",
CASE
WHEN SUM(T1."LineTotal") = 0 THEN 0
ELSE ROUND(
(SUM(T1."LineTotal") - SUM(T1."Quantity" * T0."AvgPrice"))
/ SUM(T1."LineTotal") * 100, 2)
END AS "Group Margin Percentage"
FROM OINV T3
INNER JOIN INV1 T1 ON T1."DocEntry" = T3."DocEntry"
INNER JOIN OITM T0 ON T0."ItemCode" = T1."ItemCode"
INNER JOIN OITB T2 ON T2."ItmsGrpCod" = T0."ItmsGrpCod"
WHERE T3."CANCELED" = 'N'
AND T3."DocDate" BETWEEN '[%0]' AND '[%1]'
GROUP BY T2."ItmsGrpNam"
ORDER BY SUM(T1."LineTotal") - SUM(T1."Quantity" * T0."AvgPrice") DESCVariation 3: Margin by product broken down by salesperson
For example, if you suspect that the sales team is applying discounts that erode the margin, this variation confirms it with data. It shows the actual margin by product-salesperson combination, revealing exactly where profitability is being lost.
SQL Server
-- Margin by Product and Salesperson | SQL Server
-- [%0]: Start Date | [%1]: End Date
SELECT
T4.[SlpName] AS [Salesperson],
T1.[ItemCode] AS [Code],
T1.[Description] AS [Description],
SUM(T1.[Quantity]) AS [Units Sold],
ROUND(SUM(T1.[LineTotal]), 2) AS [Net Sales],
ROUND(SUM(T1.[Quantity] * T0.[AvgPrice]), 2) AS [Total Cost],
ROUND(SUM(T1.[LineTotal])
- SUM(T1.[Quantity] * T0.[AvgPrice]), 2) AS [Gross Margin],
CASE
WHEN SUM(T1.[LineTotal]) = 0 THEN 0
ELSE ROUND(
(SUM(T1.[LineTotal]) - SUM(T1.[Quantity] * T0.[AvgPrice]))
/ SUM(T1.[LineTotal]) * 100, 2)
END AS [% Margin]
FROM OINV T3
INNER JOIN INV1 T1 ON T1.[DocEntry] = T3.[DocEntry]
INNER JOIN OITM T0 ON T0.[ItemCode] = T1.[ItemCode]
INNER JOIN OITB T2 ON T2.[ItmsGrpCod] = T0.[ItmsGrpCod]
INNER JOIN OSLP T4 ON T4.[SlpCode] = T3.[SlpCode]
WHERE T3.[CANCELED] = 'N'
AND T3.[DocDate] BETWEEN '[%0]' AND '[%1]'
GROUP BY T4.[SlpName], T1.[ItemCode], T1.[Dscription]
ORDER BY T4.[SlpName], SUM(T1.[LineTotal]) - SUM(T1.[Quantity] * T0.[AvgPrice]) DESCSAP HANA
-- Margin by Product and Salesperson | SAP HANA
-- [%0]: Start Date | [%1]: End Date
SELECT
T4."SlpName" AS "Seller",
T1."ItemCode",
T1."Description" AS "Description",
SUM(T1."Quantity") AS "Units Sold",
ROUND(SUM(T1."LineTotal"), 2) AS "Net Sales",
ROUND(SUM(T1."Quantity" * T0."AvgPrice"), 2) AS "Total Cost",
ROUND(SUM(T1."LineTotal")
- SUM(T1."Quantity" * T0."AvgPrice"), 2) AS "Gross Margin",
CASE
WHEN SUM(T1."LineTotal") = 0 THEN 0
ELSE ROUND(
(SUM(T1."LineTotal") - SUM(T1."Quantity" * T0."AvgPrice"))
/ SUM(T1."LineTotal") * 100, 2)
END AS "Margin Percentage"
FROM OINV T3
INNER JOIN INV1 T1 ON T1."DocEntry" = T3."DocEntry"
INNER JOIN OITM T0 ON T0."ItemCode" = T1."ItemCode"
INNER JOIN OITB T2 ON T2."ItmsGrpCod" = T0."ItmsGrpCod"
INNER JOIN OSLP T4 ON T4."SlpCode" = T3."SlpCode"
WHERE T3."CANCELED" = 'N'
AND T3."DocDate" BETWEEN '[%0]' AND '[%1]'
GROUP BY T4."SlpName", T1."ItemCode", T1."Description"
ORDER BY T4."SlpName", SUM(T1."LineTotal") - SUM(T1."Quantity" * T0."AvgPrice") DESC
Best practices and common mistakes when analyzing margins in SAP B1
Common Error 1: Not filtering canceled invoices
The field CANCELED = 'N' It is critical in all these queries. Without this filter, canceled documents in SAP B1 are included in the calculation and completely distort the results. However, many implementations only filter by Document Status, which is not enough: the CANCELED field is the definitive cancellation indicator and must always be included in the WHERE.
Common error 2: Mixing current cost with historical analysis
The field Average Price from the OITM table is the average cost current of the item, not necessarily the one it had when each sale was made. Consequently, for analysis of long historical periods—especially in industrial companies with significant fluctuations in raw material costs—there may be a difference between the reported margin and the actual margin of the period. For commercial management and current pricing decisions, it is the correct value. For forensic analyses of historical profitability, consult the cost history in the ITL table alongside OINM.
Common error 3: Ignoring credit notes
If your company has a return rate exceeding 3% of sales volume, failing to issue credit memos can significantly inflate margins. For example, a product with an apparent gross margin of 15% may actually have a negative margin if it has a high return rate. Always include the table ORIN and its lines RIN1 in the analysis when this is the case.
Best Practice: Define a minimum margin threshold per product
Once you have the data, the most powerful exercise is to add an alert filter to the analysis: products with a margin below the minimum acceptable threshold defined by management—for example, less than 20%. In this way, the query becomes a tool for exception management, not just reporting.
To implement it in SQL Server, add to the end of the main query:
-- Add to the HAVING clause of the main query (SQL Server and HANA)
HAVING CASE
WHEN SUM(T1.[LineTotal]) = 0 THEN 0
ELSE (SUM(T1.[LineTotal]) - SUM(T1.[Quantity] * T0.[AvgPrice]))
/ SUM(T1.[LineTotal]) * 100
END < 20 -- Adjust this threshold according to your businessBest practice: Convert to Crystal Report or view for mass access
Once the queries are validated in the Query Manager, the natural step is to convert them into a Crystal Report linked to the SAP B1 menu, or to create an SQL view in the database and connect it directly to Excel or Power BI. In this way, product margin analysis stops being a technical task reserved for IT and becomes a management report available to the entire commercial organization.
Official SAP Business One resources
- SAP Business One Help Portal — Official Technical Documentation
- SAP Community — SAP B1 Technical Forum and Queries
- SAP Business One for Latin America
Conclusion: Margin is the truth of the business
Sales are just a number. Margin is the truth. You can close the month with record revenue and still fail to generate value if margins are not where they need to be. SAP Business One has all the data you need to know that truth with surgical precision: the problem is that this data remains dormant in the tables until someone activates it.
These queries give you the starting point for a product profit margin analysis in SAP Business One that truly impacts business decisions. The next step is to run them, review the results, and make at least one concrete commercial decision based on what you find: a product whose price goes up, a line that is discontinued, a salesperson whose discount limits are adjusted. That is what differentiates companies that use SAP as an operational tool from those that turn it into a real competitive advantage.
If you want to delve deeper into the profitability analysis, combine these results with the sales analysis by salesperson and the purchases analysis by supplier —also available in the Strategic Circle—. Together, they form a complete profitability management panel for general management.
Do you have questions about how to adapt these queries to your cost structure or your product catalog? Leave it in the comments and we'll solve it together.




