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

check an internal table with 2 fields

Former Member
0 Likes
1,417

Hi all,

I have 2 fields in my internal table

field1

field2

case1 ---Field1 ---1 to 1 value to filed2

case2 ---filed1 --- 1 to many values to field2 .

i need to get number of record with case1

get number of records with case2 .

can any let me know how to achieve this one?

Hi all,

I have 2 fields in my internal table

field1

field2

case1 ---Field1 ---1 to 1 value to filed2

case2 ---filed1 --- 1 to many values to field2 .

i need to get number of record with case1

get number of records with case2 .

can any let me know how to achieve this one?

12 REPLIES 12
Read only

amit_khare
Active Contributor
0 Likes
1,397

Check help on DESCRIBE statement.

Read only

Former Member
0 Likes
1,397

please let me know if you have any idea.

Read only

0 Likes
1,397

Can you elaborate more on your requirement.

Sample data would be understandable to help you.

regards,

Simha

Read only

Former Member
0 Likes
1,397

Hi...

Can u please give more description of your problem...?

Regards,

Chintan

Read only

Former Member
0 Likes
1,397

Hi Tashvi,

You can use DESCRIBE statement to achieve that.

Try this code to get the no of records ---

DATA : w_line type i.
DESCRIBE  TABLE  t_internal_table LINES w_line.

Here w_line variable contains the no of records .

This way you can get the no of records of the both cases.

Regards

Pinaki

Read only

Former Member
0 Likes
1,397

Tashvi,

Check the demo program demo_int_tables_describe_table

Hope that helps you

Thanks,

Babu Kilari

Read only

Former Member
0 Likes
1,397

Hi Tashvi,

Try out the below code. As per my understanding, this is the kind of output you need. I have used some test data in the internal table for field1 and field2. Substitute that with your actual data and check. Hope this helps you.

REPORT  sy-repid.

TYPES: BEGIN OF ty_tab,
         field1,
         field2 TYPE i,
       END OF ty_tab.

DATA: l_t_itab TYPE TABLE OF ty_tab,
      l_wa_itab TYPE ty_tab.

DATA: prev_field1,
      counter TYPE i.

DATA: case1 TYPE i,
      case2 TYPE i.

CLEAR l_wa_itab.
l_wa_itab-field1 = 'A'.
l_wa_itab-field2 = 10.
APPEND l_wa_itab TO l_t_itab.

CLEAR l_wa_itab.
l_wa_itab-field1 = 'A'.
l_wa_itab-field2 = 20.
APPEND l_wa_itab TO l_t_itab.

CLEAR l_wa_itab.
l_wa_itab-field1 = 'B'.
l_wa_itab-field2 = 11.
APPEND l_wa_itab TO l_t_itab.

CLEAR l_wa_itab.
l_wa_itab-field1 = 'E'.
l_wa_itab-field2 = 10.
APPEND l_wa_itab TO l_t_itab.

CLEAR l_wa_itab.
l_wa_itab-field1 = 'C'.
l_wa_itab-field2 = 22.
APPEND l_wa_itab TO l_t_itab.

CLEAR l_wa_itab.
l_wa_itab-field1 = 'D'.
l_wa_itab-field2 = 33.
APPEND l_wa_itab TO l_t_itab.

CLEAR l_wa_itab.
l_wa_itab-field1 = 'E'.
l_wa_itab-field2 = 20.
APPEND l_wa_itab TO l_t_itab.

prev_field1 = ''.

SORT l_t_itab BY field1.

LOOP AT l_t_itab INTO l_wa_itab.
  WRITE:/ l_wa_itab-field1 , l_wa_itab-field2.
  IF prev_field1 NE l_wa_itab-field1 AND prev_field1 IS NOT INITIAL.

    IF counter = 1.
      case1 = case1 + 1.
    ELSEIF counter > 1.
      case2 = case2 + 1.
    ENDIF.
    counter = 0.
  ENDIF.

  counter = counter + 1.

  prev_field1 = l_wa_itab-field1.

ENDLOOP.

IF counter = 1.
  case1 = case1 + 1.
ELSEIF counter > 1.
  case2 = case2 + 1.
ENDIF.

WRITE:/ 'Number of Records with Case1 (1 to 1)    = ' , case1.
WRITE:/ 'Number of Records with Case2 (1 to Many) = ' , case2.

Regards,

Himanshu

Read only

0 Likes
1,397

Hi all,

I am getting data in 10 files . I am appending 10 files in to a single file . Internal table will contain 2 fields.

Filed1 Field2

1 A

1 B

1 C

1 D

2 E

Based on Field1 value i have to check field2 . For Field1 value if i have many entries in Field2 like

1 -


A,B,C,D 1 to many values

2------ E 1 to 1 value .

I need a count of 1 to many values and count of 1 to 1 values .

Internal table will contain 250k records .

This is my code . some how unable to get the output . the code from 'DO' to 'ENDDO ' taking lot of time . please let me know to improve the preformance.

parameters: p_ofile like rlgrap-filename,

p_count type i.

select-options:p_ifile for p_ofile.

Data :begin of itab occurs 0,

LNRZE like lfb1-LNRZE,

lifnr like lfb1-lifnr,

end of itab.

Data:t_file like rlgrap-filename,

itab1 like itab occurs 0 with header line,

itab2 like itab1 occurs 0 with header line.

  • t_file = p_ifile.

data:

t_alsmex_tabline like

standard table

of alsmex_tabline

with header line.

data:

V_LINES TYPE I,

v_line type i,

v_lin type i,

v_single type i,

V_lines1 type i,

v_lines2 type i,

v_sindx LIKE sy-tabix VALUE '1',

v_eindx LIKE sy-tabix VALUE '50000'.

move 'Head Offic' to itab-lnrze.

Move 'vendor Num' to itab-lifnr.

append itab.

loop at p_ifile.

clear :t_file,t_alsmex_tabline.

Refresh:t_alsmex_tabline.

t_file = p_ifile-low.

call function 'ALSM_EXCEL_TO_INTERNAL_TABLE'

exporting

filename = t_file

i_begin_col = 1

i_begin_row = 1

i_end_col = 4

i_end_row = 65000

tables

intern = t_alsmex_tabline

exceptions

inconsistent_parameters = 1

upload_ole = 2

others = 3.

if sy-subrc = 0 .

loop at t_alsmex_tabline.

case t_alsmex_tabline-col.

when '0001'.

itab-lifnr = t_alsmex_tabline-value.

when '0002'.

itab-lnrze = t_alsmex_tabline-value.

itab1-lnrze = t_alsmex_tabline-value.

endcase.

at end of row.

if itab-lnrze is not initial.

append itab.

append itab1.

clear itab.

clear itab1.

endif.

endat.

endloop.

endif.

endloop.

sort itab by lnrze DESCENDING .

delete adjacent duplicates from itab comparing lnrze lifnr.

describe table itab lines v_lines2.

delete adjacent duplicates from itab1 comparing lnrze.

Do .

refresh :itab2 .

clear :itab2.

APPEND LINES OF itab1 FROM v_sindx TO v_eindx TO itab2.

loop at itab2.

read table itab with key lnrze = itab2-lnrze binary search.

if sy-subrc = 0.

loop at itab where lnrze = itab2-lnrze .

v_lines = v_lines + 1.

IF v_lines GT 1.

v_line = v_line + 1.

exit.

endif.

if itab-lifnr is initial.

v_lin = v_lin + 1.

endif.

endloop.

*

endif.

endloop.

DELETE itab1 FROM v_sindx TO v_eindx.

enddo.

v_lines1 = v_lines1 - 1.

v_single = v_lines1 - ( V_line + v_lin ).

WRITE : 'Total Number of Records: ' ,V_lines.

WRITE : 'Total Number of Records with one to many: ' ,V_single.

WRITE : 'Total Number of Records with one to many: ' ,V_line.

Please advice me.

Edited by: tashvi on Apr 28, 2009 6:02 AM

Read only

0 Likes
1,397

Hi,

To get the total number of one to many and 1 to One records, use AT END OF event in the loop.

in your case, use

AT END OF field1.

one_to_many = one_to_many + 1.

one_to_one = one_to_one + 1.

total_records = one_to_one + one_to_many.

ENDAT.

This will print the statistics as per the total number of records in the internal table. Please do not forget to initialize the fields to 0 during declaration.

ChK

Read only

Former Member
0 Likes
1,397

Hi Tashvi,

Please execute this code -


DATA : w_line TYPE i,
       BEGIN OF fs_itab ,
         fld1 TYPE c,
         fld2 TYPE c,
       END OF fs_itab,
       t_itab LIKE TABLE OF fs_itab,
       w_fld1 TYPE c.

fs_itab-fld1 = '1'.
fs_itab-fld2 = 'A'.
APPEND fs_itab TO t_itab.

fs_itab-fld1 = '1'.
fs_itab-fld2 = 'B'.
APPEND fs_itab TO t_itab.

fs_itab-fld1 = '1'.
fs_itab-fld2 = 'C'.
APPEND fs_itab TO t_itab.

fs_itab-fld1 = '1'.
fs_itab-fld2 = 'D'.
APPEND fs_itab TO t_itab.

fs_itab-fld1 = '2'.
fs_itab-fld2 = 'E'.
APPEND fs_itab TO t_itab.

fs_itab-fld1 = '3'.
fs_itab-fld2 = 'F'.
APPEND fs_itab TO t_itab.

fs_itab-fld1 = '3'.
fs_itab-fld2 = 'G'.
APPEND fs_itab TO t_itab.

fs_itab-fld1 = '3'.
fs_itab-fld2 = 'H'.
APPEND fs_itab TO t_itab.




SORT t_itab BY fld1.
LOOP AT t_itab INTO fs_itab.
  IF fs_itab-fld1 = w_fld1 OR sy-tabix EQ 1.
    w_line = w_line + 1.
  ELSE.
    WRITE: / 'For fld1 value',w_fld1,'there is',w_line,'value in field2'.
    w_line = 1.
  ENDIF.
  w_fld1 = fs_itab-fld1.
ENDLOOP.
WRITE: / 'For fld1 value',w_fld1,'there is',w_line,'value in field2'.

Read only

Former Member
0 Likes
1,397

simple and fast solution. after ur Internal table is filled with values.

sort itab by field1 field2.

loop at itab.

count = count + 1.

at end of field1.

if count > 1.

v_onetomany = v_onetomany + 1.

else.

v_onetoone = v_onetoone + 1.

endif.

clear count.

endat.

endloop.

Read only

Former Member
0 Likes
1,397

hi

try this

data: fval type itav-field1,

int1 type i,

int2 type i,

loop at itab in wa.

if wa-field1 = fval.

add 1 to int2.

else.

fval = wa-field1.

add 1 to int2.

add 1 to int1.

endif.

write: / 'No of entries in case one (1 to 1) = ', int1.

write: / 'No of entries in case two (1 to many) = ', int2.