My idea is similar to idea by @Sorack, but I am trying to do without joining by the name of the parents. In this case, I define a IRMANDADE
(name borrowed from proposal by @Alexandrecavaloti) and then together with the student, returning a brotherhood code arbitrary.
I have not tried to treat for the generalized case, where brotherhood is defined by a generalized graph, and clicks are always siblings who share both parents. The solution proposed here offers the correct answer to sibling clicks, other graph formats can bring weird answers.
Determining the click code
Since all siblings are linked, using one of the siblings' codes as click code ensures that the codes between a sibling grouping will never conflict with the code of another sibling grouping. To always pick up the same code at the click, sets it to be the smallest student code belonging to the click.
Thus, we have the following brotherhoods:
COD_ALUNO | COD_IRMANDADE
1 | 1
2 | 1
3 | 1
4 | 4
5 | 4
7 | 7
8 | 7
11 | 11
12 | 11
Building a click
Since we are assuming that the graph is a set of disconnected clicks, detecting each of these clicks is just making a table join ALUNOS_IRMAOS
with itself (more about auto joining here and here).
SELECT
AI1.COD_ALUNO,
CASE
WHEN AI1.COD_ALUNO < min(AI2.COD_ALUNO_IRMAO)
THEN AI1.COD_ALUNO
ELSE min(AI2.COD_ALUNO_IRMAO)
END AS COD_IRMANDADE
FROM
ALUNOS_IRMAOS AI1
LEFT JOIN ALUNOS_IRMAOS AI2
ON AI1.COD_ALUNO = AI2.COD_ALUNO
GROUP BY AI1.COD_ALUNO
Note that the case
and the min
ensure that the COD_IRMANDADE
always be as small as possible for the click.
Entire consultation
I can take the brotherhood click query and use it in the final query as a CTE or as a sub-query. I find it more elective as CTE:
WITH IRMANDADE AS (
SELECT
AI1.COD_ALUNO,
CASE
WHEN AI1.COD_ALUNO < min(AI2.COD_ALUNO_IRMAO)
THEN AI1.COD_ALUNO
ELSE min(AI2.COD_ALUNO_IRMAO)
END AS COD_IRMANDADE
FROM
ALUNOS_IRMAOS AI1
LEFT JOIN ALUNOS_IRMAOS AI2
ON AI1.COD_ALUNO = AI2.COD_ALUNO
GROUP BY AI1.COD_ALUNO
)
SELECT
A.COD_ALUNO, A.NM_ALUNO, A.NM_PAI, A.NM_MAE, I.COD_IRMANDADE
FROM ALUNOS A
LEFT JOIN IRMANDADE I
ON A.COD_ALUNO = I.COD_ALUNO
Upshot:
COD_ALUNO | NM_ALUNO | NM_PAI | NM_MAE | COD_IRMANDADE
1 | GABRIEL | SERGIO | CELIA | 1
2 | VITOR | SERGIO | CELIA | 1
3 | GEOVANNE | SERGIO | CELIA | 1
4 | BRUNO | WAGNER | PAULA | 4
5 | PEDRO | WAGNER | PAULA | 4
6 | LARISSA | TIAGO | LAURA | (null)
7 | GRAÇA | PEDRO | ISADORA | 7
8 | MELISSA | PEDRO | ISADORA | 7
9 | ENZO | RAFAEL | CAROLINE | (null)
10 | RAFAEL | RAFAEL | CELIA | (null)
11 | MARIANE | DANIEL | MAITE | 11
12 | TATIANE | DANIEL | MAITE | 11
13 | MARIA | RODOLFO | DANIELA | (null)
Realize that even the null
s that you had predicted in the desired response occur here. To catch consecutive sorority rates, you would need to use a ROW_NUMBER
on the TEC IRMANDADE
.
Behold running on Sqlfiddle
What if Gabriel was Vitor’s brother, Vitor was Geovanne’s brother, but Geovanne and Gabriel weren’t brothers? Vitor would belong to two unique brother codes (one that would be shared with Gabriel and the other with Geovanne)?
– Alisson
Yes As in the case of the sibling table is: Gabriel to Victor ; Gabriel to Geovanne; Victor to Gabriel ; Victor to Geovanne; Geovanne to Gabriel ; and Geovanne to Vitor ; The relation that exists in the database is this. I still don’t know the business rules of the programs that feed this bank, so I’m not sure if it’s valid for half brothers. but for now I want it this way, counted with half-brothers.
– Gabriel Heguedusch
Ignore how your table is currently, I created a hypothetical situation to better understand how to assemble the query. Unless you tell me it will never exist, but still the ideal would be for the consultation to predict this situation.
– Alisson
now you got me, I can’t predict everything from this platform, but for now I wanted to get it the way it is, all brothers connected the same is in the table I created on the link.
– Gabriel Heguedusch
Okay, one more thing. You tagged
sql-server
but made a Fiddle using Mysql. Which of the two you actually need?– Alisson
Here is SQL Server, but it has no problem being in MYSQL.
– Gabriel Heguedusch
You have a table of "responsible" or "parent" are student table columns , in the second case I would revert the model , but anyway a subselect resolve : select * from student a Where exists (select null from student a2 Where a2.nm_parent = a1.nm_parent or a2.nm_mae = a1.nm_mae)
– Motta
I have no table of guardians, they are only columns. And it can happen that the student does not have parents. or not have one of the parents, etc.
– Gabriel Heguedusch
Has some problem the
COD_UNICO_DOS_IRMAIS
not be consecutive?– Jefferson Quesado