‎2010 Apr 27 9:10 AM
Hi All,
My requirement is simple.
I want a Z table which combines the distinct Cost Center and Employee Pernr combination from two tables into one ztable.
e.g.
table 1..................................
Cost Center Pernr
A emp1
A emp2
A emp3
B emp1
C emp3
table 2.................................
Cost Center Pernr
A emp3
B emp1
B emp2
B emp3
Resultant ZTable.................
Cost Center Pernr
A emp1
A emp2
A emp3
B emp1
B emp2
B emp3
C emp3
In SQL a simple UNION clause will do the trick, but in ABAP i am not able to achieve it.
Please advise.
‎2010 Apr 28 8:46 AM
Hi Saqi....
types : begin of ty_tab,
cc type string,
emp type string,
end of ty_tab.
data : it_tab1 type table of ty_tab.
data : it_tab2 type table of ty_tab.
data : it_result type table of ty_tab.
data : wa_tab type ty_tab.
clear wa_tab.
wa_tab-cc = 'A'.
wa_tab-emp = 'emp1'.
append wa_tab to it_tab1.
clear wa_tab.
wa_tab-cc = 'A'.
wa_tab-emp = 'emp2'.
append wa_tab to it_tab1.
clear wa_tab.
wa_tab-cc = 'A'.
wa_tab-emp = 'emp3'.
append wa_tab to it_tab1.
clear wa_tab.
wa_tab-cc = 'B'.
wa_tab-emp = 'emp1'.
append wa_tab to it_tab1.
clear wa_tab.
wa_tab-cc = 'C'.
wa_tab-emp = 'emp3'.
append wa_tab to it_tab1.
***********
clear wa_tab.
wa_tab-cc = 'A'.
wa_tab-emp = 'emp3'.
append wa_tab to it_tab2.
clear wa_tab.
wa_tab-cc = 'B'.
wa_tab-emp = 'emp1'.
append wa_tab to it_tab2.
clear wa_tab.
wa_tab-cc = 'B'.
wa_tab-emp = 'emp2'.
append wa_tab to it_tab2.
clear wa_tab.
wa_tab-cc = 'B'.
wa_tab-emp = 'emp3'.
append wa_tab to it_tab2.
clear wa_tab.
loop at it_tab1 into wa_tab.
append wa_tab to it_result.
clear wa_tab.
endloop.
clear wa_tab.
loop at it_tab2 into wa_tab.
append wa_tab to it_result.
clear wa_tab.
endloop.
sort it_result by cc emp.
delete adjacent duplicates from it_result comparing cc emp.
clear wa_tab.
loop at it_result into wa_tab.
write : wa_tab-cc.
write : wa_tab-emp.
clear wa_tab.
endloop.
Hope this above code helps you...
It is able to create a union of the two tables...
Please revert back in case of any issues...
‎2010 Apr 27 10:52 AM
hi, try the following
insert ztable from table table1.
insert ztable from table table2.
or
collect both the 2 table values in on iternal table and insert it.
insert ztable from table table3.
Thanks & Regards,
Vallamuthu.M
‎2010 Apr 28 7:04 AM
can u give an example how to do this:
"collect both the 2 table values in on iternal table and insert it."
thanks..
‎2010 Apr 27 11:10 AM
>
> In SQL a simple UNION clause will do the trick, but in ABAP i am not able to achieve it.
>
> Please advise.
Make n SQL statements, the first with inserting into tables, the other with appending. See onloiune help on howto select vakues into internal tables.
‎2010 Apr 28 8:46 AM
Hi Saqi....
types : begin of ty_tab,
cc type string,
emp type string,
end of ty_tab.
data : it_tab1 type table of ty_tab.
data : it_tab2 type table of ty_tab.
data : it_result type table of ty_tab.
data : wa_tab type ty_tab.
clear wa_tab.
wa_tab-cc = 'A'.
wa_tab-emp = 'emp1'.
append wa_tab to it_tab1.
clear wa_tab.
wa_tab-cc = 'A'.
wa_tab-emp = 'emp2'.
append wa_tab to it_tab1.
clear wa_tab.
wa_tab-cc = 'A'.
wa_tab-emp = 'emp3'.
append wa_tab to it_tab1.
clear wa_tab.
wa_tab-cc = 'B'.
wa_tab-emp = 'emp1'.
append wa_tab to it_tab1.
clear wa_tab.
wa_tab-cc = 'C'.
wa_tab-emp = 'emp3'.
append wa_tab to it_tab1.
***********
clear wa_tab.
wa_tab-cc = 'A'.
wa_tab-emp = 'emp3'.
append wa_tab to it_tab2.
clear wa_tab.
wa_tab-cc = 'B'.
wa_tab-emp = 'emp1'.
append wa_tab to it_tab2.
clear wa_tab.
wa_tab-cc = 'B'.
wa_tab-emp = 'emp2'.
append wa_tab to it_tab2.
clear wa_tab.
wa_tab-cc = 'B'.
wa_tab-emp = 'emp3'.
append wa_tab to it_tab2.
clear wa_tab.
loop at it_tab1 into wa_tab.
append wa_tab to it_result.
clear wa_tab.
endloop.
clear wa_tab.
loop at it_tab2 into wa_tab.
append wa_tab to it_result.
clear wa_tab.
endloop.
sort it_result by cc emp.
delete adjacent duplicates from it_result comparing cc emp.
clear wa_tab.
loop at it_result into wa_tab.
write : wa_tab-cc.
write : wa_tab-emp.
clear wa_tab.
endloop.
Hope this above code helps you...
It is able to create a union of the two tables...
Please revert back in case of any issues...
‎2010 Apr 28 1:07 PM
you can take the values of the it_result into your resultant ztable like...
ztable [] = it_result [] .
‎2010 Apr 28 1:55 PM
refresh table_3.
append lines of table_1 to table_3.
append lines of table_2 to table_3.
sort table_3 by CostCenter Pernr.
delete adjacent duplicates from table_3 comparing CostCenter Pernr.