Sorting, Filtering, Totals and Subtotals
Use ALV built-in features to make reports more useful without unnecessary custom code.
Explanation
ALV provides built-in support for sorting, filtering, totals and subtotals. These features help users analyze report data without exporting to Excel. Amount and quantity fields can be aggregated, while business grouping fields such as company code, customer, plant or sales organization can be used for sorting and subtotaling. Developers should avoid writing unnecessary custom subtotal logic if ALV can handle it. However, totals should be used carefully when currency and unit fields are involved.
Code example
* Purpose:* Add sorting and subtotals so business users can analyze output directly.* Example: Group sales values by customer and show subtotal per customer. DATA(lo_sorts) = lo_alv->get_sorts( ). * Sort by customer number and request subtotal at customer levello_sorts->add_sort( columnname = 'KUNNR' subtotal = abap_true ). DATA(lo_aggrs) = lo_alv->get_aggregations( ). * Add total for net value column* Ensure currency field is also available in output for correct business meaninglo_aggrs->add_aggregation( columnname = 'NETWR' ).Real project scenario
A sales report groups invoices by customer and shows subtotal net value per customer. Users can quickly identify high-value customers directly in ALV.
Common mistakes
- Adding totals to fields where aggregation is meaningless. - Ignoring currency or unit fields. - Manually calculating totals when ALV can do it. - Not testing subtotal layout with realistic data.
Best practices
- Use ALV built-in sorting and totals. - Validate currency/unit handling. - Keep grouping meaningful. - Avoid unnecessary manual subtotal code.
Interview angle
Interviewers may ask how to add totals/subtotals in ALV. A strong answer explains ALV aggregation and currency/unit care.