Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

ALV grid total andsub-total

Former Member
0 Kudos

hi there...I know there´s a lot of posts about this subject but i didn't find what i´m looking for...

I want to know if there´s some way to give a total and a sub-total of a field, but instead of the total and subtotal sums I just want a count of the number of rows for the field that I've sorted.

FIELDCATALOG-FIELDNAME   = 'BUKRS'.
  FIELDCATALOG-SELTEXT_M   = 'BUKRS'.
  FIELDCATALOG-COL_POS     = 0.
  APPEND FIELDCATALOG TO FIELDCATALOG.
  CLEAR  FIELDCATALOG.


  FIELDCATALOG-FIELDNAME   = 'BUTXT'.
  FIELDCATALOG-SELTEXT_M   = 'BUTXT'.
  FIELDCATALOG-COL_POS     = 1.
  APPEND FIELDCATALOG TO FIELDCATALOG.
  CLEAR  FIELDCATALOG.



  FIELDCATALOG-FIELDNAME   = 'LIFNR'.
  FIELDCATALOG-SELTEXT_M   = 'LIFNR'.
  FIELDCATALOG-COL_POS     = 2.
  APPEND FIELDCATALOG TO FIELDCATALOG.
  CLEAR  FIELDCATALOG.

  FIELDCATALOG-FIELDNAME   = 'NAME1'.
  FIELDCATALOG-SELTEXT_M   = 'NAME1'.
  FIELDCATALOG-COL_POS     = 3.
  APPEND FIELDCATALOG TO FIELDCATALOG.
  CLEAR  FIELDCATALOG.

  FIELDCATALOG-FIELDNAME   = 'MBLNR'.
  FIELDCATALOG-SELTEXT_M   = 'MBLNR'.
  FIELDCATALOG-COL_POS     = 4.
  APPEND FIELDCATALOG TO FIELDCATALOG.
  CLEAR  FIELDCATALOG.

  FIELDCATALOG-FIELDNAME   = 'MJAHR'.
  FIELDCATALOG-SELTEXT_M   = 'MJAHR'.
  FIELDCATALOG-COL_POS     = 5.
  APPEND FIELDCATALOG TO FIELDCATALOG.
  CLEAR  FIELDCATALOG.

  FIELDCATALOG-FIELDNAME   = 'CPUDT'.
  FIELDCATALOG-SELTEXT_M   = 'CPUDT'.
  FIELDCATALOG-COL_POS     = 6.
  APPEND FIELDCATALOG TO FIELDCATALOG.
  CLEAR  FIELDCATALOG.

  FIELDCATALOG-FIELDNAME   = 'BUDAT'.
  FIELDCATALOG-SELTEXT_M   = 'BUDAT'.
  FIELDCATALOG-COL_POS     = 7.
  APPEND FIELDCATALOG TO FIELDCATALOG.
  CLEAR  FIELDCATALOG.

  FIELDCATALOG-FIELDNAME   = 'USNAM'.
  FIELDCATALOG-SELTEXT_M   = 'BUDAT'.
  FIELDCATALOG-COL_POS     = 8.
  APPEND FIELDCATALOG TO FIELDCATALOG.
  CLEAR  FIELDCATALOG.

  FIELDCATALOG-FIELDNAME   = 'MENGE'.
  FIELDCATALOG-SELTEXT_M   = 'MENGE'.
  FIELDCATALOG-COL_POS     = 9.
  APPEND FIELDCATALOG TO FIELDCATALOG.
  CLEAR  FIELDCATALOG.

  FIELDCATALOG-FIELDNAME   = 'DMBTR'.
  FIELDCATALOG-SELTEXT_M   = 'DMBTR'.
  FIELDCATALOG-COL_POS     = 10.
  APPEND FIELDCATALOG TO FIELDCATALOG.
  CLEAR  FIELDCATALOG.

*&---------------------------------------------------------------------*
*          SORT
*&---------------------------------------------------------------------*
  WA_SORT-SPOS = 1.
  WA_SORT-FIELDNAME = 'BUKRS'.
  WA_SORT-TABNAME = 'ITAB'.
  WA_SORT-UP = 'X'.
  WA_SORT-SUBTOT = 'X'.
  APPEND WA_SORT TO IT_SORT.
  CLEAR WA_SORT.
  
  WA_SORT-SPOS = 2.
  WA_SORT-FIELDNAME = 'LIFNR'.
  WA_SORT-TABNAME = 'ITAB'.
  WA_SORT-UP = 'X'.
  WA_SORT-SUBTOT = 'X'.
  APPEND WA_SORT TO IT_SORT.
  CLEAR WA_SORT.

in the alv i want to know the number of rows for each value of lifnr (sub-total) and in the end the total of the all values of lifnr

thanks

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi,

You can add one field COUNT in your output table which is to be displayed in ALV output. Assign value to this COUNT field as '1'.

lt_output-count = '1'.

Now in fieldcatalog do this change:

 FIELDCATALOG-FIELDNAME   = 'COUNT'.
  FIELDCATALOG-SELTEXT_M   = 'Count'.
  FIELDCATALOG-COL_POS     = 11.
  FIELDCATALOG-DO_SUM = 'X'.
  APPEND FIELDCATALOG TO FIELDCATALOG.
  CLEAR  FIELDCATALOG.

As you have already provided sort criteria, it will result in total number of rows which are sorted in COUNT column.

Hope this fullfills your requirement.

Regards,

Brajvir

3 REPLIES 3

MarcinPciak
Active Contributor
0 Kudos

Hi,

It is better to obtain this information not in ALV but working on the table. Using control level processing it is easy to achieve.


data: begin of it_overview occurs 0 
          lifnr type ..
          tot_item type i,     "number of lifnr withing subtotal
        end of it_overwiev.

data: pos type i.   "to remember the position.

"your output table
Loop at it_ouptut.
   at new lifnr.
      pos = sy-tabix.    "remember first position of new LIFNR
   endat. 
   
   at end of lifnr.
      it_overview-lifnr = it_output-lifnr.
      it_overview-tot_item = sy-tabix - pos.    "calculate how many within one LIFNR
      append it_overview.                             "store it in the table
      
       clear pos.
   endat.
endloop.

this way table IT_OVERVIEW stores LIFNRs and number of items forming their subtotals.

To get total use:


DESCRIEBE TABLE it_output.
it_output-lifnr = 'TOTAL_ITEMS'.    "total items within all LIFNRs
it_output-tot_item = sy-tfill.            "is equal to total lines in the table
append it_output.

Regards

Marcin

Former Member
0 Kudos

Hi,

I dont think there is such a way that you are looking. If required you need to programatically handle the requirement. As you have already know the sort fields, you can just add rows where ever a new sortlevel occurs.

Venkat.

Former Member
0 Kudos

Hi,

You can add one field COUNT in your output table which is to be displayed in ALV output. Assign value to this COUNT field as '1'.

lt_output-count = '1'.

Now in fieldcatalog do this change:

 FIELDCATALOG-FIELDNAME   = 'COUNT'.
  FIELDCATALOG-SELTEXT_M   = 'Count'.
  FIELDCATALOG-COL_POS     = 11.
  FIELDCATALOG-DO_SUM = 'X'.
  APPEND FIELDCATALOG TO FIELDCATALOG.
  CLEAR  FIELDCATALOG.

As you have already provided sort criteria, it will result in total number of rows which are sorted in COUNT column.

Hope this fullfills your requirement.

Regards,

Brajvir