Every month, the same scene repeats in hundreds of companies. The accounting department downloads three or four reports from the ERP, pastes them into Excel, manually crosses formulas, and only then declares VAT. The process takes two days, depends on a single person, and no one can explain exactly where each figure came from. Therefore, the risk is not just operational: it is fiscal. A SAP Business One VAT report built directly on top of the database, it eliminates that fragility and turns the tax closing into a five-minute traceable process.
In this tutorial, you will build, step by step, a complete purchase and sales ledger with ready-to-execute SQL queries. In addition, each query comes in both SQL Server and SAP HANA versions, so that it works regardless of the platform your installation runs on.

Why your SAP Business One VAT report cannot live in Excel
When the purchase and sales ledger is compiled outside the ERP, the company loses three things at the same time. First, it loses traceability: no one can reconstruct how a total was reached without repeating the entire work. Second, it loses speed, because the closing depends on the availability of a specific person. Finally, it loses control, since any copying error propagates silently all the way to the tax return.
The financial consequence is clear. An incorrectly recorded tax credit means paying more tax. An underreported tax liability means risking fines and interest. For example, a discrepancy in the 1% tax credit for a company that purchases $500,000 monthly represents a loss of $950 annually in unrecovered VAT alone. Multiplied over several years, the cost of this disorganization far exceeds the cost of automating the reporting process.
Therefore, the correct question is not whether these queries are worth building. The question is how much longer you are going to fund the manual process.
Prerequisites before running the queries
Before writing the first query, make sure to meet these points. This way you will avoid incomplete results that are later hard to detect.
- Access to Query Manager of SAP Business One (Tools → Queries → Query Generator) or read permissions on the database.
- Tax groups correctly configured Go to Management → Definitions → Finance → Taxes → Tax groups.
- Tax ID uploaded in the business partner master (field License Trade Number in the OCRD table).
- Defined accounting period and month-end documents closed or at least stabilized.
- A read-only user to run reports. Never use the manager user for mass queries.
How VAT is stored in SAP Business One tables
Before consulting, it is helpful to understand the model. SAP Business One stores tax at the document line level, not just at the header level. Consequently, each row of an invoice carries its own tax group, rate, and calculated amount.
These are the tables you'll be using:
- OINV / INV1: sales invoices (header and lines).
- ORIN / RIN1sales credit memos.
- OPCH / PCH1purchase invoices.
- ORPC / RPC1purchase credit memos.
- OVTGdefinition of tax groups (code, name, category, and rate).
- OCRDbusiness partner master, where the tax ID lives.
The key fields in the line tables are VatGroup (tax group code), VAT Percentage (applied rate), VatSum (VAT amount of that line) and Line Total (line net). In addition, the field Canceled from the header allows you to exclude voided documents, a detail that many reports forget and that distorts the total.
Explore your tax groups first
Run this diagnostic query before any other. It will tell you exactly which rates you have active and how they are classified.
SQL Server
-- Diagnosis of configured tax groups
SELECT
T0.Code AS 'Code',
T0.Name AS 'Description',
CASE T0.Category
WHEN 'I' THEN 'Purchases (Tax Credit)'
WHEN 'O' THEN 'Sales (Tax Liability)'
ELSE 'Other'
END AS 'Category',
T0.Rate AS '% Rate'
FROM OVTG T0
ORDER BY T0.Category, T0.Code;
SAP HANA
-- Diagnostic of configured tax groups
SELECT
T0."Code" AS "Code",
T0."Name" AS "Description",
CASE T0."Category"
WHEN 'I' THEN 'Purchases (Tax Credit)'
WHEN 'O' THEN 'Sales (Tax Debit)'
ELSE 'Other'
END AS "Category",
T0."Rate" AS "Rate"
FROM OVTG T0
ORDER BY T0."Category", T0."Code";
If duplicate groups or obsolete rates appear, stop here. Correcting the parameterization before reporting is always cheaper than explaining differences afterwards.
sales ledger for the period
This query consolidates sales invoices and credit notes into a single result. In addition, it inverts the sign of the credit notes so that the total reflects the actual fiscal debit of the period.
SQL Server
-- Libro de Ventas: facturas + notas de credito
SELECT
V.DocDate AS 'Fecha',
V.DocNum AS 'Documento',
V.TipoDoc AS 'Tipo',
V.CardCode AS 'Cod Cliente',
V.CardName AS 'Razon Social',
ISNULL(C.LicTradNum, 'SIN ID') AS 'ID Fiscal',
V.VatGroup AS 'Grupo IVA',
V.VatPrcnt AS 'Tasa %',
SUM(V.Neto) AS 'Neto',
SUM(V.Iva) AS 'Debito Fiscal',
SUM(V.Neto + V.Iva) AS 'Total'
FROM (
SELECT T0.DocNum, T0.DocDate, T0.CardCode, T0.CardName,
'Factura' AS TipoDoc, T1.VatGroup, T1.VatPrcnt,
T1.LineTotal AS Neto, T1.VatSum AS Iva
FROM OINV T0
INNER JOIN INV1 T1 ON T1.DocEntry = T0.DocEntry
WHERE T0.CANCELED = 'N'
AND T0.DocDate BETWEEN '20260701' AND '20260731'
UNION ALL
SELECT T0.DocNum, T0.DocDate, T0.CardCode, T0.CardName,
'Nota de Credito' AS TipoDoc, T1.VatGroup, T1.VatPrcnt,
-T1.LineTotal, -T1.VatSum
FROM ORIN T0
INNER JOIN RIN1 T1 ON T1.DocEntry = T0.DocEntry
WHERE T0.CANCELED = 'N'
AND T0.DocDate BETWEEN '20260701' AND '20260731'
) Ventas V
LEFT JOIN OCRD C ON C.CardCode = V.CardCode
GROUP BY V.DocDate, V.DocNum, V.TipoDoc, V.CardCode,
V.CardName, C.LicTradNum, V.VatGroup, V.VatPrcnt
ORDER BY V.DocDate, V.DocNum;
SAP HANA
-- Libro de Ventas: facturas + notas de credito
SELECT
V."DocDate" AS "Fecha",
V."DocNum" AS "Documento",
V."TipoDoc" AS "Tipo",
V."CardCode" AS "Cod Cliente",
V."CardName" AS "Razon Social",
IFNULL(C."LicTradNum", 'SIN ID') AS "ID Fiscal",
V."VatGroup" AS "Grupo IVA",
V."VatPrcnt" AS "Tasa",
SUM(V."Neto") AS "Neto",
SUM(V."Iva") AS "Debito Fiscal",
SUM(V."Neto" + V."Iva") AS "Total"
FROM (
SELECT T0."DocNum", T0."DocDate", T0."CardCode", T0."CardName",
'Factura' AS "TipoDoc", T1."VatGroup", T1."VatPrcnt",
T1."LineTotal" AS "Neto", T1."VatSum" AS "Iva"
FROM OINV T0
INNER JOIN INV1 T1 ON T1."DocEntry" = T0."DocEntry"
WHERE T0."CANCELED" = 'N'
AND T0."DocDate" BETWEEN '2026-07-01' AND '2026-07-31'
UNION ALL
SELECT T0."DocNum", T0."DocDate", T0."CardCode", T0."CardName",
'Nota de Credito' AS "TipoDoc", T1."VatGroup", T1."VatPrcnt",
-T1."LineTotal", -T1."VatSum"
FROM ORIN T0
INNER JOIN RIN1 T1 ON T1."DocEntry" = T0."DocEntry"
WHERE T0."CANCELED" = 'N'
AND T0."DocDate" BETWEEN '2026-07-01' AND '2026-07-31'
) Ventas V
LEFT JOIN OCRD C ON C."CardCode" = V."CardCode"
GROUP BY V."DocDate", V."DocNum", V."TipoDoc", V."CardCode",
V."CardName", C."LicTradNum", V."VatGroup", V."VatPrcnt"
ORDER BY V."DocDate", V."DocNum";
Notice the important detail: the grouping includes the rate. Therefore, a document that mixes taxable and exempt lines appears in two rows, exactly as required by any serious tax ledger.
Purchase and Tax Credit Ledger
Logic is symmetrical, but it changes the set of tables. Furthermore, it is advisable to incorporate the date of the vendor's document here, since the tax credit is usually allocated based on that criterion and not on the entry date.
SQL Server
-- Libro de Compras: facturas + notas de credito de proveedor
SELECT
C.TaxDate AS 'Fecha Documento',
C.NumAtCard AS 'Folio Proveedor',
C.TipoDoc AS 'Tipo',
C.CardName AS 'Proveedor',
ISNULL(P.LicTradNum, 'SIN ID') AS 'ID Fiscal',
C.VatGroup AS 'Grupo IVA',
C.VatPrcnt AS 'Tasa %',
SUM(C.Neto) AS 'Neto',
SUM(C.Iva) AS 'Credito Fiscal',
SUM(C.Neto + C.Iva) AS 'Total'
FROM (
SELECT T0.DocNum, T0.DocDate, T0.TaxDate, T0.NumAtCard,
T0.CardCode, T0.CardName, 'Factura' AS TipoDoc,
T1.VatGroup, T1.VatPrcnt,
T1.LineTotal AS Neto, T1.VatSum AS Iva
FROM OPCH T0
INNER JOIN PCH1 T1 ON T1.DocEntry = T0.DocEntry
WHERE T0.CANCELED = 'N'
AND T0.TaxDate BETWEEN '20260701' AND '20260731'
UNION ALL
SELECT T0.DocNum, T0.DocDate, T0.TaxDate, T0.NumAtCard,
T0.CardCode, T0.CardName, 'Nota de Credito' AS TipoDoc,
T1.VatGroup, T1.VatPrcnt,
-T1.LineTotal, -T1.VatSum
FROM ORPC T0
INNER JOIN RPC1 T1 ON T1.DocEntry = T0.DocEntry
WHERE T0.CANCELED = 'N'
AND T0.TaxDate BETWEEN '20260701' AND '20260731'
) Compras C
LEFT JOIN OCRD P ON P.CardCode = C.CardCode
GROUP BY C.TaxDate, C.NumAtCard, C.TipoDoc, C.CardName,
P.LicTradNum, C.VatGroup, C.VatPrcnt
ORDER BY C.TaxDate, C.NumAtCard;
SAP HANA
-- Libro de Compras: facturas + notas de credito de proveedor
SELECT
C."TaxDate" AS "Fecha Documento",
C."NumAtCard" AS "Folio Proveedor",
C."TipoDoc" AS "Tipo",
C."CardName" AS "Proveedor",
IFNULL(P."LicTradNum", 'SIN ID') AS "ID Fiscal",
C."VatGroup" AS "Grupo IVA",
C."VatPrcnt" AS "Tasa",
SUM(C."Neto") AS "Neto",
SUM(C."Iva") AS "Credito Fiscal",
SUM(C."Neto" + C."Iva") AS "Total"
FROM (
SELECT T0."DocNum", T0."DocDate", T0."TaxDate", T0."NumAtCard",
T0."CardCode", T0."CardName", 'Factura' AS "TipoDoc",
T1."VatGroup", T1."VatPrcnt",
T1."LineTotal" AS "Neto", T1."VatSum" AS "Iva"
FROM OPCH T0
INNER JOIN PCH1 T1 ON T1."DocEntry" = T0."DocEntry"
WHERE T0."CANCELED" = 'N'
AND T0."TaxDate" BETWEEN '2026-07-01' AND '2026-07-31'
UNION ALL
SELECT T0."DocNum", T0."DocDate", T0."TaxDate", T0."NumAtCard",
T0."CardCode", T0."CardName", 'Nota de Credito' AS "TipoDoc",
T1."VatGroup", T1."VatPrcnt",
-T1."LineTotal", -T1."VatSum"
FROM ORPC T0
INNER JOIN RPC1 T1 ON T1."DocEntry" = T0."DocEntry"
WHERE T0."CANCELED" = 'N'
AND T0."TaxDate" BETWEEN '2026-07-01' AND '2026-07-31'
) Compras C
LEFT JOIN OCRD P ON P."CardCode" = C."CardCode"
GROUP BY C."TaxDate", C."NumAtCard", C."TipoDoc", C."CardName",
P."LicTradNum", C."VatGroup", C."VatPrcnt"
ORDER BY C."TaxDate", C."NumAtCard";
The field NumAtCard Save the supplier's document folio number. However, many companies leave it blank due to operational rush. If your result shows blank folios, you have already found your first audit finding.

Query 3: consolidated summary and net VAT position
The detail is useful for auditing. However, management needs a single line: how much is being paid this month. This query delivers the debit, credit, and net position in a compact result.
SQL Server
-- Resumen consolidado de IVA por tasa
SELECT
M.Concepto AS 'Concepto',
M.Tasa AS 'Tasa %',
SUM(M.Neto) AS 'Base Imponible',
SUM(M.Impuesto) AS 'Impuesto',
SUM(CASE WHEN M.Concepto = 'DEBITO'
THEN M.Impuesto ELSE -M.Impuesto END) AS 'Aporte a Posicion Neta'
FROM (
SELECT 'DEBITO' AS Concepto, T1.VatPrcnt AS Tasa,
T1.LineTotal AS Neto, T1.VatSum AS Impuesto
FROM OINV T0 INNER JOIN INV1 T1 ON T1.DocEntry = T0.DocEntry
WHERE T0.CANCELED = 'N' AND T0.DocDate BETWEEN '20260701' AND '20260731'
UNION ALL
SELECT 'DEBITO', T1.VatPrcnt, -T1.LineTotal, -T1.VatSum
FROM ORIN T0 INNER JOIN RIN1 T1 ON T1.DocEntry = T0.DocEntry
WHERE T0.CANCELED = 'N' AND T0.DocDate BETWEEN '20260701' AND '20260731'
UNION ALL
SELECT 'CREDITO', T1.VatPrcnt, T1.LineTotal, T1.VatSum
FROM OPCH T0 INNER JOIN PCH1 T1 ON T1.DocEntry = T0.DocEntry
WHERE T0.CANCELED = 'N' AND T0.TaxDate BETWEEN '20260701' AND '20260731'
UNION ALL
SELECT 'CREDITO', T1.VatPrcnt, -T1.LineTotal, -T1.VatSum
FROM ORPC T0 INNER JOIN RPC1 T1 ON T1.DocEntry = T0.DocEntry
WHERE T0.CANCELED = 'N' AND T0.TaxDate BETWEEN '20260701' AND '20260731'
) Movimientos M
GROUP BY M.Concepto, M.Tasa
ORDER BY M.Concepto DESC, M.Tasa;
SAP HANA
-- Resumen consolidado de IVA por tasa
SELECT
M."Concepto" AS "Concepto",
M."Tasa" AS "Tasa",
SUM(M."Neto") AS "Base Imponible",
SUM(M."Impuesto") AS "Impuesto",
SUM(CASE WHEN M."Concepto" = 'DEBITO'
THEN M."Impuesto" ELSE -M."Impuesto" END) AS "Aporte a Posicion Neta"
FROM (
SELECT 'DEBITO' AS "Concepto", T1."VatPrcnt" AS "Tasa",
T1."LineTotal" AS "Neto", T1."VatSum" AS "Impuesto"
FROM OINV T0 INNER JOIN INV1 T1 ON T1."DocEntry" = T0."DocEntry"
WHERE T0."CANCELED" = 'N' AND T0."DocDate" BETWEEN '2026-07-01' AND '2026-07-31'
UNION ALL
SELECT 'DEBITO', T1."VatPrcnt", -T1."LineTotal", -T1."VatSum"
FROM ORIN T0 INNER JOIN RIN1 T1 ON T1."DocEntry" = T0."DocEntry"
WHERE T0."CANCELED" = 'N' AND T0."DocDate" BETWEEN '2026-07-01' AND '2026-07-31'
UNION ALL
SELECT 'CREDITO', T1."VatPrcnt", T1."LineTotal", T1."VatSum"
FROM OPCH T0 INNER JOIN PCH1 T1 ON T1."DocEntry" = T0."DocEntry"
WHERE T0."CANCELED" = 'N' AND T0."TaxDate" BETWEEN '2026-07-01' AND '2026-07-31'
UNION ALL
SELECT 'CREDITO', T1."VatPrcnt", -T1."LineTotal", -T1."VatSum"
FROM ORPC T0 INNER JOIN RPC1 T1 ON T1."DocEntry" = T0."DocEntry"
WHERE T0."CANCELED" = 'N' AND T0."TaxDate" BETWEEN '2026-07-01' AND '2026-07-31'
) Movimientos M
GROUP BY M."Concepto", M."Tasa"
ORDER BY M."Concepto" DESC, M."Tasa";
The sum of the last column is your net position for the period. If the result is positive, you pay. If it is negative, you carry forward a tax credit balance. This way, management has the figure before the accountant finishes the closing.
tax inconsistency audit
This is the query that separates a report from a control system. It detects lines where the recorded VAT does not match what should result from applying the rate to the tax base. For example, poorly applied discounts, manual edits, or faulty migrations appear immediately.
SQL Server
-- Audit: lines with inconsistent VAT or members without a tax ID
SELECT
'Sale' AS 'Source',
T0.DocNum AS 'Document',
T0.DocDate AS 'Date',
T0.CardName AS 'Business Partner',
ISNULL(C.LicTradNum,'') AS 'Tax ID',
T1.LineNum + 1 AS 'Line',
T1.VatGroup AS 'VAT Group',
T1.VatPrcnt AS '% Rate',
T1.LineTotal AS 'Base',
T1.VatSum AS 'Recorded VAT',
ROUND(T1.LineTotal * T1.VatPrcnt / 100.0, 2) AS 'Expected VAT',
T1.VatSum - ROUND(T1.LineTotal * T1.VatPrcnt / 100.0, 2) AS 'Difference'
FROM OINV T0
INNER JOIN INV1 T1 ON T1.DocEntry = T0.DocEntry
LEFT JOIN OCRD C ON C.CardCode = T0.CardCode
WHERE T0.CANCELED = 'N'
AND T0.DocDate BETWEEN '20260701' AND '20260731'
AND (
ABS(T1.VatSum - ROUND(T1.LineTotal * T1.VatPrcnt / 100.0, 2)) > 1
OR C.LicTradNum IS NULL
OR T1.VatGroup IS NULL
)
ORDER BY ABS(T1.VatSum - ROUND(T1.LineTotal * T1.VatPrcnt / 100.0, 2)) DESC;
SAP HANA
-- Audit: lines with inconsistent VAT or a partner without a tax ID
SELECT
'Sale' AS "Source",
T0."DocNum" AS "Document",
T0."DocDate" AS "Date",
T0."CardName" AS "Business Partner",
IFNULL(C."LicTradNum", '') AS "Tax ID",
T1."LineNum" + 1 AS "Line",
T1."VatGroup" AS "VAT Group",
T1."VatPrcnt" AS "Rate",
T1."LineTotal" AS "Base Amount",
T1."VatSum" AS "Recorded VAT",
ROUND(T1."LineTotal" * T1."VatPrcnt" / 100.0, 2) AS "Expected VAT",
T1."VatSum" - ROUND(T1."LineTotal" * T1."VatPrcnt" / 100.0, 2) AS "Difference"
FROM OINV T0
INNER JOIN INV1 T1 ON T1."DocEntry" = T0."DocEntry"
LEFT JOIN OCRD C ON C."CardCode" = T0."CardCode"
WHERE T0."CANCELED" = 'N'
AND T0."DocDate" BETWEEN '2026-07-01' AND '2026-07-31'
AND (
ABS(T1."VatSum" - ROUND(T1."LineTotal" * T1."VatPrcnt" / 100.0, 2)) > 1
OR C."LicTradNum" IS NULL
OR T1."VatGroup" IS NULL
)
ORDER BY ABS(T1."VatSum" - ROUND(T1."LineTotal" * T1."VatPrcnt" / 100.0, 2)) DESC;
Run this query before closing each month. Consequently, you will correct the documents while it is still cheap to do so, rather than discovering them during an audit.
Variations and real-world use cases
Parametrize the period without editing the query
Replace the fixed dates with Query Manager variables. This way, SAP Business One will prompt for the range upon execution and the end user will not touch the code.
SQL Server
-- Replace the fixed filter with interactive variables WHERE T0.CANCELED = 'N' AND T0.DocDate >= [%0] AND T0.DocDate <= [%1]
SAP HANA
-- Replace the fixed filter with interactive variables WHERE T0."CANCELED" = 'N' AND T0."DocDate" >= :FechaDesde AND T0."DocDate" <= :FechaHasta
Companies with multiple branches or business units
If you operate with multiple branches, add the field BPLId from the header to the SELECT and the GROUP BY. As a result, you will get a book segmented by establishment without duplicating queries.
Comparison with accounting
A very powerful additional control consists of cross-checking the VAT of the documents against the balance of the tax ledger account in JDT1. If both numbers do not match, there is a manual journal entry that no one documented. Therefore, that cross-check should be part of your monthly routine.
Automation with alerts
Query 4 works perfectly as an automatic alert within SAP Business One. Configure it to run every morning and notify the head of accounting. This way, the error is detected the same day it is generated.

Best practices and common mistakes
These are the stumbles I see most frequently when implementing a purchase and sales ledger on SAP Business One. Furthermore, almost all of them are avoided with discipline, not technology.
- Forget the CANCELED filter. Annulled documents still exist in the database. Consequently, including them inflates the tax debit and generates unexplained discrepancies.
- Confusing DocDate with TaxDate. The tax allocation date in purchases is usually Tax Date. However, many reports use Document Date and shift the tax credit to another period.
- Ignore the credit notes. A book that only totals invoices always overdeclares. Therefore, the union with ORIN and ORPC is not optional.
- Add the VAT from the header. The VatSum field in the header does not allow breakdown by rate. For example, if you have exempt and taxable items in the same document, you will lose the breakdown required by the tax authority.
- Do not validate the tax ID. A business partner without License Trade Number produces a rejected record in the electronic filing.
- Running them during production hours. These queries scan large tables. As a result, it is better to schedule them outside the peak-usage window.
- Not versioning the queries. Save each query in the Query Manager with a clear name and category. Also document who changed it and why.
Official SAP Resources
Before adapting these queries to your localization, review the official documentation. Every country has tax particularities that the standard covers in a different way.
- SAP Help Portal – SAP Business One: complete functional documentation by version.
- Vendor and Customer Tax Invoice: standard behavior of the tax module.
- SAP Community – SAP Business One table structure: table and relationship reference.
Conclusion: the tax close is a maturity indicator
The way a company closes its VAT reveals far more than its accounting standard. It reveals whether it trusts its data, whether its ERP is really implemented and whether management governs with information or with intuition. Consequently, automating the purchase and sales ledger is not a minor technical task: it is a control decision.
A SAP Business One VAT report built on these four queries gives you three things Excel never will. First, full traceability from the declared total down to the document line. Second, early detection of inconsistencies. Third, independence from one person's tacit knowledge.
Start this week. Run the tax group diagnostic query, then the Query 4 audit, and compare the result with your last close. If differences appear, you have just found money or risk that was invisible. Either way, you win.
If you implemented these queries in your company, tell me what you found. The first month's findings are usually the most revealing.




