2006 Mar 23 3:11 PM
Hi
I have a short question.
What is the best way to split a table into sub-tables of the same structure where each sub-tables has the same value for an element X.
<i><b>I forgot to mention:
the amount of values for element X vary and are unknown beforehand!</b></i>
Thanks for your ideas.
Greetings,
Nana<b></b><i></i><b></b>
Message was edited by: Nana Lohmanns
2006 Mar 23 4:10 PM
Hi, first thanks for all the help but I think maybe I need to be more specific...
I have a table with a field USERID. For each USERID there might be several lines with the same structure but except of USERID with different data.
I want to get a loop function that moves all lines associated to one USERID into a sub-table which I can use for processing. After processing the loop function is supposed to jump to the next USERID and create a sub-table containing the associated data again, until all USERIDs have been processed.
As such I am looking for a way in order to loop through a table and split it into sub-tables by the field USERID.
Anybody got a 10 point idea for this one?
Thanks,
Nana
Hi
I have a short question.
What is the best way to split a table into sub-tables of the same structure where each sub-tables has the same value for an element X.
<i><b>I forgot to mention:
the amount of values for element X vary and are unknown beforehand!</b></i>
Thanks for your ideas.
Greetings,
Nana<b></b><i></i><b></b>
Message was edited by: Nana Lohmanns
2006 Mar 23 3:14 PM
u have the internal table. say it1.
now, create tables with same structure as above. say it2, it3, it4.
now, u need to have same value for element x.
so, u need to do it as
it2[] = it3[] = it4[] = it1[].
this solves ur problem.
2006 Mar 23 3:19 PM
Declare sub internal tables with same structure
Eg - ITAB is main internal table. IT1, IT2, IT3, IT4 are sub internal tables.
LOOP AT ITAB.
IF ITAB-ELEMENT = 'X'.
IT1 = ITAB.
APPEND IT1.
CLEAR IT1.
ENDIF.
IF ITAB-ELEMENT = 'Y'.
IT2 = ITAB.
APPEND IT2.
CLEAR IT2.
ENDIF.
IF ITAB-ELEMENT = 'Z'.
IT3 = ITAB.
APPEND IT3.
CLEAR IT3.
ENDIF.
IF ITAB-ELEMENT = 'W'.
IT4 = ITAB.
APPEND IT4.
CLEAR IT4.
ENDIF.
ENDLOOP.
2006 Mar 23 3:43 PM
Hi Nana,
if you want to split a table into sub-tables ..
you can do this..
assume having four tables of the same type.. tbl1
tbl2 tbl3 and tbl from which data is fetched..
data : it_1 like table of tbl with header line.
data : it_2 like table of tbl with header line.
data : it_3 like table of tbl with header line.
data : it_4 like table of tbl with header line.
select * from tbl into table it_1.
loop at it_1 where <f> = val1.
move it_1 to it_2.
append it_2.
endloop.
loop at it_1 where <f> = val2.
move it_1 to it_3.
append it_2.
endloop.
loop at it_1 where <f> = val3.
move it_1 to it_4.
append it_2.
endloop.
modify tbl1 from table it_2.
modify tbl1 from table it_3.
modify tbl1 from table it_4.
regards
satesh
2006 Mar 23 4:10 PM
Hi, first thanks for all the help but I think maybe I need to be more specific...
I have a table with a field USERID. For each USERID there might be several lines with the same structure but except of USERID with different data.
I want to get a loop function that moves all lines associated to one USERID into a sub-table which I can use for processing. After processing the loop function is supposed to jump to the next USERID and create a sub-table containing the associated data again, until all USERIDs have been processed.
As such I am looking for a way in order to loop through a table and split it into sub-tables by the field USERID.
Anybody got a 10 point idea for this one?
Thanks,
Nana
2006 Mar 23 4:20 PM
You can achieve this requirement using CONTROL BREAK Statements.
My understanding is you need to store all entries related to 1 user id in one sub internal table.....right?
How many user ids you will have?
You can sort main internal table.
sort itab by userid. (This should be first field in internal table)
Loop at itab.
at new userid.
refresh table
endat.
itab1 = itab.
append itab1.
clear itab1.
at end of userid.
populate su internal table for userid using ITAB1.
Refresh ITAB1 for storing next userid data.
endat.
endloop.
2006 Mar 23 4:22 PM
HI Nana,
Is this what you ask for..
loop at itab.
move itab to it1.
append it1.
at end of userid.
*move to sub table.
it2[] = it1[].
*clears the it1 for new userid
refresh it1.
append it2.
endat.
*now use it2.
endloop.
regards
satesh
2006 Mar 23 4:33 PM
try this workaround...
append lines of itab to itab1 " appending all entries into another table"
loop at itab.
at new userid.
g_count = sy-tabix.
loop at itab1 where userid = itab-userid.
case g_count.
when '1'.
move itab1 to subtab1.
append subtab1.
clear subtab1.
when '2'.
move itab1 to subtab2.
append subtab2.
clear subtab2.
when '3'.
move itab1 to subtab3.
append subtab3.
clear subtab3. " n so on
endloop.
endloop.
doesnt seem very efficient but hope it gives a little idea..
Regards,
Bikash
2006 Mar 23 4:37 PM
Hi Nana,
it is moving thru all ids..
when a new userid is upcoming at end of userid is triggered..
for every new userid it1 is cleared .. which means it has fields for only one value of the userid.. which is moved to it2..
hope you got it..
loop at itab.
move itab to it1.
append it1.
at end of userid.
*move to sub table.
it2[] = it1[].
*clears the it1 for new userid
refresh it1.
append it2.
endat.
*now use it2.
endloop.
regards
satesh
2006 Mar 23 4:40 PM
Is this what you are looking for?
unique_users_itab[] = all_users_itab[].
SORT unique_users_itab BY uname.
DELETE ADJACENT DUPLICATES FROM unique_users_itab COMPARING uname.
LOOP AT unique_users_itab.
LOOP AT all_users_itab WHERE uname = unique_users_itab-uname.
*-- do whatever
ENDLOOP.
ENDLOOP.
2006 Mar 23 4:28 PM
Sounds like what I was looking for, but I can't try it right now.....
Will it move through all ID's in the list?
(Where will the loop resume?)
I will try it when I get home....
Thanks a lot,
Nana
| User | Count |
|---|---|
| 3 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |