I have two tables table1 and table2. Both tables have the same structure. They have 4 columns (x1, y1, z1 and w1 in the first table and x2, y2, z2, w2, in the second table).
I want to extract values of the first column of the second table (x2). The procedure is as following:
- Extract all rows from the second table for which
y2=y0andw2=w0. - For every extracted row take values of the first three columns (
x_i,y_iandz_i). - From the first table extract all rows for which
x1==x_i and y1==y_i and z1==z_i(I know in advance that one or zero rows will be extracted). - For the extracted row (if there is one) check the value in the column
w1. If it is not equal tow0addx_ito the list. If there are no rows extracted from the first table, also addx_ito the list. - Repeat this procedure for all rows extracted from the second table on the step 1.
In the end I want to have a unique list of values from the x2 (no duplicates).
My question is if the described procedure can be performed by one command.
ADDED
w0 and y0 are known and fixed (just some fixed values).
You will need an idColumn on table1 that will only have not null values. It will be used to test that the left join (which represents condition number 3.) actually produced any results or not. If any of the table’s other columns does not allow null values, it can be used instead of idColumn.