user11823122
user11823122

Reputation: 99

One Select from five tables

There are 5 types of web services S1,S2,S3,S4,S5 that store their logs in separate tables T1,T2,T3,T4,T5. Logs of one service cannot get to the table of logs of another service. There is a program that collects all these logs and generates one common log for all services. There is a temporary table Temp_Tab, which can contain all logs from all tables. So, in this program now to collect all the logs wrote 5 separate select, and this as you know, takes a very long time. I want instead of these 5 select to write 1 select statement (Well, a maximum of 2) to shorten. Suggested that with OUTER JOIN-Ohm, but in JOIN-e necessary "common fields" to substitute after ON to specify the relationship. And in tables T1,T2,T3,T4,T5 there is nothing in common and can not be, because there are logs on the operation of a particular service.

Upvotes: 0

Views: 103

Answers (1)

peterulb
peterulb

Reputation: 2988

Check out SELECT - UNION. Your target itab needs all relevant fields of course, or you use CORRESPONDING (if you specified additional fields in your select clause) or inline declaration with @DATA(). If required, the column names can be modified using aliases with AS.

Example:

SELECT a_id as id, a_name as name, a_descr as descr, a_value as value
  FROM tab_a
UNION ALL
SELECT b_id as id, b_name as name, b_descr as descr, b_value as value
  FROM tab_b
UNION ALL
SELECT c_id as id, c_name as name, c_descr as descr, c_value as value
  FROM tab_c
INTO TABLE @lt_temp_tab.

You can skip the as part if they have the same field name. Your text says so, your image doesn't.

Upvotes: 3

Related Questions