10-31-2013 2:57 AM
Hello all,
I am new to ABAP development and I have this following question which might seem silly to experienced ABAPers
I have an internal table say itab1 with two fields say T and K.Sample data may be like
T K
t1 k1
t2 K2
t2 K3
t3 K2
t3 K1
t3 K3
t3 K1
t4 K3
(This table will not have duplicate rows since before this step I've already performed DELETE ADJACENT DUPLICATES FROM itab1 COMPARING T and K.)
I need the count of every unique T in another itab.So required output in another itab is
T Count
t1 1
t2 2
t3 3
t4 1
Thanks and regards.
10-31-2013 3:44 AM
Hi,
Here t_itab is the main table and t_fin contain the count.
LOOP AT t_itab INTO x_itab.
READ TABLE t_fin INTO x_fin WITH TABLE KEY t = x_itab-t.
x_fin-no = x_fin-no + 1.
IF sy-subrc EQ 0.
MODIFY t_fin FROM x_fin INDEX sy-tabix.
ELSE.
x_fin-t = x_itab-t.
APPEND x_fin TO t_fin.
ENDIF.
CLEAR x_fin.
ENDLOOP.
Regards,
Jeffin
10-31-2013 4:55 AM
Simple....Try like this
TYPES :BEGIN OF ty_final,
field1 TYPE char10,
field2 TYPE char10,
END OF ty_final.
TYPES :BEGIN OF ty_final_2,
field1 TYPE char10,
field2 TYPE i,
END OF ty_final_2.
DATA :it_final TYPE TABLE OF ty_final,
it_final2 TYPE TABLE OF ty_final_2,
wa_final TYPE ty_final,
wa_final2 TYPE ty_final_2.
DATA : count TYPE i,
flag TYPE char10.
PERFORM internal_table.
SORT it_final BY field1.
LOOP AT it_final INTO wa_final.
count = count + 1.
AT END OF field1.
wa_final2-field1 = wa_final-field1.
wa_final2-field2 = count.
APPEND wa_final2 TO it_final2.
CLEAR count.
ENDAT.
ENDLOOP.
CLEAR wa_final2.
LOOP AT it_final2 INTO wa_final2.
WRITE : / wa_final2-field1,
wa_final2-field2.
ENDLOOP.
*&---------------------------------------------------------------------*
*& Form INTERNAL_TABLE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM internal_table .
PERFORM wa_it USING :
't1' 'K1',
't2' 'K2',
't2' 'K3',
't3' 'K2',
't3' 'K1',
't3' 'K3',
't3' 'K1',
't4' 'K3'.
ENDFORM. " INTERNAL_TABLE
*&---------------------------------------------------------------------*
*& Form WA_IT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_ENDFORM text
*----------------------------------------------------------------------*
FORM wa_it USING v_field1 TYPE any
v_field2 TYPE any.
wa_final-field1 = v_field1.
wa_final-field2 = v_field2.
APPEND wa_final TO it_final.
CLEAR wa_final.
ENDFORM. " WA_IT
10-31-2013 3:44 AM
Hello Abhiroop,
This you can achieve by writing a simple piece of logic, as follows,
Before this code, just declare a variable called count of type 'i'
Logic:
Result:
Let me know if this is helpful.
Thanks and Regards
Nivash
10-31-2013 4:00 AM
Hi,
Follow the steps mentioned below.
1. Loop the first internal table
2. Perform a read statement on the second internal table.
3. If sy-subrc equals zero, then then add the count by one and modify the internal table.
4. If sy-subrc not equal to zero then set the count to one and append to the internal table.
LOOP AT t_itab INTO x_itab.
READ TABLE t_count INTO x_count WITH TABLE KEY t = x_itab-t.
IF sy-subrc EQ 0.
count = count + 1.
x_count-no = count.
MODIFY t_count FROM x_count INDEX sy-tabix.
ELSE.
count = 1.
x_count-no = count.
x_count-t = x_itab-t.
APPEND x_count TO t_count.
ENDIF.
ENDLOOP.
Regards,
Riju Thomas.
10-31-2013 4:29 AM
Thank you everyone for your answers.I'll try each way suggested and get back to you in case of further queries.
Regards.
10-31-2013 9:23 AM
Hello,
Just copy, paste and run.
REPORT z_ybi_count_of_unique_entry.
TYPES : BEGIN OF ty_table,
t TYPE string,
k TYPE string,
END OF ty_table.
TYPES : BEGIN OF ty_count,
t TYPE string,
count TYPE string,
END OF ty_count.
DATA gt_tab TYPE STANDARD TABLE OF ty_table.
DATA gs_tab TYPE ty_table.
DATA gs_tab1 TYPE ty_table.
DATA gt_count TYPE STANDARD TABLE OF ty_count.
DATA gs_count TYPE ty_count.
DATA l_count TYPE i.
CLEAR gs_tab.
gs_tab-t = 't1'.
gs_tab-k = 'k1'.
APPEND gs_tab TO gt_tab.
CLEAR gs_tab.
gs_tab-t = 't2'.
gs_tab-k = 'K2'.
APPEND gs_tab TO gt_tab.
CLEAR gs_tab.
gs_tab-t = 't2'.
gs_tab-k = 'K3'.
APPEND gs_tab TO gt_tab.
CLEAR gs_tab.
gs_tab-t = 't3'.
gs_tab-k = 'K2'.
APPEND gs_tab TO gt_tab.
CLEAR gs_tab.
gs_tab-t = 't3'.
gs_tab-k = 'K1'.
APPEND gs_tab TO gt_tab.
CLEAR gs_tab.
gs_tab-t = 't3'.
gs_tab-k = 'K3'.
APPEND gs_tab TO gt_tab.
CLEAR gs_tab.
gs_tab-t = 't3'.
gs_tab-k = 'K1'.
APPEND gs_tab TO gt_tab.
CLEAR gs_tab.
gs_tab-t = 't4'.
gs_tab-k = 'K3'.
APPEND gs_tab TO gt_tab.
SORT gt_tab BY t.
LOOP AT gt_tab INTO gs_tab.
LOOP AT gt_tab INTO gs_tab1 WHERE t = gs_tab-t.
l_count = l_count + 1.
gs_count-t = gs_tab1-t.
gs_count-count = l_count.
ENDLOOP.
APPEND gs_count TO gt_count.
CLEAR l_count.
ENDLOOP.
SORT gt_count BY t.
DELETE ADJACENT DUPLICATES FROM gt_count COMPARING t.
LOOP AT gt_count INTO gs_count.
WRITE : / gs_count-t,
gs_count-count.
ENDLOOP.
Thanks & Kind Regards,
Yovish.
11-01-2013 10:34 AM
Hi Abhiroop,
You can use the control break statement AT END OF to achieve this.
A simple code snippet attached below.
TYPES : BEGIN OF ty_input,
field1 TYPE char10,
field2 TYPE char10,
END OF ty_input.
TYPES : BEGIN OF ty_output,
field1 TYPE char10,
field2 TYPE i,
END OF ty_output.
DATA : it_input TYPE TABLE OF ty_input,
it_output TYPE TABLE OF ty_output,
wa_input TYPE ty_input,
wa_output TYPE ty_output.
DATA : count TYPE i.
wa_input-field1 = 't1'.
wa_input-field2 = 'K1'.
APPEND wa_input TO it_input.
CLEAR wa_input.
wa_input-field1 = 't2'.
wa_input-field2 = 'K2'.
APPEND wa_input TO it_input.
CLEAR wa_input.
wa_input-field1 = 't2'.
wa_input-field2 = 'K3'.
APPEND wa_input TO it_input.
CLEAR wa_input.
wa_input-field1 = 't3'.
wa_input-field2 = 'K2'.
APPEND wa_input TO it_input.
CLEAR wa_input.
wa_input-field1 = 't3'.
wa_input-field2 = 'K1'.
APPEND wa_input TO it_input.
CLEAR wa_input.
wa_input-field1 = 't3'.
wa_input-field2 = 'K3'.
APPEND wa_input TO it_input.
CLEAR wa_input.
wa_input-field1 = 't4'.
wa_input-field2 = 'K3'.
APPEND wa_input TO it_input.
CLEAR wa_input.
SORT it_input BY field1.
LOOP AT it_input INTO wa_input.
count = count + 1.
AT END OF field1.
wa_output-field1 = wa_input-field1.
wa_output-field2 = count.
APPEND wa_output TO it_output.
CLEAR count.
ENDAT.
ENDLOOP.
WRITE : / 'Input'.
SKIP 1.
LOOP AT it_input INTO wa_input.
WRITE : / wa_input-field1,
wa_input-field2.
ENDLOOP.
CLEAR wa_output.
SKIP 1.
WRITE: / 'Output'.
SKIP 1.
LOOP AT it_output INTO wa_output.
WRITE : / wa_output-field1,
wa_output-field2.
ENDLOOP.
Regards,
Shahanaz.
11-01-2013 2:47 PM
Hello Abhiroop,
You can just create another internal table (lets say tTable) with two fields
Then use the below code to create your intended internal table
Loop at itab1.
tTable-T = itab1-T.
tTable-Count = 1.
Collect tTable.
EndLoop.
-Amit.
11-01-2013 4:08 PM
11-01-2013 4:19 PM