Customer aging report (AR aging)
Open invoice balance per customer in 0–30, 31–60, 61–90 and 90+ day buckets by due date.
Tool
SELECT T0.CardCode, T0.CardName, SUM(CASE WHEN DATEDIFF(day, T0.DocDueDate, GETDATE()) <= 30 THEN T0.DocTotal - T0.PaidToDate ELSE 0 END) AS Days0to30, SUM(CASE WHEN DATEDIFF(day, T0.DocDueDate, GETDATE()) BETWEEN 31 AND 60 THEN T0.DocTotal - T0.PaidToDate ELSE 0 END) AS Days31to60, SUM(CASE WHEN DATEDIFF(day, T0.DocDueDate, GETDATE()) BETWEEN 61 AND 90 THEN T0.DocTotal - T0.PaidToDate ELSE 0 END) AS Days61to90, SUM(CASE WHEN DATEDIFF(day, T0.DocDueDate, GETDATE()) > 90 THEN T0.DocTotal - T0.PaidToDate ELSE 0 END) AS Over90, SUM(T0.DocTotal - T0.PaidToDate) AS TotalOpen FROM OINV T0 WHERE T0.DocStatus = 'O' AND T0.CANCELED = 'N' GROUP BY T0.CardCode, T0.CardName ORDER BY TotalOpen DESC
SELECT T0."CardCode", T0."CardName", SUM(CASE WHEN DAYS_BETWEEN(T0."DocDueDate", CURRENT_TIMESTAMP) <= 30 THEN T0."DocTotal" - T0."PaidToDate" ELSE 0 END) AS "Days0to30", SUM(CASE WHEN DAYS_BETWEEN(T0."DocDueDate", CURRENT_TIMESTAMP) BETWEEN 31 AND 60 THEN T0."DocTotal" - T0."PaidToDate" ELSE 0 END) AS "Days31to60", SUM(CASE WHEN DAYS_BETWEEN(T0."DocDueDate", CURRENT_TIMESTAMP) BETWEEN 61 AND 90 THEN T0."DocTotal" - T0."PaidToDate" ELSE 0 END) AS "Days61to90", SUM(CASE WHEN DAYS_BETWEEN(T0."DocDueDate", CURRENT_TIMESTAMP) > 90 THEN T0."DocTotal" - T0."PaidToDate" ELSE 0 END) AS "Over90", SUM(T0."DocTotal" - T0."PaidToDate") AS "TotalOpen" FROM OINV T0 WHERE T0."DocStatus" = 'O' AND T0."CANCELED" = 'N' GROUP BY T0."CardCode", T0."CardName" ORDER BY "TotalOpen" DESC
What this query answers
The credit controller prepares the weekly collections list and the CFO wants to know how much customer money is stuck beyond 60 or 90 days. This splits each customer's open invoice balance into age buckets by due date.
Sample output
Illustrative demo values. Your columns are the same; the numbers come from your company.
| CardCode | CardName | Days0to30 | Days31to60 | Days61to90 | Over90 | TotalOpen |
|---|---|---|---|---|---|---|
| C20000 | Acme Trading | 12400 | 5300 | 0 | 2100 | 19800 |
| C30000 | Northwind Retail | 8750 | 0 | 3200 | 0 | 11950 |
| C40000 | Blue Harbor Foods | 2500 | 1800 | 0 | 4600 | 8900 |
| C50000 | Summit Supplies | 0 | 0 | 0 | 3150 | 3150 |
SQL Server vs SAP HANA
Each DATEDIFF(day, T0.DocDueDate, GETDATE()) becomes DAYS_BETWEEN(T0."DocDueDate", CURRENT_TIMESTAMP). The argument order is the same (start, end), so a positive result still means past due and the bucket limits do not change.
Common errors and fixes
- Totals do not match the standard Customer Receivables Aging report: This query reads open A/R invoices only. Open credit notes, down payments and payments on account are not deducted, so the standard report can be lower.
- Invoices that are not due yet sit in the 0-30 bucket: Not-yet-due invoices give a negative day count, which matches <= 30. Add a separate NotDue bucket (days <= 0) if you want to separate them.
- Amounts look wrong for foreign-currency customers: DocTotal and PaidToDate are in local currency. Use DocTotalFC - PaidFC to age in the document currency.
Need this report built into SAP Business One?
A saved query answers the question once. We build B2B customer portals for SAP Business One: customers see their prices, stock, order status, invoices and statements, and their orders land in SAP as sales orders. 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))
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 customer aging report (ar aging) 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 from the standard SAP Business One schema. No user-defined fields are needed.
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.
