Expensive,
I have a table of "processes requested for consultation" and two tables with information of the processes registered (the "new" processes and the "old" processes). My intention is to bring the information contained in the process registers to the consulted process table, especially the "subject" column.
Although it is a simple question, Table A has a major flaw: some old processes that have already been migrated to "new processes" do not appear in Table A (query), which forces me to have to resort for a "Table D", of converting old processes to new ones, in order to extract the much desired subject contained in Table B. In addition, my table cross key are two fields: the year and process number - since the process number may repeat itself in different years, but never in the same year.
Exemplifying my problem:
Tabela A (processos consultados)
ano_pcs_novo nro_pcs_novo pcs_antigo outras_A
A a NULL X
B b NULL X
NULL NULL AAA X
NULL NULL BBB X
C c NULL X
A a NULL X
NULL NULL DDD X
Tabela B (cadastro dos processos novos)
ano_pcs_novo nro_pcs_novo assunto outras_B
A a 1 Y
B b 2 Y
C c 3 Y
D d 4 Y
E e 5 Y
F f 6 Y
Tabela C (cadastro dos processos antigos)
pcs_antigo outras_C
AAA Z
BBB Z
CCC Z
DDD Z
Tabela D (conversão de processos antigos para novos)
pcs_antigo ano_pcs_novo nro_pcs_novo
AAA A a
BBB B b
CCC C c
DDD D d
I wish:
Tabela A
ano_pcs_novo nro_pcs_novo pcs_antigo assunto outras_A outras_B outras_C
A a NULL 1 X Y Z
B b NULL 2 X Y Z
A a AAA 1 X Y Z
B b BBB 2 X Y Z
C c NULL 3 X Y Z
A a NULL 1 X Y Z
D d DDD 4 X Y Z
Comments: In Table A, the query fields are repeated. In Table B, C and D the fields are unique. Not all old processes have been converted.
How can I proceed? With community help, the closest I got involved using Select Distinct, Coalesce and Left Join, but as my main key here are two columns (ano_pcs_new and nro_pcs_new) I could not go any further. I guess I have to use concatenation, but I was not able to query properly.