Skip to content
Advertisement

How to retrieve all grand parents at once

I have two simple tables in SQLITE3 (family bonds):

“persons” (id, fname, lname)
“relationships” (parent/child)

I would like to get each grand children along with all their grand parents (from 1 to 4 of them depending on the grand child) so that 1 row result =

Thanks to Caius Jard, I’ve been able to get each child and its grand parents in another star overflow question. However, so far I have:
1 line = Grand child, 1 grand parent (so it needs up to 4 lines to get all grand parents of a child).

How could I edit this so that I get each grand child along with all the grand parents in one single row result?

If possible only using SELECT (+ count/distinct), WHERE (+in/exists), INNER/LEFT JOIN, GROUP BY (+having), ORDER BY.

Advertisement

Answer

Assuming there are four grandparents, you can enumerate them and aggregate:

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement