2
Expensive,
I have a table of "processes requested for consultation" and two tables with information of registered processes (the "new" processes and the "old" processes). My intention is to bring the information contained in the registers of the processes to the table of consulted processes, in particular the column "subject".
Despite being a simple question, Table A has a major flaw: some old processes, which have already been migrated to "new processes", do not appear in Table A (query), which forces me to have to resort to a "Table D"of converting old processes to new ones, thus extracting the much desired subject contained in Table B. In addition, my table crossing key are two fields: the year and number of the process - since the process number can be repeated in different years, but never in a single 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 consulted process 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 help from the community, 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 couldn’t go any further. I imagine I have to use concatenate, but I couldn’t perform the query properly.
Table D has lines for all old processes?
– José Diz
It does not, but most are converted. But it is not 1:1 in this case.
– Yuri Camara Batista
complicated a little more q yesterday né amigo...rsrs put the code that is already using rsrs
– Rovann Linhalis
Yeah, I noticed that yesterday’s question didn’t reveal the whole problem...
– Yuri Camara Batista
if you can, put in Sqlfiddle, it helps a lot
– Rovann Linhalis
@Yuricamarabatista: there are no columns of common content in tables B and C, and then the content would have or would come from B or C? For example, there is the column nameReferent in the process, both in the old and new form, in table A should be placed or the contents of table B (if it is a new process) or table C (if it is an old process)?
– José Diz