Application Development and Automation 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: 
Read only

Collect? SUM? -

Former Member
0 Likes
1,020

Hi,

I have follwing internal table with Numbers and texts

01    ABC    100	200	300
01    ZSD    500	700	700
01    ASW    600	200	300

What I need is a single record with all totals omitting that text.how can we do this? like below

01	1200	1100	1300

rgds

vara

1 ACCEPTED SOLUTION
Read only

faisalatsap
Active Contributor
0 Likes
990

Hi,

Test the following code.

TYPES: BEGIN OF t_test,
  name(3),
  amount1 TYPE i,
  amount2 TYPE i,
  END OF t_test.

DATA: it1_test TYPE STANDARD TABLE OF t_test WITH HEADER LINE,
      it2_test TYPE STANDARD TABLE OF t_test WITH HEADER LINE.


it1_test-name = 'AAA'.
it1_test-amount1 = 15.
it1_test-amount2 = 15.
APPEND it1_test TO it1_test.
it1_test-name = 'BBB'.
it1_test-amount1 = 15.
it1_test-amount2 = 15.
APPEND it1_test TO it1_test.
it1_test-name = 'BBB'.
it1_test-amount1 = 15.
it1_test-amount2 = 15.
APPEND it1_test TO it1_test.
it1_test-name = 'BBB'.
it1_test-amount1 = 15.
it1_test-amount2 = 15.
APPEND it1_test TO it1_test.


LOOP AT it1_test INTO it1_test.
  it1_test-name = ''. " USE THIS 
  COLLECT it1_test INTO it2_test.
ENDLOOP.

Kind Regards,

Faisal

Edited by: Faisal Altaf on Feb 3, 2009 7:40 PM

Edited by: Faisal Altaf on Feb 3, 2009 7:44 PM

Hi,

I have follwing internal table with Numbers and texts

01    ABC    100	200	300
01    ZSD    500	700	700
01    ASW    600	200	300

What I need is a single record with all totals omitting that text.how can we do this? like below

01	1200	1100	1300

rgds

vara

6 REPLIES 6
Read only

faisalatsap
Active Contributor
0 Likes
991

Hi,

Test the following code.

TYPES: BEGIN OF t_test,
  name(3),
  amount1 TYPE i,
  amount2 TYPE i,
  END OF t_test.

DATA: it1_test TYPE STANDARD TABLE OF t_test WITH HEADER LINE,
      it2_test TYPE STANDARD TABLE OF t_test WITH HEADER LINE.


it1_test-name = 'AAA'.
it1_test-amount1 = 15.
it1_test-amount2 = 15.
APPEND it1_test TO it1_test.
it1_test-name = 'BBB'.
it1_test-amount1 = 15.
it1_test-amount2 = 15.
APPEND it1_test TO it1_test.
it1_test-name = 'BBB'.
it1_test-amount1 = 15.
it1_test-amount2 = 15.
APPEND it1_test TO it1_test.
it1_test-name = 'BBB'.
it1_test-amount1 = 15.
it1_test-amount2 = 15.
APPEND it1_test TO it1_test.


LOOP AT it1_test INTO it1_test.
  it1_test-name = ''. " USE THIS 
  COLLECT it1_test INTO it2_test.
ENDLOOP.

Kind Regards,

Faisal

Edited by: Faisal Altaf on Feb 3, 2009 7:40 PM

Edited by: Faisal Altaf on Feb 3, 2009 7:44 PM

Read only

Former Member
0 Likes
990

Hi

I amn't sure about a shorter way,

but you could copy your internal table to another which doesn't have the text field.

Now you could use COLLECT on this table.

Pushpraj

Read only

faisalatsap
Active Contributor
0 Likes
990

Hi,

You can use the following too.

LOOP AT it1_test INTO it1_test.
  AT LAST.
    SUM.
    it1_test-name = ''.
    APPEND it1_test TO it2_test.
  ENDAT.
ENDLOOP.

Please Reply if any problem,

Kind Regards,

Faisal

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
990

Hello Vara,

Is '01' a numeric field ?? Then COLLECT will add the values and you will have output as

03 1200 1100 1300

The statement SUM can only be specified within a loop starting with LOOP, and is only considered within a AT-ENDAT control structure. SUM will calculate the total of numeric fields.

Try this code:



TYPES: BEGIN OF t_test,
  line TYPE i,
  name(3),
  amount1 TYPE i,
  amount2 TYPE i,
  END OF t_test.

DATA:
it  TYPE STANDARD TABLE OF t_test,
wa  TYPE t_test,
wa1 TYPE t_test.

wa-line = 1.
wa-name = 'AAA'.
wa-amount1 = 15.
wa-amount2 = 15.
APPEND wa TO it.

wa-line = 1.
wa-name = 'BBB'.
wa-amount1 = 15.
wa-amount2 = 15.
APPEND wa TO it.

wa-line = 1.
wa-name = 'CCC'.
wa-amount1 = 15.
wa-amount2 = 15.
APPEND wa TO it.

wa-line = 1.
wa-name = 'DDD'.
wa-amount1 = 15.
wa-amount2 = 15.
APPEND wa TO it.

wa-line = 2.
wa-name = 'AAA'.
wa-amount1 = 15.
wa-amount2 = 15.
APPEND wa TO it.

wa-line = 3.
wa-name = 'BBB'.
wa-amount1 = 15.
wa-amount2 = 15.
APPEND wa TO it.

wa-line = 2.
wa-name = 'CCC'.
wa-amount1 = 15.
wa-amount2 = 15.
APPEND wa TO it.

wa-line = 2.
wa-name = 'DDD'.
wa-amount1 = 15.
wa-amount2 = 15.
APPEND wa TO it.

SORT it BY line name.

LOOP AT it INTO wa.

  AT END OF line.
    CLEAR wa-name.
    SUM.
    WRITE:/
      wa-line,
      wa-amount1,
      wa-amount2.
  ENDAT.
ENDLOOP.

I think SUM is the option you should use.

@ Faisal: So how do you intend to clarify the OP's doubt. Which method do you suggest? You have mentioned both. Please be clear in your answers.

BR,

Suhas

Edited by: Suhas Saha on Feb 3, 2009 8:17 PM

Read only

Former Member
0 Likes
990

Hi Vara,

If you are summing according to field1, than do it this way:

Loop at itab into wa.
  At end of field1.
    Sum.
    Write:/
      wa-field1,
      wa-field3,            " hope its numeric field
      wa-field4,            " hope its numeric field
      wa-field5.            " hope its numeric field
  Endat.  
Endloop.

Also you can do it this way:

Loop at itab into wa.

w_field3 = w_field3 + wa-field3.
w_field4 = w_field4 + wa-field4.
w_field5 = w_field5 + wa-field5.
  
  At end of field1.
    Write:/
      wa-field1,
      w_field3,            
      w_field4,            
      w_field5.   
    Clear w_field3.
    Clear w_field4.
    Clear w_field5.
  Endat.        
Endloop.

With luck,

Pritam.

Edited by: Pritam Ghosh on Feb 3, 2009 3:51 PM

Read only

0 Likes
990

Hi All,

Thank you veruy much for all your inputs.It worked!!!

I took faisal code with collect statements.I am awarding full points to faisal.

Rgds

vara