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

Implement a UNION clause

Former Member
0 Likes
784

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.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
748

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...

6 REPLIES 6
Read only

vallamuthu_madheswaran2
Active Contributor
0 Likes
748

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

Read only

0 Likes
748

can u give an example how to do this:

"collect both the 2 table values in on iternal table and insert it."

thanks..

Read only

rainer_hbenthal
Active Contributor
0 Likes
748

>

> 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.

Read only

Former Member
0 Likes
749

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...

Read only

Former Member
0 Likes
748

you can take the values of the it_result into your resultant ztable like...

ztable [] = it_result [] .

Read only

Maciej_DomagaBa
Contributor
0 Likes
748

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.