简体   繁体   中英

Combining data from Two Identical tables using SQL query

We have a few different tables with identical structure. The goal is to take data from each one of them daily into one master table.

Along the way we need to add two columns too one for serial other for keeping the track of which table this record came from (eg table1, table2 etc)

There is a common unique column like CustomerID to join these table. What do you think would be the best approach.

I know nothing about tags you used, only some Oracle SQL.

The way you described it, it looks as if UNION might help.

Create a view as

create or replace view v_data as
  select 'tab 1' source_table, 
         id, 
         name, 
         address
    from table_1
  union
  select 'tab 2' source_table, 
         id, 
         name, 
         address
    from table_2;

You said that you need a "serial" number; you'd use something like a "sequence" (in Oracle, it is an object that - generally speaking - returns distinct value each time you fetch from it):

create sequence seq_tab;

Finally, to insert rows into a "master" table, you'd

insert into master
  (source_table,
   serial,
   id,
   name,
   address
  )
select v.source_table,
       seq_tab.nextval,
       v.id,
       v.name,
       v.address
from v_data v
where (v.source_table, v.id) not in (select a.source_table, a.id
                                     from master a
                                    )

where clause is used to skip rows which are already inserted into the master table.


Such a principle should work. Syntax you need might be different, as well as source for the serial column (I don't know whether your database supports sequences, or offers something similar).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM