SQL - Crossing tables within tables

Asked

Viewed 46 times

1

I don’t know if the title is appropriate, but come on.

I have in Table A (table of consulted processes) two key fields (new process and old process) and I have in Table B my variable of interest (new process). In Table C, I have the conversion of the old process to the new one. Illustrating:

Table A
pcs_novo   pcs_antigo   outras_A 
A          NULL         X
B          NULL         X
NULL       AA           X
NULL       BB           X
C          NULL         X
A          NULL         X
NULL       AA           X

Table B
pcs_novo   assunto  
A          1           
B          2       
C          3
D          4
E          5
F          6

Table C
pcs_antigo  pcs_novo
AA          D
BB          E
CC          F

Naturally, I want to:

Final Table
    pcs_novo  pcs_antigo   assunto   outras_A
    A         NULL         1         X
    B         NULL         2         X
    D         AA           4         X
    E         BB           5         X
    C         NULL         3         X

How do I get it?

Notes: In Table A the process fields are repeated. In Table B and C the fields are unique.

  • Using joints. LEFT JOINS seem the most appropriate

  • How would I do that?

  • the structure does not seem to be correct, would have some model ER ?

  • @Yuricamarabatista: Is the database manager Mysql or SQL Server? Both are defined as tag.

  • 1

    SQL Server, I will fix, thank you

1 answer

1


Let’s start connecting the tables A and C, so we will definitely have a pcs_novo valid:

SELECT
    coalesce(A.pcs_novo, C.pcs_novo) as pcs_novo,
    A.pcs_antigo,
    A.outros_a
FROM A 
    LEFT JOIN C ON (A.pcs_antigo = C.pcs_antigo)

The coalesce will assure me that in case I don’t get a non-zero value of A.pcs_novo, he’s gonna get from C.pcs_novo.

The LEFT JOIN will ensure that every possible junction of A with C will be done, and all lines of A will be displayed, even those that do not have matching on C

We can take this result and join with the table B to take up the subject:

SELECT
    coalesce(A.pcs_novo, C.pcs_novo) as pcs_novo,
    A.pcs_antigo,
    B.assunto,
    A.outros_a
FROM A 
    LEFT JOIN C ON (A.pcs_antigo = C.pcs_antigo)
    LEFT JOIN B ON (coalesce(A.pcs_novo, C.pcs_novo) = B.pcs_novo)

Like the lines in A may have pcs_novo or pcs_antigo repeated, to take only the unique values for the processes, we can use the distinct not to catch the repetitions:

SELECT DISTINCT
    coalesce(A.pcs_novo, C.pcs_novo) as pcs_novo,
    A.pcs_antigo,
    B.assunto,
    A.outros_a
FROM A 
    LEFT JOIN C ON (A.pcs_antigo = C.pcs_antigo)
    LEFT JOIN B ON (coalesce(A.pcs_novo, C.pcs_novo) = B.pcs_novo)
  • 1

    I’ll try and give you some feedback!

  • There will be some repetitions because at the time of writing of my reply I had not seen that there could be repetition in the table A. I’ll put the distinct to ensure that no unwanted repetition appears

  • 1

    no problem, I missed even I make it clear in the question.

  • I’m almost there, Jefferson. There’s an issue I’m having trouble solving on account of the coalition. My pcs_new field is actually two fields (ano_pcs_new and nro_pcs_new). At first I did not put this in the topic because I thought that any answer could be adapted with a simple "and" in the conditions (ex: LEFT JOIN C on (A.ano_new = B.ano_new and A.pcs_new = B.pcs_new). But in the coalition I could not yet adapt. As I could adapt in the case?

  • Non-performation method: concatenating the information as strings. More performatic method: coalesce of ano_novo equal to C.ano_new AND coalesce of nro_pcs_novo same as C.nro_new. The joining of A with C must have no secret

  • When I’m on the computer again I update to with the updated question data

  • 1

    It worked out! Thank you very much!

Show 2 more comments

Browser other questions tagged

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