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

Count based on conditions

Former Member
0 Likes
2,235

Dear Apapers,

My requirement is count based on matnr and werks. that is how many same matnr available in particular plant wise.

My input is,

Matnr werks

12 chen

12 chen

12 pblr

13 chen in this via

my target is,

matnr werks cnt

12 chen 2

12 pblr 1

13 chen 1 . plz help me?how to resolve this issues..

regards,

prabu

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,336

You can use control break statement inside your loop,

declare a 3rd variable as counter now

loop .

at end of

or

at last

increase the counter by 1

endloop

Hope it helps

Bhanu

Dear Apapers,

My requirement is count based on matnr and werks. that is how many same matnr available in particular plant wise.

My input is,

Matnr werks

12 chen

12 chen

12 pblr

13 chen in this via

my target is,

matnr werks cnt

12 chen 2

12 pblr 1

13 chen 1 . plz help me?how to resolve this issues..

regards,

prabu

7 REPLIES 7
Read only

Former Member
0 Likes
1,337

You can use control break statement inside your loop,

declare a 3rd variable as counter now

loop .

at end of

or

at last

increase the counter by 1

endloop

Hope it helps

Bhanu

Read only

Former Member
0 Likes
1,336

Sort your table .

Then loop at table and use AT END OF to count number of occurances.

Hope it helps you.

Read only

Former Member
0 Likes
1,336

Try this!

Loop at the internal table.

Check if the material and plant combination is same for 1st and 2nd record.If yes increment the counter else reset it. Store the counter value in same internal table.

Read only

I355602
Product and Topic Expert
Product and Topic Expert
0 Likes
1,336

Hi,

Use:-


DATA : v_count TYPE i.

SORT itab BY matnr werks.

LOOP AT itab INTO wa.
  CLEAR v_count.
  AT NEW werks.
    v_count = v_count + 1. "<--increment counter
  ENDAT.
  wa_count = v_count. "<--move value to work area
  MODIFY itab FROM wa INDEX sy-tabix TRANSPORTING count. "<--modify internal table
  CLEAR wa.
ENDLOOP.

Hope this helps you.

Regards,

Tarun

Read only

dev_parbutteea
Active Contributor
0 Likes
1,336

Hi,

create a new structure wa_new with matnr, werks, count(type i).

then

loop at itab into wa_itab.

wa_new-matnr = wa_itab-matnr.

wa_new-werks = wa_itab-werks.

wa_new-count = 1.

collect wa_new into itab_new.

endloop.

Thus, itab_new will contain the require output.

Regards.

Read only

Former Member
0 Likes
1,336

Dear this you cae do as follow

make a duplicate copy of your internal table .

say :

it_mara and it_mara_dup .

loop at it_mara into wa_mara .
at new wa_mara-werks .
 
loop at it_mara_dup where werks = wa_mara-werks into wa_mara_dup .
count = count + 1 .
endlloop.
  
endat.

endloop.

regards ankit

Read only

Former Member
0 Likes
1,336

create an internal table itab1 and wa1 with fields matnr werks count.

DATA : count TYPE i.
SORT itab BY matnr werks.
LOOP AT itab INTO wa.
  count = count + 1.
  AT end werks.                  " triggered when matnr or werks is about to change
    CLEAR count
    wa1-matnr = wa-matnr.
    wa1-werks = wa-werks.
    wa1-count = count.
    append wa1 into itab1.   " add matnr werks and count in itab1
  ENDAT.
ENDLOOP.

the table itab1 that you get contains the number of times a matnr and werks repeats.

Regards,

Lalit Mohan Gupta.