SAP Business One Discount Analysis - tutorial featured image

Publications

SAP Business One Discount Analysis: SQL Query to Stop Margin Leakage

Hector Pincheira

Hector Pincheira

CIO | CTO
Digital Strategy & Technology Innovation

items

There is a money leak that almost never shows up in the income statement under its real name: the discount. It is granted in seconds, justified with a «we had to close the deal» and dissolves into the margin without leaving a visible trace. That is why SAP Business One Discount Analysis with SQL queries is one of the most profitable tools a manager can ask for: it turns an invisible concession into a number you can manage. In this tutorial you will build, step by step, the queries that reveal how much margin you are giving away, who is giving it away and to which customers.

This is not theory. This is the order that went out with a 12% «volume» discount when the volume never arrived, the sales rep who discounts out of habit rather than strategy, and the customer who negotiated a break three years ago that is still in force today. All of it is recorded in your database. It just needs to be looked at.

The real problem: margin is lost in the details

When a company reviews its profitability, it looks at consolidated gross margin. That average number, however, hides enormous dispersion. Two invoices for the same product can carry radically different margins for one single reason: the discount applied on the line. The problem is not that you discount, but that you discount blind.

In SAP Business One every discount is stored at two levels. First there is the line discount, in the DiscPrcnt from the table INV1, which compares the original price (PriceBefDi) with the final price. Then there is the document discount, in the DiscSum field of the OINV. Many reports ignore one of the two and, as a result, understate the total leak. Here we will measure them together.

Why you need to measure your discounts

A percentage point of discount is not equal to a point of margin: it is worth far more. If your gross margin is 25% and you grant an extra 5% discount, you do not lose 5% of profit, you lose close to 20% of it. The discount is therefore the most sensitive lever in the business and, paradoxically, the one with the least formal control.

Measuring discounts with data also lets you change conversations. Instead of telling a sales rep «you discount too much», you show him that his average discount is 9% against the team's 4%, and that this represents a concrete amount of margin lost in the quarter. The discussion stops being an opinion and becomes a manageable fact.

SAP Business One discount analysis - review of financial documents and margin
Every discount granted is margin leaving the business: measuring it is the first step to controlling it.

Prerequisites before starting

Running these queries takes few elements, but they matter. First, you need access to the Query Generator (Query Manager) under Tools > Queries, or read-only permissions on the database via SQL. Second, you should know whether your installation runs on SQL Server the envelope SAP HANA, because the syntax changes and here we give you both versions. Finally, define the period you want to analyze; a quarter is usually enough to spot patterns without drowning in data.

One piece of advice up front: always run these queries first in a test environment or with tight date filters. That keeps you from loading the production server with heavy reads during operating hours.

Step-by-step tutorial: SAP Business One discount analysis

We are going to build three queries in increasing order of depth. The first gives you the general picture by document. The second assigns responsibility by sales rep. The third connects the discount with real margin by customer. Together they form a simple but devastating control panel.

Step 1: Total discount per invoice

This query calculates, for each sales invoice, the gross amount (before discounts), the line discount, the document discount and the total percentage discounted. That way you immediately identify transactions with out-of-range reductions.

SQL Server

-- Total discount per sales invoice
SELECT
    T0.DocNum        AS 'Invoice',
    T0.DocDate       AS 'Date',
    T0.CardName      AS 'Customer',
    SUM(T1.Quantity * T1.PriceBefDi)                         AS 'Gross Amount',
    SUM(T1.Quantity * T1.PriceBefDi) - SUM(T1.LineTotal)     AS 'Line Disc.',
    T0.DiscSum                                               AS 'Document Disc.',
    CAST(100.0 * ((SUM(T1.Quantity * T1.PriceBefDi) - SUM(T1.LineTotal)) + T0.DiscSum)
        / NULLIF(SUM(T1.Quantity * T1.PriceBefDi), 0) AS DECIMAL(10,2)) AS 'Total Disc. %'
FROM OINV T0
INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry
WHERE T0.DocDate BETWEEN '20260101' AND '20260331'
GROUP BY T0.DocNum, T0.DocDate, T0.CardName, T0.DiscSum
ORDER BY 'Total Disc. %' DESC;

SAP HANA

-- Total discount per sales invoice
SELECT
    T0."DocNum"   AS "Invoice",
    T0."DocDate"  AS "Date",
    T0."CardName" AS "Customer",
    SUM(T1."Quantity" * T1."PriceBefDi")                          AS "Gross Amount",
    SUM(T1."Quantity" * T1."PriceBefDi") - SUM(T1."LineTotal")    AS "Line Disc",
    T0."DiscSum"                                                  AS "Document Disc",
    CAST(100.0 * ((SUM(T1."Quantity" * T1."PriceBefDi") - SUM(T1."LineTotal")) + T0."DiscSum")
        / (CASE WHEN SUM(T1."Quantity" * T1."PriceBefDi") = 0 THEN NULL
                ELSE SUM(T1."Quantity" * T1."PriceBefDi") END) AS DECIMAL(10,2)) AS "Total Disc Pct"
FROM OINV T0
INNER JOIN INV1 T1 ON T0."DocEntry" = T1."DocEntry"
WHERE T0."DocDate" BETWEEN '2026-01-01' AND '2026-03-31'
GROUP BY T0."DocNum", T0."DocDate", T0."CardName", T0."DiscSum"
ORDER BY "Total Disc Pct" DESC;

The key is the subtraction PriceBefDi - Price, expressed here as the difference between the gross amount and the Line Total. We also add the DiscSum of the header so the discount applied to the whole document is not left out. As a result, the final percentage reflects the real reduction and not just part of it.

Step 2: Average discount by sales rep

Now we move up a level. This query groups the discount granted by each sales rep and calculates their weighted average percentage. It stops talking about isolated invoices and starts showing behavior. The rep at the top is not necessarily the worst; sometimes he serves the toughest customers. Still, the number opens the right conversation.

SQL Server

-- Discount granted by sales rep
SELECT
    T2.SlpName                                              AS 'Sales Rep',
    SUM(T1.Quantity * T1.PriceBefDi)                        AS 'Gross Sales',
    SUM(T1.Quantity * T1.PriceBefDi) - SUM(T1.LineTotal)    AS 'Discount Granted',
    CAST(100.0 * (SUM(T1.Quantity * T1.PriceBefDi) - SUM(T1.LineTotal))
        / NULLIF(SUM(T1.Quantity * T1.PriceBefDi), 0) AS DECIMAL(10,2)) AS 'Avg. Disc. %'
FROM OINV T0
INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry
INNER JOIN OSLP T2 ON T0.SlpCode = T2.SlpCode
WHERE T0.DocDate BETWEEN '20260101' AND '20260331'
GROUP BY T2.SlpName
ORDER BY 'Discount Granted' DESC;

SAP HANA

-- Discount granted by sales rep
SELECT
    T2."SlpName"                                                AS "Sales Rep",
    SUM(T1."Quantity" * T1."PriceBefDi")                        AS "Gross Sales",
    SUM(T1."Quantity" * T1."PriceBefDi") - SUM(T1."LineTotal")  AS "Discount Granted",
    CAST(100.0 * (SUM(T1."Quantity" * T1."PriceBefDi") - SUM(T1."LineTotal"))
        / (CASE WHEN SUM(T1."Quantity" * T1."PriceBefDi") = 0 THEN NULL
                ELSE SUM(T1."Quantity" * T1."PriceBefDi") END) AS DECIMAL(10,2)) AS "Avg Disc Pct"
FROM OINV T0
INNER JOIN INV1 T1 ON T0."DocEntry" = T1."DocEntry"
INNER JOIN OSLP T2 ON T0."SlpCode" = T2."SlpCode"
WHERE T0."DocDate" BETWEEN '2026-01-01' AND '2026-03-31'
GROUP BY T2."SlpName"
ORDER BY "Discount Granted" DESC;

Notice the INNER JOIN with the OSLP, which translates the sales rep code (SlpCode) into a name. We then sort by the absolute discount amount and not just by percentage, because 3% on a large customer can weigh more than 15% on a small one.

SAP Business One discount analysis - data dashboard and sales reports
Discount by sales rep stops being a perception and becomes a comparable indicator.

Step 3: Discount against real margin by customer

Here we get to the heart of the matter. A discount is only dangerous when it erodes margin below an acceptable threshold. That is why this third query crosses the discount with product cost (GrossBuyPr) and calculates the net margin left after the reduction. That is how you spot the customer who buys a lot but leaves little.

SQL Server

-- Discount vs. real margin by customer
SELECT
    T0.CardCode                                            AS 'Code',
    T0.CardName                                            AS 'Customer',
    SUM(T1.Quantity * T1.PriceBefDi)                       AS 'Gross',
    SUM(T1.Quantity * T1.PriceBefDi) - SUM(T1.LineTotal)   AS 'Discount',
    SUM(T1.LineTotal)                                      AS 'Net Sales',
    SUM(T1.LineTotal - (T1.Quantity * T1.GrossBuyPr))      AS 'Margin',
    CAST(100.0 * SUM(T1.LineTotal - (T1.Quantity * T1.GrossBuyPr))
        / NULLIF(SUM(T1.LineTotal), 0) AS DECIMAL(10,2))   AS 'Margin %'
FROM OINV T0
INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry
WHERE T0.DocDate BETWEEN '20260101' AND '20260331'
GROUP BY T0.CardCode, T0.CardName
HAVING SUM(T1.Quantity * T1.PriceBefDi) - SUM(T1.LineTotal) > 0
ORDER BY 'Discount' DESC;

SAP HANA

-- Discount vs. real margin by customer
SELECT
    T0."CardCode"                                              AS "Code",
    T0."CardName"                                              AS "Customer",
    SUM(T1."Quantity" * T1."PriceBefDi")                       AS "Gross",
    SUM(T1."Quantity" * T1."PriceBefDi") - SUM(T1."LineTotal") AS "Discount",
    SUM(T1."LineTotal")                                        AS "Net Sales",
    SUM(T1."LineTotal" - (T1."Quantity" * T1."GrossBuyPr"))    AS "Margin",
    CAST(100.0 * SUM(T1."LineTotal" - (T1."Quantity" * T1."GrossBuyPr"))
        / (CASE WHEN SUM(T1."LineTotal") = 0 THEN NULL
                ELSE SUM(T1."LineTotal") END) AS DECIMAL(10,2)) AS "Margin Pct"
FROM OINV T0
INNER JOIN INV1 T1 ON T0."DocEntry" = T1."DocEntry"
WHERE T0."DocDate" BETWEEN '2026-01-01' AND '2026-03-31'
GROUP BY T0."CardCode", T0."CardName"
HAVING SUM(T1."Quantity" * T1."PriceBefDi") - SUM(T1."LineTotal") > 0
ORDER BY "Discount" DESC;

The field GrossBuyPr stores the purchase cost of each line at the moment of the transaction, so the calculated margin is historical and faithful. If your operation values inventory at average cost, though, consider contrasting this result with the accounting valuation to avoid surprises.

Variations and use cases

The three queries above are the base, but the real value appears when you adapt them to your reality. Below are some variants that tend to deliver immediate findings.

You can filter by product line by adding a Join a OITM and grouping by ItmsGrpCod, so you discover which families concentrate the leak. You can also isolate discounts above a threshold, for example by adding Having with a minimum percentage, to review only the exceptions. Another powerful variant is to compare two periods and detect whether the average discount is growing, an early sign of commercial indiscipline.

A frequent use case is feeding a formatted search or an alert that warns when a document exceeds a certain discount before it is posted. That way the analysis stops being retrospective and becomes preventive, which is where it really protects the margin.

SAP Business One discount analysis - calculator and margin and profitability documents
Crossing discount with cost reveals the customer who invoices a lot and leaves little.

Best practices and common mistakes

The first mistake, and the most serious, is measuring only the line discount and ignoring the DiscSum of the header. As a consequence, the report shows a smaller leak than the real one and conveys false calm. Always add both levels.

The second mistake is confusing discount with low price. A product can have terrible margin without a single discount, simply because its price list is badly calibrated. So complement this analysis with a review of price lists; the visible discount is only part of the story.

As for good practices, define a discount threshold by segment and turn it into policy, not individual judgment. Also review this report on a fixed cadence, ideally monthly, because pricing discipline erodes slowly and recovers slowly. Finally, avoid WITH (NOLOCK) as a crutch in production; use it with judgment and only when you understand its dirty-read implications.

Official SAP Resources

To go deeper, check the official documentation. The SAP Help Portal for SAP Business One portal contains the complete functional reference, and the SAP Business One space in the SAP Community brings together query examples and technical discussions about the data model OINV / INV1. Both resources are the best compass when you need to confirm the exact name of a field before taking a query to production.

Conclusion: from blind discounting to managed margin

Measuring discounts is not a technical exercise, it is a leadership decision. When you turn an invisible concession into an indicator that gets reviewed, you change the entire commercial culture: the team stops discounting by reflex and starts discounting by strategy. That is the real return on these three queries.

The SAP Business One Discount Analysis is, at bottom, a map of where your profitability escapes. You have the data, you have the queries and you have the why. The next step is yours: run them this week, take the three numbers to your next commercial meeting and watch how the conversation changes.

Do you want the complete arsenal of production-ready SQL queries, with the variants by product line, thresholds and preventive alerts? Become part of the Strategic Circle at hectorpincheira.com and get access to the advanced tutorials with the complete code explained.

Most Read

QlikSense Hierarchy: parent-child chart of accounts expanded by levels
QlikSense Hierarchy: the Chart of Accounts That Doesn't Add Up
QlikSense Sum TOTAL: share of each family over the total
QlikSense Sum TOTAL: the Percentage That Changes When You Filter
SAP Business One Inventory Revaluation - featured image of the tutorial
SAP Business One Inventory Revaluation: Fix Wrongly Loaded Costs
Empty chamber with wooden benches: the counterweight that required being present to be used
The Veto You Had to Say in Person: Your Decisions With No Counterweight
SAP Business One customizations - featured image of the pre-upgrade audit tutorial
SAP Business One Customizations: Pre-Upgrade Audit
SAP Business One Customer Concentration - featured image of the tutorial
SAP Business One Customer Concentration: Measure Your Risk with SQL

RELATED ARTICLES