Add row from table B to table A based on conditions

1

I have two tables of data, they are: table A and table B. I want to join the rows of table B with A under the following conditions:

If a line occurs in B and not in A, add it to A. If a row occurs more in B than occurs in A, add it to A until it occurs as often in A as in B. If I have the following two tables:

TabelaA TabelaB 2 4 1 3 2 2 2 1 2 2 2 1 2 4 1 3 4 5 2 4 2 4 1 3 1 4 2 5 2 4 1 3 2 4 1 3 5 3 2 3

I want to have the following table:

Tabela A + B 2 2 2 1 2 4 1 3 2 4 1 3 2 4 1 3 4 5 2 4 1 4 2 5 5 3 2 3

Is it possible to achieve this with an sql query?

    
asked by anonymous 12.09.2018 / 21:12

1 answer

0
  

If a line occurs in B and not in A, add it to A.

You solve this with set theory. Just do

A U (B - A)

which is implemented in SQL by

-- código #1
SELECT distinct A1, A2, A3, A4 from B
minus 
SELECT distinct B1, B2, B3, B4 from A;

The code above lists the first part of what should be added in A.
Depending on the database manager it is not necessary to use the DISTINCT clause.

    
22.09.2018 / 22:31