2009 Apr 27 11:07 PM
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?
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.
2009 Apr 27 11:09 PM
2009 Apr 28 2:47 AM
2009 Apr 28 4:19 AM
Can you elaborate more on your requirement.
Sample data would be understandable to help you.
regards,
Simha
2009 Apr 28 4:13 AM
Hi...
Can u please give more description of your problem...?
Regards,
Chintan
2009 Apr 28 4:20 AM
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
2009 Apr 28 4:41 AM
Tashvi,
Check the demo program demo_int_tables_describe_table
Hope that helps you
Thanks,
Babu Kilari
2009 Apr 28 4:50 AM
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
2009 Apr 28 5:00 AM
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
2009 Apr 28 5:16 AM
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
2009 Apr 28 8:15 AM
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'.
2009 Apr 28 8:44 AM
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.
2009 Apr 28 8:53 AM
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.
| User | Count |
|---|---|
| 3 | |
| 2 | |
| 2 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |