Monday, September 22, 2008

Display Average in ALV report

By Joyjit Ghosh,
Kolkata, India.


Code:

*&---------------------------------------------------------------------*
*& Report Z_DEMO_JG
*&
*&---------------------------------------------------------------------*
*& Display average in ALV report
*&---------------------------------------------------------------------*

REPORT z_demo_jg LINE-SIZE 80 LINE-COUNT 50

NO STANDARD PAGE HEADING.

TABLES: sflight.

TYPE-POOLS: slis.

* Data declaration

DATA: i_flight TYPE STANDARD TABLE OF sflight,

i_catalog TYPE slis_t_fieldcat_alv,

w_flight TYPE sflight,

w_catalog TYPE slis_fieldcat_alv.

DATA: v_repid TYPE syrepid.

* Selection screen

SELECT-OPTIONS: s_carrid FOR sflight-carrid.


START-OF-SELECTION.

* Fetch data from sflight table

SELECT *

FROM sflight

INTO TABLE i_flight

WHERE carrid IN s_carrid.


CHECK sy-subrc = 0.

v_repid = sy-repid.

* Build field catalog for ALV report

PERFORM build_catalog.

* Display ALV report

PERFORM display_report.

*&---------------------------------------------------------------------*
*& Form build_catalog
*&---------------------------------------------------------------------*
* Build field catalog for ALV report
*----------------------------------------------------------------------*
* None
*----------------------------------------------------------------------*

FORM build_catalog .

CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'

EXPORTING

i_program_name = v_repid

i_structure_name = 'SFLIGHT'

CHANGING

ct_fieldcat = i_catalog

EXCEPTIONS

inconsistent_interface = 1

program_error = 2

OTHERS = 3.

IF sy-subrc = 0.

* For average populate 'C' as value in

* field DO_SUM

w_catalog-do_sum = 'C'. " C = Average

* Modify record with new value

MODIFY i_catalog FROM w_catalog TRANSPORTING

do_sum WHERE fieldname = 'PRICE'.

ENDIF.

ENDFORM. " build_catalog

*&---------------------------------------------------------------------*
*& Form display_report
*&---------------------------------------------------------------------*
* Display ALV report
*----------------------------------------------------------------------*
* None
*----------------------------------------------------------------------*

FORM display_report .

IF i_catalog[] IS NOT INITIAL.

* Call ALV grid

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

i_callback_program = v_repid

it_fieldcat = i_catalog

TABLES

t_outtab = i_flight

EXCEPTIONS

program_error = 1

OTHERS = 2.

IF sy-subrc = 0.

ENDIF.

ENDIF.

ENDFORM. " display_report

Selection Screen:

Output:

No comments: