Recursive subquery - create a group
Quote from kevync on 2024-06-05, 6:50 amHi
I have used a subquery to create group(s) before. (or views). The recursive query works to create a view. What I am trying to do is to do is get the syntax to insert the list into the tag table -- what am I missing?
-- subquery
WITH RECURSIVE '*Tree (SQL) Tony Vuksic'
(
constants(C_StartPerson, C_BirthParentOnly) AS (
SELECT 20112 AS C_StartPerson,
1 AS C_BirthParentOnly
),
cousin_of(CousinID) AS (
SELECT AncestorID FROM ancestor_of
UNION
SELECT ChildID FROM child_of
INNER JOIN cousin_of ON ParentID = CousinID
),
ancestor_of(AncestorID) AS (
SELECT ParentID FROM parent_of
WHERE ChildID=(SELECT C_StartPerson FROM constants)
UNION
SELECT ParentID FROM parent_of
INNER JOIN ancestor_of ON ChildID = AncestorID
),
parent_of(ChildID, ParentID) AS (
SELECT ct.ChildID, FatherID AS ParentID
FROM ChildTable AS ct
LEFT JOIN FamilyTable USING(FamilyID)
WHERE ParentID <> 0
AND CASE (SELECT C_BirthParentOnly FROM constants)
WHEN 1 THEN RelFather=0 ELSE 1
END
UNION
SELECT ct.ChildID, MotherID AS ParentID
FROM ChildTable AS ct
LEFT JOIN FamilyTable USING(FamilyID)
WHERE ParentID <> 0
AND CASE (SELECT C_BirthParentOnly FROM constants)
WHEN 1 THEN RelMother=0 ELSE 1
END
),
child_of(ParentID, ChildID) AS (
SELECT FatherID, ct.ChildID FROM FamilyTable
LEFT JOIN ChildTable as ct USING(FamilyID)
WHERE FatherID <> 0
AND CASE (SELECT C_BirthParentOnly FROM constants)
WHEN 1 THEN RelFather=0 ELSE 1
END
UNION
SELECT MotherID, ct.ChildID FROM FamilyTable
LEFT JOIN ChildTable as ct USING(FamilyID)
WHERE MotherID <> 0
AND CASE (SELECT C_BirthParentOnly FROM constants)
WHEN 1 THEN RelFather=0 ELSE 1
END
),
spouse_of(PersonID, SpouseID) AS (
SELECT ft.FatherID AS PersonID, ft.MotherID AS SpouseID
FROM FamilyTable AS ft
WHERE PersonID <> 0 AND SpouseID <> 0
UNION
SELECT ft.MotherID AS PersonID, ft.FatherID AS SpouseID
FROM FamilyTable AS ft
WHERE PersonID <> 0 AND SpouseID <> 0
),
cousin_spouse_of(SpouseID) AS (
SELECT SpouseID AS PersonID
FROM spouse_of AS so
INNER JOIN cousin_of AS co ON co.CousinID = so.PersonID
),
cousin_spouse_parent_of(ParentID) AS (
SELECT ParentID
FROM parent_of AS po
INNER JOIN cousin_spouse_of AS cso ON cso.SpouseID = po.ChildID
)
SELECT CousinID AS PersonID FROM cousin_of
UNION
SELECT SpouseID AS PersonID FROM cousin_spouse_of
UNION
SELECT ParentID AS PersonID FROM cousin_spouse_parent_of
)
-- Create Named Group if it does not exist *Tree (SQL) Tony Vuksic
INSERT OR IGNORE INTO TagTable
VALUES
(
(SELECT TagID FROM TagTable WHERE TagName LIKE '*Tree (SQL) Tony Vuksic')
,0
,(SELECT IFNULL(MAX(TagValue),0)+1 FROM TagTable)
,'*Tree (SQL) Tony Vuksic'
,'SQLite query'
,julianday('now') - 2415018.5
)
;-- Delete all members of the named group
DELETE FROM GroupTable
WHERE GroupID =
(SELECT TagValue
FROM TagTable a
WHERE TagName
LIKE '*Tree (SQL) Tony Vuksic'
)
;-- Tony Vuksic Tree
-- Add members to the named group via Recursive Method
Hi
I have used a subquery to create group(s) before. (or views). The recursive query works to create a view. What I am trying to do is to do is get the syntax to insert the list into the tag table -- what am I missing?
-- subquery
WITH RECURSIVE '*Tree (SQL) Tony Vuksic'
(
constants(C_StartPerson, C_BirthParentOnly) AS (
SELECT 20112 AS C_StartPerson,
1 AS C_BirthParentOnly
),
cousin_of(CousinID) AS (
SELECT AncestorID FROM ancestor_of
UNION
SELECT ChildID FROM child_of
INNER JOIN cousin_of ON ParentID = CousinID
),
ancestor_of(AncestorID) AS (
SELECT ParentID FROM parent_of
WHERE ChildID=(SELECT C_StartPerson FROM constants)
UNION
SELECT ParentID FROM parent_of
INNER JOIN ancestor_of ON ChildID = AncestorID
),
parent_of(ChildID, ParentID) AS (
SELECT ct.ChildID, FatherID AS ParentID
FROM ChildTable AS ct
LEFT JOIN FamilyTable USING(FamilyID)
WHERE ParentID <> 0
AND CASE (SELECT C_BirthParentOnly FROM constants)
WHEN 1 THEN RelFather=0 ELSE 1
END
UNION
SELECT ct.ChildID, MotherID AS ParentID
FROM ChildTable AS ct
LEFT JOIN FamilyTable USING(FamilyID)
WHERE ParentID <> 0
AND CASE (SELECT C_BirthParentOnly FROM constants)
WHEN 1 THEN RelMother=0 ELSE 1
END
),
child_of(ParentID, ChildID) AS (
SELECT FatherID, ct.ChildID FROM FamilyTable
LEFT JOIN ChildTable as ct USING(FamilyID)
WHERE FatherID <> 0
AND CASE (SELECT C_BirthParentOnly FROM constants)
WHEN 1 THEN RelFather=0 ELSE 1
END
UNION
SELECT MotherID, ct.ChildID FROM FamilyTable
LEFT JOIN ChildTable as ct USING(FamilyID)
WHERE MotherID <> 0
AND CASE (SELECT C_BirthParentOnly FROM constants)
WHEN 1 THEN RelFather=0 ELSE 1
END
),
spouse_of(PersonID, SpouseID) AS (
SELECT ft.FatherID AS PersonID, ft.MotherID AS SpouseID
FROM FamilyTable AS ft
WHERE PersonID <> 0 AND SpouseID <> 0
UNION
SELECT ft.MotherID AS PersonID, ft.FatherID AS SpouseID
FROM FamilyTable AS ft
WHERE PersonID <> 0 AND SpouseID <> 0
),
cousin_spouse_of(SpouseID) AS (
SELECT SpouseID AS PersonID
FROM spouse_of AS so
INNER JOIN cousin_of AS co ON co.CousinID = so.PersonID
),
cousin_spouse_parent_of(ParentID) AS (
SELECT ParentID
FROM parent_of AS po
INNER JOIN cousin_spouse_of AS cso ON cso.SpouseID = po.ChildID
)
SELECT CousinID AS PersonID FROM cousin_of
UNION
SELECT SpouseID AS PersonID FROM cousin_spouse_of
UNION
SELECT ParentID AS PersonID FROM cousin_spouse_parent_of
)
-- Create Named Group if it does not exist *Tree (SQL) Tony Vuksic
INSERT OR IGNORE INTO TagTable
VALUES
(
(SELECT TagID FROM TagTable WHERE TagName LIKE '*Tree (SQL) Tony Vuksic')
,0
,(SELECT IFNULL(MAX(TagValue),0)+1 FROM TagTable)
,'*Tree (SQL) Tony Vuksic'
,'SQLite query'
,julianday('now') - 2415018.5
)
;-- Delete all members of the named group
DELETE FROM GroupTable
WHERE GroupID =
(SELECT TagValue
FROM TagTable a
WHERE TagName
LIKE '*Tree (SQL) Tony Vuksic'
)
;-- Tony Vuksic Tree
-- Add members to the named group via Recursive Method
Quote from kevync on 2024-06-05, 6:56 amps. I suppose I could create a temp view from the recursive then do the group from the tempview but wonder if there was a way to use recursive in a subquery
ps. I suppose I could create a temp view from the recursive then do the group from the tempview but wonder if there was a way to use recursive in a subquery
Quote from Richard Otter on 2024-06-05, 11:17 amYou're trying to insert PersonID's into the TagTable?
Is your goal to create one SQL statementthat will generate the members and create the group in one shot?I might suggest my GroupFromSQL utility. It will do what you want without having to modify such large SQL statements.
You're trying to insert PersonID's into the TagTable?
Is your goal to create one SQL statementthat will generate the members and create the group in one shot?
I might suggest my GroupFromSQL utility. It will do what you want without having to modify such large SQL statements.
Quote from kevync on 2024-06-05, 11:33 amThanks- I have not taken a look at Python scripts. I am familiar with SQL -- just not recursive in a subquery. Though I have used over a dozen different subquery scripts to create/update groups
Thanks- I have not taken a look at Python scripts. I am familiar with SQL -- just not recursive in a subquery. Though I have used over a dozen different subquery scripts to create/update groups
Quote from Richard Otter on 2024-06-05, 11:47 amTo save some searching- here is the link-
https://github.com/ricko2001/Genealogy-scripts/releases/tag/GroupFromSQL_v1.1.0.0
Edit- I pointed to the wrong utility the first time
I do include the python script file in the zip, but it also has a ready to run exe file as well. You'll also need unifuz64.dll (Are you running MacOS? ? If so, I could use some help porting the script to a MacOS app....)
Regarding your question, I am leaving to the airport soon, so can't give an answer, but- start with the smallest recursive piece and get that to work first.
There's nothing really special about the recursive part, it's just that the query has many Common Table Expressions.
To save some searching- here is the link-
https://github.com/ricko2001/Genealogy-scripts/releases/tag/GroupFromSQL_v1.1.0.0
Edit- I pointed to the wrong utility the first time
I do include the python script file in the zip, but it also has a ready to run exe file as well. You'll also need unifuz64.dll (Are you running MacOS? ? If so, I could use some help porting the script to a MacOS app....)
Regarding your question, I am leaving to the airport soon, so can't give an answer, but- start with the smallest recursive piece and get that to work first.
There's nothing really special about the recursive part, it's just that the query has many Common Table Expressions.
Quote from kevync on 2024-06-05, 11:53 amThanks again Richard
the recursive part I tested works perfectly as I can create a list or view.
The subqueries I have used (not recursive ) in past to create groups also worked great.
The challenge is getting the recursive part with correct syntax in the subquery. Aso, I am running Windows.
Have a good trip!
Thanks again Richard
the recursive part I tested works perfectly as I can create a list or view.
The subqueries I have used (not recursive ) in past to create groups also worked great.
The challenge is getting the recursive part with correct syntax in the subquery. Aso, I am running Windows.
Have a good trip!