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 Index Internal Table or Select Statement

adeel_sarwar
Contributor
0 Likes
1,356

Hi All,

I wish to index and display my internal table. How do i do this? for example in my Internal Table I have the following data:-

Material-----Plant--


Index

ABCD1--


1000--


1

ABCD1--


1000--


2

ABCD1--


1000--


3

ABCD2--


1000--


1

ABCD2--


1000--


2

Basically based on the unique values in Material and Plant an Index is generated? does that make sense?

Thanks

Adeel

10 REPLIES 10
Read only

faisalatsap
Active Contributor
0 Likes
1,156

Hi Adeel,

Please test the following Sample Code hope will solve out your problem,

DATA: BEGIN OF it_m OCCURS 10,
  index TYPE i,
  material(6),
  amount TYPE i,
  END OF it_m.

DATA: it_m2 LIKE STANDARD TABLE OF it_m WITH HEADER LINE.

it_m-material = 'AAAAA'.
it_m-amount = 10.
APPEND it_m TO it_m.

it_m-material = 'AAAAA'.
it_m-amount = 10.
APPEND it_m TO it_m.

it_m-material = 'AAAAA'.
it_m-amount = 10.
APPEND it_m TO it_m.

it_m-material = 'BBBBB'.
it_m-amount = 10.
APPEND it_m TO it_m.

it_m-material = 'BBBBB'.
it_m-amount = 10.
APPEND it_m TO it_m.

SORT it_m BY material.

DATA: index TYPE i.
LOOP AT it_m INTO it_m.
  AT NEW material.
    ADD: 1 TO index.
  ENDAT.
  MOVE-CORRESPONDING it_m TO it_m2.
  it_m2-index = index.
  APPEND it_m2.
ENDLOOP.

Please Reply if any issue,

Kind Regards,

Faisal

Read only

Former Member
0 Likes
1,156

hi,

Use the following loop.

data w_index type i.
loop at itab into wa.
at new material.
  w_index = 1.
endat.
write : wa-material, wa-plant, w_index.
" if you want to modify internal table then you can use the code below added to the above code...
"  wa-index = w_index.
" modify itab from wa index sy-tabix.
endloop.

Regards,

Siddarth

Read only

faisalatsap
Active Contributor
0 Likes
1,156

Hi, Adeel

Test the following to i think this time it is according to your requirement now.

DATA: BEGIN OF it_m OCCURS 10,
  material(6),
  amount TYPE i,
  index TYPE i,
  END OF it_m.

DATA: it_m2 LIKE STANDARD TABLE OF it_m WITH HEADER LINE.

it_m-material = 'AAAAA'.
it_m-amount = 10.
APPEND it_m TO it_m.

it_m-material = 'AAAAA'.
it_m-amount = 10.
APPEND it_m TO it_m.

it_m-material = 'AAAAA'.
it_m-amount = 10.
APPEND it_m TO it_m.

it_m-material = 'BBBBB'.
it_m-amount = 10.
APPEND it_m TO it_m.

it_m-material = 'BBBBB'.
it_m-amount = 10.
APPEND it_m TO it_m.

SORT it_m BY material.

DATA: index TYPE i.
LOOP AT it_m INTO it_m.

  AT NEW material.
    ADD: 1 TO index.
  ENDAT.

  MOVE-CORRESPONDING it_m TO it_m2.
  it_m2-index = index.
  APPEND it_m2.
  ADD: 1 TO index.

  AT END OF material.
    CLEAR: index.
  ENDAT.

ENDLOOP.

LOOP AT it_m2.

  WRITE: / it_m2-material, it_m2-amount, it_m2-index.

ENDLOOP.

Please Reply if any Issue,

Kind Regards,

Faisal

Read only

Former Member
0 Likes
1,156

Siddarth's suggestion would work, except that he forgot to increment the variable.


data w_index type i.
loop at itab into wa.
  at new material.
    w_index = 1.
  endat.
  add 1 to w_index.
  write : wa-material, wa-plant, w_index.
" if you want to modify internal table then you can use the code below added to the above code...
"  wa-index = w_index.
" modify itab from wa index sy-tabix.
endloop.

___

Sudhi Karkada

<a href="http://main.nationalmssociety.org/site/TR/Bike/TXHBikeEvents?px=5888378&pg=personal&fr_id=10222">Biking for MS Relief</a>

Read only

0 Likes
1,156

Hi,

I think he also forget to clear INDEX in So Both Code will not work fine for the condition, Please Have a look at the code bellow it will work accordingly i have tested,

DATA: index TYPE i.

LOOP AT it_m INTO it_m.

  AT NEW material.
    CLEAR: index.
    ADD: 1 TO index.
  ENDAT.

  it_m-index = index.
  WRITE: / it_m-material, it_m-amount, it_m-index.
  ADD: 1 TO index.

ENDLOOP.

Kind Regards,

Faisal

Read only

0 Likes
1,156

Hi Guys,

Tried them it doesn't seem to work. I might try a different approach. i have the following loop.

What i am trying to do is bring the last price of an entry from itab and storing this in itab. Example

*Material----


Price----


Previous Price*

ABC1 -

-


£10----

-


£0 (as this is the first record)

ABC1 -

-


£30----

-


£10

ABC2 -

-


£15----


£30 *<----

-


ERROR*

ABC2 -

-


£45----

-


£15

So basically it is working exactly how i want it apart from when there is a new Material it isn't clearing the previous price even though there is no previous price....does that make sense? I was thinking of doing something via counting etc... but you guys might know an easier solution. Code is below how can i clear the previous price using the example above.

Please note this code does exactly what i want apart from clearing the previous price if it doesn;t exist.

LOOP AT itab.
  MOVE sy-tabix TO chnge.        " sy-tabix counts no of records.
  IF sy-tabix > 1.
    w_num = sy-tabix.
    w_num = w_num - 1.
  MOVE konp-kbetr TO itab-price.
  
  ENDIF.
ENDLOOP.

Read only

0 Likes
1,156

Hi,

use the logic mentioned below

data : prev_price like itab-price,
         mat like itab-price.

loop at itab.
  if mat <> itab-material.
     mat = itab-material.
     clear prev_price.
  endif. 
  itab-previous_price = prev_price.
  prev_price = itab-price.
  modify itab.
endloop.

Read only

0 Likes
1,156

Hi, Adeel

Test the code bellow it working accordingly hope this time will solve out your problem i have tested it is working fine as per you requirement.

DATA: BEGIN OF it_m OCCURS 10,
  material(6),
  amount TYPE i,
  p_amount TYPE i,
  END OF it_m.

DATA: wa_it_m LIKE it_m,
      c_record LIKE sy-tabix,
      p_record LIKE sy-tabix.

it_m-material = 'AAAAA'.
it_m-amount = 10.
APPEND it_m TO it_m.

it_m-material = 'AAAAA'.
it_m-amount = 20.
APPEND it_m TO it_m.

it_m-material = 'AAAAA'.
it_m-amount = 30.
APPEND it_m TO it_m.

it_m-material = 'BBBBB'.
it_m-amount = 10.
APPEND it_m TO it_m.

it_m-material = 'BBBBB'.
it_m-amount = 20.
APPEND it_m TO it_m.

DATA: index TYPE i.

BREAK-POINT.
LOOP AT it_m INTO it_m.
  c_record = sy-tabix.
  p_record = c_record - 1.

  READ TABLE it_m INTO wa_it_m INDEX p_record.
  IF sy-subrc EQ 0.
    IF it_m-material EQ wa_it_m-material.
      it_m-p_amount = wa_it_m-amount.
    ELSE.
      CLEAR: it_m-p_amount.
    ENDIF.
  ENDIF.

  WRITE: / it_m-material, it_m-amount, it_m-p_amount.
  MODIFY it_m FROM it_m INDEX c_record.

ENDLOOP.

Please Reply if any Issue,

Kind Regards,

Faisal

Read only

Former Member
0 Likes
1,156

Hi Adeel,

Check the below logic, its working.

Here it_tab has the fields matnr, price, price_prev and wa_tab is the work area for the same.

w_prev stores the previous price

LOOP AT it_tab INTO wa_tab.
  w_flag = 'X'. "Set the flag
  AT NEW matnr.
    CLEAR w_flag. "If its ne material clear it
  ENDAT.
  IF w_flag EQ 'X'. "If the flag has value X still then that is no the new material
    wa_tab-price_prev = w_prev. "So assign the previous value
    MODIFY it_tab FROM wa_tab TRANSPORTING price_prev. "And upadate the int tab with prev value
  ENDIF.
  w_prev = wa_tab-price. "Store the currt price in the so that it'll be used as prev price in next loop
ENDLOOP.

Regards,

Manoj Kumar P

Edited by: Manoj Kumar on Mar 6, 2009 6:04 AM

Read only

adeel_sarwar
Contributor
0 Likes
1,156

No useful replies. Created an ABAP Program instead of query.

Thread Closed