Start a project

Top 20 selling items

Best-selling items by sales value, with quantity sold, for a date range.

Tables: OINV, INV1Use: Purchasing planning and product focus.Parameters: [%0] = from date, [%1] = to date

Tool

SELECT TOP 20 T1.ItemCode, T1.Dscription AS ItemName,
  SUM(T1.Quantity) AS QtySold, SUM(T1.LineTotal * (1 - T0.DiscPrcnt / 100.0)) AS SalesValue
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 T1.ItemCode, T1.Dscription
ORDER BY SalesValue DESC
Need your UDFs in this report, or a dashboard? We extend it for your company.Customize it →

What this query answers

The purchasing team is planning next quarter's buying and wants to know which 20 items brought in the most revenue last period, and how many units that took. It answers which products deserve stock priority and which are just noise.

Sample output

Illustrative demo values. Your columns are the same; the numbers come from your company.

ItemCodeItemNameQtySoldSalesValue
A00001Laser Printer 14204225200
A00005Toner Cartridge Black31013950
A00012Wireless Keyboard1857400
A00007Paper A4 Box5205720

SQL Server vs SAP HANA

SELECT TOP 20 is kept as it is, because SAP HANA also accepts TOP n (LIMIT 20 at the end would work too). Everything else is identifier quoting, including the alias "SalesValue" used in ORDER BY.

Common errors and fixes

  • Invalid column name 'Description': The line description column in SAP B1 is spelled Dscription (no 'e'). Use T1.Dscription in both SELECT and GROUP BY.
  • The same item appears twice in the top 20: The query groups by the line description, so an item whose name was edited on some invoices splits into two rows. Group by T1.ItemCode only and join OITM for the current ItemName.
  • Returned goods are not deducted: Only A/R invoices (INV1) are read. Add a UNION ALL of RIN1/ORIN with negative Quantity and LineTotal if returns are significant for your business.
  • Totals don't match the Sales Analysis report (header discount): A discount entered on the document header (OINV.DiscPrcnt) is not inside INV1.LineTotal, so a plain SUM(T1.LineTotal) overstates sales. The query above applies it: SUM(T1.LineTotal * (1 - T0.DiscPrcnt / 100.0)) (HANA: SUM(T1."LineTotal" * (1 - T0."DiscPrcnt" / 100.0))). Credit memos (ORIN/RIN1) must be subtracted with the same factor, using ORIN.DiscPrcnt. 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 SAP Business One developer for the whole job, fixed price per phase.

Sales

Related queries

Table reference: OINV table (A/R Invoice (header)) · INV1 table (A/R Invoice (lines))

How it works

Three steps, no signup

1

Copy the right version

Use the SQL Server tab for MS SQL databases and the HANA tab for SAP B1 on HANA.

2

Open Query Generator

In SAP Business One go to Tools → Queries → Query Generator, paste and click Execute.

3

Save and share

Click Save, pick a category and give the query a name. Your team can run it from Query Manager.

FAQ

Questions about this tool

How do I run the top 20 selling items 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.

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.