Add table B row to table A based on conditions

Asked

Viewed 70 times

1

I have two tables of data, being them: 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 line occurs more in B than 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

You can achieve this with an sql query?

  • Are each of the table elements in a different column? For example, the first row of the table A, "2 4 1 3", are 4 different columns?

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 above code lists the first part of which should be added in A.
Depending on the database manager it is not necessary to use the DISTINCT clause.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.