-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetAllChildrenWithPartnerFromBothPartners.sql
60 lines (33 loc) · 1.6 KB
/
GetAllChildrenWithPartnerFromBothPartners.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
DELIMITER $$
CREATE DEFINER=`root`@`%` PROCEDURE `GetAllChildrenWithPartnerFromBothPartners`(IN `Father` int(11), IN `Mother` int(11))
SQL SECURITY INVOKER
COMMENT 'Gets all children from a specific father and/or mother and show'
BEGIN
SELECT DISTINCT
CONCAT(Children.PersonGivvenName, ' ', Children.PersonFamilyName) AS 'Naam kind',
Partner.PartNaam AS 'Naam partner van kind'
FROM relations AllRelations
# Get all records from RELATIONS from specific persons who are either father or mother
INNER JOIN relationnames IsChild
ON AllRelations.RelationName = IsChild.RelationNameID
AND (IsChild.RelationnameName = 'Vader'
OR IsChild.RelationnameName = 'Moeder')
AND (AllRelations.RelationWithPerson = Father
OR AllRelations.RelationWithPerson = Mother)
# Get all the children from these specific persons who are either father or mother
INNER JOIN persons Children
ON AllRelations.RelationPerson = Children.PersonID
#Get all partners of the above found children
LEFT JOIN (SELECT
AllPartners.RelationPerson AS LinkToPartner,
CONCAT(Partner.PersonGivvenName, ' ', Partner.PersonFamilyName) AS PartNaam
FROM relations AllPartners
INNER JOIN relationnames IsPartner
ON AllPartners.RelationName = IsPartner.RelationnameID
AND IsPartner.RelationnameName = 'Partner'
INNER JOIN persons Partner
ON AllPartners.RelationWithPerson = Partner.PersonID) AS Partner
ON Children.PersonID = Partner.LinkToPartner
ORDER BY Children.PersonDateOfBirth;
END$$
DELIMITER ;