Re: Reporting by family tree

From: Ibrahim Shaame <ishaame(at)gmail(dot)com>
To: swastik Gurung <gurung_swastik(at)yahoo(dot)com>
Cc: "pgsql-novice(at)lists(dot)postgresql(dot)org" <pgsql-novice(at)lists(dot)postgresql(dot)org>
Subject: Re: Reporting by family tree
Date: 2023-10-05 14:48:56
Message-ID: CAJOWwD7YmD8jPediD7qG49vq9mtTbJOx3eAV4a9234RTJe_aHw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Swastik, thank you for the response. I started there, and have been stuck
for many months now. I managed to get only father-child did not get
further. Here is the example sql where I get father-child results.
With this code:
WITH RECURSIVE ukoo AS (
SELECT namba,
jina,
baba,
babu,
nasaba_1,
daraja
FROM majina2
WHERE majina2.nasaba_1 IN (SELECT DISTINCT namba FROM majina2)

UNION ALL

SELECT mtoto.namba,
mtoto.jina,
mtoto.baba,
mtoto.babu,
mtoto.nasaba_1,
daraja
FROM majina2 mtoto
WHERE mtoto.nasaba_1 NOT IN (SELECT DISTINCT namba FROM majina2)

)

SELECT g.jina AS jina_la_mtoto,
g.baba AS baba_wa_mtoto,
g.babu AS babu_wa_mtoto,
g.namba,
mzazi.jina AS jina_la_mzazi,
mzazi.baba AS jina_la_baba_la_mzazi,
g.daraja
FROM ukoo g
JOIN majina2 mzazi
ON g.namba = mzazi.namba
ORDER BY g.namba;

I get:

jina_la_mtoto baba_wa_mtoto babu_wa_mtoto namba jina_la_mzazi
jina_la_baba_la_mzazi
daraja
---------------+---------------+---------------+--------+---------------+-----------------------+--------

Ibrahim Khamis Haji 100001 Ibrahim Khamis 6
Asia Khamis Haji 100002 Asia Khamis 6
Zubeir Khamis Haji 100003 Zubeir Khamis 6
Asha Mwinyi Bakari 100004 Asha Mwinyi 6
Mariama Mwinyi Bakari 100005 Mariama Mwinyi 6
Zainab Ibrahim Khamis 100006 Zainab Ibrahim 7
Fatma Ibrahim Khamis 100007 Fatma Ibrahim 7

Shaban Ibrahim Khamis 100162 Shaban Ibrahim 7
Alicia Shaban Ibrahim 100163 Alicia Shaban 8

Ideally I should get

Ibrahim (father of Shaban)
then Shaban the father of Alicia)
Under Shaban should get Alicia
Then Zainab (sister of Shaban
Then Fatma (sister of Shaban)
Then another member (after I have got Ibrahim and his descendants)

Any idea?

On Thu, Oct 5, 2023 at 4:15 PM swastik Gurung <gurung_swastik(at)yahoo(dot)com>
wrote:

> I suppose you ought to be using, recursive CTE queries
>
> Documentation can be found at: 7.8. WITH Queries (Common Table
> Expressions)
> <https://www.postgresql.org/docs/current/queries-with.html#QUERIES-WITH-RECURSIVE>
>
> 7.8. WITH Queries (Common Table Expressions)
>
> 7.8. WITH Queries (Common Table Expressions) # 7.8.1. SELECT in WITH
> 7.8.2. Recursive Queries 7.8.3. Common Tabl...
>
> <https://www.postgresql.org/docs/current/queries-with.html#QUERIES-WITH-RECURSIVE>
>
>
>
> On Thursday, 5 October 2023 at 16:44:46 GMT+5:45, Ibrahim Shaame <
> ishaame(at)gmail(dot)com> wrote:
>
>
> I have a table of members of a large family extendending back to eight
> generations. The current members contribute a monthly amount to the family
> fund. Only true descendants are included in the family list, no wives, no
> husbands. There are two tables
>
> 1 - Names with the following fields: idno (unique) --family member
>
> parentid -- id number of the parent who connected the child to the family
>
> etc
>
> etc
>
> 2 – Contributions with fields: idno
>
> etc
>
> etc
>
>
> Now I want to report Names and contributions par family tree: My ideal is
> to list grandfather, father, children based on the two fields (id,
> parentid).
>
> Any suggestions?
>
> Thanks in advance
>
>
>

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message David G. Johnston 2023-10-05 15:02:43 Re: Reporting by family tree
Previous Message swastik Gurung 2023-10-05 13:15:07 Re: Reporting by family tree