ERPClimb logoERPClimb

Code examples · Modern ABAP · Intermediate

Let the database add up: GROUP BY, SUM, HAVING and a size label on sales items (VBAP)

The problem

A sales manager wants the five biggest multi-item orders with their item count, total net value and a size label.

How we solve it

  1. 1Groupone row per order
  2. 2FilterHAVING
  3. 3LabelCOND
  4. 4Printloop

The result

Up to five sales documents with their item count, total net value, currency and a size label.

SAP objects in play

Overview drawn by ERPClimb from this example. The steps are the same as in the code below.

1. The requirement

A sales manager wants the five biggest multi-item orders with their item count, total net value and a size label. Reading every item into the program and adding them up in a loop would move far too much data.

2. How we design it

The database is very good at adding up. Instead of reading all items and summing in ABAP, we ask for one row per sales document: GROUP BY makes the groups, COUNT and SUM calculate per group, HAVING removes groups after the calculation, and the program gives each group a label. Only a handful of result rows travel to the program.

Why we decided it this way

  1. 1Aggregate in the SELECT, not in a loopThousands of item rows become one row per document inside the database. The program receives only what it prints.
  2. 2Currency in the GROUP BYA sum of amounts is only meaningful per currency. Grouping by the currency keeps different currencies apart.
  3. 3HAVING for a condition on the resultWHERE filters rows before they are grouped. A rule such as 'more than one item' can only be checked after counting, which is what HAVING does.
  4. 4ORDER BY the alias, biggest orders firstThe alias total_net names the calculated column, so the sort and the output use the same name.

Before you start

  • A system with sales orders. Look at the items first in SE16N: table VBAP, fields VBELN (document), POSNR (item), NETWR (net value) and WAERK (currency).
  • ADT and a package such as $TMP.

3. The code, step by step

Step 1 of 4: Group the items per order

One result row per sales document with count and sum.

ABAP · Class · ZCL_ITEM_TOTALS (main method, part 1)
" Class frame
CLASS zcl_item_totals DEFINITION PUBLIC FINAL CREATE PUBLIC.
  PUBLIC SECTION.
    INTERFACES if_oo_adt_classrun.
ENDCLASS.

CLASS zcl_item_totals IMPLEMENTATION.
  METHOD if_oo_adt_classrun~main.
    " COUNT, SUM and GROUP BY
    SELECT vbeln,
           COUNT( * )    AS item_count,
           SUM( netwr )  AS total_net,
           waerk
Part by part (plain text)
  1. Class frame. The standard frame of a runnable class. The SELECT follows in the next segments, inside the main method.
  2. COUNT, SUM and GROUP BY. COUNT(*) counts the item rows of a group and SUM( netwr ) adds up their net values. AS gives each calculated column a name that the program and the ORDER BY can use. GROUP BY lists every selected field that is not calculated: the document and the currency.

Open in the portal

The SAP objects named in this code are linked in the code above and listed here. Each opens its page in the SAP Library.

SAP tables

4. The final outcome

Up to five sales documents with their item count, total net value, currency and a size label.

What you should see

Example output (your system has other data): Order 12345: 7 items, 15200.00 EUR, Large Order 12399: 3 items, 480.50 EUR, Small Order 12410: 2 items, 99.00 EUR, Small

Try it and check the result

  • Put the four SELECT segments together in the order shown, activate with Ctrl+F3 and run with F9.
  • Open VBAP in SE16N for the first document that is printed and count its items: the number matches item_count.
  • Change HAVING COUNT( * ) > 1 to > 5 and run again: only orders with more than five items remain.

Common mistakes

  • Every plain field in the select list must also appear in GROUP BY. A missing field is a syntax error.
  • Adding amounts of different currencies gives a meaningless number. Always group by the currency field, or convert first.
  • VBAP is one of the largest tables in a system. A WHERE condition (a date range or a sales organization) makes the same query much faster in a real system.

Keep learning

Checking status: Reviewed line by line against SAP documentation. Not executed on a live system. Reviewed 2026-11-25. Release: SAP S/4HANA or ECC system (for example an S/4HANA trial appliance from SAP Cloud Appliance Library). Uses SAP business tables, which are not released for ABAP Cloud.

Original ERPClimb teaching code with illustrative names. Not taken from a customer system. Syntax can differ by release; check the references below and activate in a sandbox first. ERPClimb is independent of SAP SE.