/* Ahnentafel-64.sql
Builds a table of all the lines of ancestors (RINs) for a given person (RIN),
set as a runtime manual input parameter and reports Ahnentafel of last person
up to the 64th generation. 
If the runtime parameter is left blank or if the SQLite manager does not
support runtime parameters, lists all the ancestral lines for everyone in the
database - avoid doing so because it is a huge result set and very slow.
2012-01-13 ve3meo
*/
SELECT   * , 
  ( 
    1 << 0 
  ) * SUBSTR( Ahn , 
  -1 , 
  1 ) + ( 
    1 << 1 
  ) * SUBSTR( Ahn , 
  -2 , 
  1 ) + ( 
    1 << 2 
  ) * SUBSTR( Ahn , 
  -3 , 
  1 ) + ( 
    1 << 3 
  ) * SUBSTR( Ahn , 
  -4 , 
  1 ) + ( 
    1 << 4 
  ) * SUBSTR( Ahn , 
  -5 , 
  1 ) + ( 
    1 << 5 
  ) * SUBSTR( Ahn , 
  -6 , 
  1 ) + ( 
    1 << 6 
  ) * SUBSTR( Ahn , 
  -7 , 
  1 ) + ( 
    1 << 7 
  ) * SUBSTR( Ahn , 
  -8 , 
  1 ) + ( 
    1 << 8 
  ) * SUBSTR( Ahn , 
  -9 , 
  1 ) + ( 
    1 << 9 
  ) * SUBSTR( Ahn , 
  -10 , 
  1 ) + ( 
    1 << 10 
  ) * SUBSTR( Ahn , 
  -11 , 
  1 ) + ( 
    1 << 11 
  ) * SUBSTR( Ahn , 
  -12 , 
  1 ) + ( 
    1 << 12 
  ) * SUBSTR( Ahn , 
  -13 , 
  1 ) + ( 
    1 << 13 
  ) * SUBSTR( Ahn , 
  -14 , 
  1 ) + ( 
    1 << 14 
  ) * SUBSTR( Ahn , 
  -15 , 
  1 ) + ( 
    1 << 15 
  ) * SUBSTR( Ahn , 
  -16 , 
  1 ) + ( 
    1 << 16 
  ) * SUBSTR( Ahn , 
  -17 , 
  1 ) + ( 
    1 << 17 
  ) * SUBSTR( Ahn , 
  -18 , 
  1 ) + ( 
    1 << 18 
  ) * SUBSTR( Ahn , 
  -19 , 
  1 ) + ( 
    1 << 19 
  ) * SUBSTR( Ahn , 
  -20 , 
  1 ) + ( 
    1 << 20 
  ) * SUBSTR( Ahn , 
  -21 , 
  1 ) + ( 
    1 << 21 
  ) * SUBSTR( Ahn , 
  -22 , 
  1 ) + ( 
    1 << 22 
  ) * SUBSTR( Ahn , 
  -23 , 
  1 ) + ( 
    1 << 23 
  ) * SUBSTR( Ahn , 
  -24 , 
  1 ) + ( 
    1 << 24 
  ) * SUBSTR( Ahn , 
  -25 , 
  1 ) + ( 
    1 << 25 
  ) * SUBSTR( Ahn , 
  -26 , 
  1 ) + ( 
    1 << 26 
  ) * SUBSTR( Ahn , 
  -27 , 
  1 ) + ( 
    1 << 27 
  ) * SUBSTR( Ahn , 
  -28 , 
  1 ) + ( 
    1 << 28 
  ) * SUBSTR( Ahn , 
  -29 , 
  1 ) + ( 
    1 << 29 
  ) * SUBSTR( Ahn , 
  -30 , 
  1 ) + ( 
    1 << 30 
  ) * SUBSTR( Ahn , 
  -31 , 
  1 ) + ( 
    1 << 31 
  ) * SUBSTR( Ahn , 
  -32 , 
  1 ) + ( 
    1 << 32 
  ) * SUBSTR( Ahn , 
  -33 , 
  1 ) + ( 
    1 << 33 
  ) * SUBSTR( Ahn , 
  -34 , 
  1 ) + ( 
    1 << 34 
  ) * SUBSTR( Ahn , 
  -35 , 
  1 ) + ( 
    1 << 35 
  ) * SUBSTR( Ahn , 
  -36 , 
  1 ) + ( 
    1 << 36 
  ) * SUBSTR( Ahn , 
  -37 , 
  1 ) + ( 
    1 << 37 
  ) * SUBSTR( Ahn , 
  -38 , 
  1 ) + ( 
    1 << 38 
  ) * SUBSTR( Ahn , 
  -39 , 
  1 ) + ( 
    1 << 39 
  ) * SUBSTR( Ahn , 
  -40 , 
  1 ) + ( 
    1 << 40 
  ) * SUBSTR( Ahn , 
  -41 , 
  1 ) + ( 
    1 << 41 
  ) * SUBSTR( Ahn , 
  -42 , 
  1 ) + ( 
    1 << 42 
  ) * SUBSTR( Ahn , 
  -43 , 
  1 ) + ( 
    1 << 43 
  ) * SUBSTR( Ahn , 
  -44 , 
  1 ) + ( 
    1 << 44 
  ) * SUBSTR( Ahn , 
  -45 , 
  1 ) + ( 
    1 << 45 
  ) * SUBSTR( Ahn , 
  -46 , 
  1 ) + ( 
    1 << 46 
  ) * SUBSTR( Ahn , 
  -47 , 
  1 ) + ( 
    1 << 47 
  ) * SUBSTR( Ahn , 
  -48 , 
  1 ) + ( 
    1 << 48 
  ) * SUBSTR( Ahn , 
  -49 , 
  1 ) + ( 
    1 << 49 
  ) * SUBSTR( Ahn , 
  -50 , 
  1 ) + ( 
    1 << 50 
  ) * SUBSTR( Ahn , 
  -51 , 
  1 ) + ( 
    1 << 51 
  ) * SUBSTR( Ahn , 
  -52 , 
  1 ) + ( 
    1 << 52 
  ) * SUBSTR( Ahn , 
  -53 , 
  1 ) + ( 
    1 << 53 
  ) * SUBSTR( Ahn , 
  -54 , 
  1 ) + ( 
    1 << 54 
  ) * SUBSTR( Ahn , 
  -55 , 
  1 ) + ( 
    1 << 55 
  ) * SUBSTR( Ahn , 
  -56 , 
  1 ) + ( 
    1 << 56 
  ) * SUBSTR( Ahn , 
  -57 , 
  1 ) + ( 
    1 << 57 
  ) * SUBSTR( Ahn , 
  -58 , 
  1 ) + ( 
    1 << 58 
  ) * SUBSTR( Ahn , 
  -59 , 
  1 ) + ( 
    1 << 59 
  ) * SUBSTR( Ahn , 
  -60 , 
  1 ) + ( 
    1 << 60 
  ) * SUBSTR( Ahn , 
  -61 , 
  1 ) + ( 
    1 << 61 
  ) * SUBSTR( Ahn , 
  -62 , 
  1 ) + ( 
    1 << 62 
  ) * SUBSTR( Ahn , 
  -63 , 
  1 ) + ( 
    1 << 63 
  ) * SUBSTR( Ahn , 
  -64 , 
  1 ) AS Ahnentafel 
FROM 
  ( 
    SELECT 
      Fam1.PersonID AS Gen1 , 
      Fam2.PersonID AS Gen2 , 
      Fam3.PersonID AS Gen3 , 
      Fam4.PersonID AS Gen4 , 
      Fam5.PersonID AS Gen5 , 
      Fam6.PersonID AS Gen6 , 
      Fam7.PersonID AS Gen7 , 
      Fam8.PersonID AS Gen8 , 
      Fam9.PersonID AS Gen9 , 
      Fam10.PersonID AS Gen10 , 
      Fam11.PersonID AS Gen11 , 
      Fam12.PersonID AS Gen12 , 
      Fam13.PersonID AS Gen13 , 
      Fam14.PersonID AS Gen14 , 
      Fam15.PersonID AS Gen15 , 
      Fam16.PersonID AS Gen16 , 
      Fam17.PersonID AS Gen17 , 
      Fam18.PersonID AS Gen18 , 
      Fam19.PersonID AS Gen19 , 
      Fam20.PersonID AS Gen20 , 
      Fam21.PersonID AS Gen21 , 
      Fam22.PersonID AS Gen22 , 
      Fam23.PersonID AS Gen23 , 
      Fam24.PersonID AS Gen24 , 
      Fam25.PersonID AS Gen25 , 
      Fam26.PersonID AS Gen26 , 
      Fam27.PersonID AS Gen27 , 
      Fam28.PersonID AS Gen28 , 
      Fam29.PersonID AS Gen29 , 
      Fam30.PersonID AS Gen30 , 
      Fam31.PersonID AS Gen31 , 
      Fam32.PersonID AS Gen32 , 
      Fam33.PersonID AS Gen33 , 
      Fam34.PersonID AS Gen34 , 
      Fam35.PersonID AS Gen35 , 
      Fam36.PersonID AS Gen36 , 
      Fam37.PersonID AS Gen37 , 
      Fam38.PersonID AS Gen38 , 
      Fam39.PersonID AS Gen39 , 
      Fam40.PersonID AS Gen40 , 
      Fam41.PersonID AS Gen41 , 
      Fam42.PersonID AS Gen42 , 
      Fam43.PersonID AS Gen43 , 
      Fam44.PersonID AS Gen44 , 
      Fam45.PersonID AS Gen45 , 
      Fam46.PersonID AS Gen46 , 
      Fam47.PersonID AS Gen47 , 
      Fam48.PersonID AS Gen48 , 
      Fam49.PersonID AS Gen49 , 
      Fam50.PersonID AS Gen50 , 
      Fam51.PersonID AS Gen51 , 
      Fam52.PersonID AS Gen52 , 
      Fam53.PersonID AS Gen53 , 
      Fam54.PersonID AS Gen54 , 
      Fam55.PersonID AS Gen55 , 
      Fam56.PersonID AS Gen56 , 
      Fam57.PersonID AS Gen57 , 
      Fam58.PersonID AS Gen58 , 
      Fam59.PersonID AS Gen59 , 
      Fam60.PersonID AS Gen60 , 
      Fam61.PersonID AS Gen61 , 
      Fam62.PersonID AS Gen62 , 
      Fam63.PersonID AS Gen63 , 
      Fam64.PersonID AS Gen64 , 
      '1' || IFNULL( Fam2.Sex , 
      '' ) || IFNULL( Fam3.Sex , 
      '' ) || IFNULL( Fam4.Sex , 
      '' ) || IFNULL( Fam5.Sex , 
      '' ) || IFNULL( Fam6.Sex , 
      '' ) || IFNULL( Fam7.Sex , 
      '' ) || IFNULL( Fam8.Sex , 
      '' ) || IFNULL( Fam9.Sex , 
      '' ) || IFNULL( Fam10.Sex , 
      '' ) || IFNULL( Fam11.Sex , 
      '' ) || IFNULL( Fam12.Sex , 
      '' ) || IFNULL( Fam13.Sex , 
      '' ) || IFNULL( Fam14.Sex , 
      '' ) || IFNULL( Fam15.Sex , 
      '' ) || IFNULL( Fam16.Sex , 
      '' ) || IFNULL( Fam17.Sex , 
      '' ) || IFNULL( Fam18.Sex , 
      '' ) || IFNULL( Fam19.Sex , 
      '' ) || IFNULL( Fam20.Sex , 
      '' ) || IFNULL( Fam21.Sex , 
      '' ) || IFNULL( Fam22.Sex , 
      '' ) || IFNULL( Fam23.Sex , 
      '' ) || IFNULL( Fam24.Sex , 
      '' ) || IFNULL( Fam25.Sex , 
      '' ) || IFNULL( Fam26.Sex , 
      '' ) || IFNULL( Fam27.Sex , 
      '' ) || IFNULL( Fam28.Sex , 
      '' ) || IFNULL( Fam29.Sex , 
      '' ) || IFNULL( Fam30.Sex , 
      '' ) || IFNULL( Fam31.Sex , 
      '' ) || IFNULL( Fam32.Sex , 
      '' ) || IFNULL( Fam33.Sex , 
      '' ) || IFNULL( Fam34.Sex , 
      '' ) || IFNULL( Fam35.Sex , 
      '' ) || IFNULL( Fam36.Sex , 
      '' ) || IFNULL( Fam37.Sex , 
      '' ) || IFNULL( Fam38.Sex , 
      '' ) || IFNULL( Fam39.Sex , 
      '' ) || IFNULL( Fam40.Sex , 
      '' ) || IFNULL( Fam41.Sex , 
      '' ) || IFNULL( Fam42.Sex , 
      '' ) || IFNULL( Fam43.Sex , 
      '' ) || IFNULL( Fam44.Sex , 
      '' ) || IFNULL( Fam45.Sex , 
      '' ) || IFNULL( Fam46.Sex , 
      '' ) || IFNULL( Fam47.Sex , 
      '' ) || IFNULL( Fam48.Sex , 
      '' ) || IFNULL( Fam49.Sex , 
      '' ) || IFNULL( Fam50.Sex , 
      '' ) || IFNULL( Fam51.Sex , 
      '' ) || IFNULL( Fam52.Sex , 
      '' ) || IFNULL( Fam53.Sex , 
      '' ) || IFNULL( Fam54.Sex , 
      '' ) || IFNULL( Fam55.Sex , 
      '' ) || IFNULL( Fam56.Sex , 
      '' ) || IFNULL( Fam57.Sex , 
      '' ) || IFNULL( Fam58.Sex , 
      '' ) || IFNULL( Fam59.Sex , 
      '' ) || IFNULL( Fam60.Sex , 
      '' ) || IFNULL( Fam61.Sex , 
      '' ) || IFNULL( Fam62.Sex , 
      '' ) || IFNULL( Fam63.Sex , 
      '' ) || IFNULL( Fam64.Sex , 
      '' ) AS Ahn 
    FROM 
      ( 
        SELECT 
          PersonID , 
          1 AS Sex 
        FROM 
          PersonTable 
      ) AS Fam1 LEFT JOIN 
      ( 
        SELECT 
          Ch2.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch2 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch2.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch2 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam2 ON Fam1.PersonID = Fam2.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch3.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch3 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch3.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch3 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam3 ON Fam2.PersonID = Fam3.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch4.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch4 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch4.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch4 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam4 ON Fam3.PersonID = Fam4.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch5.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch5 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch5.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch5 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam5 ON Fam4.PersonID = Fam5.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch6.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch6 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch6.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch6 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam6 ON Fam5.PersonID = Fam6.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch7.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch7 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch7.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch7 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam7 ON Fam6.PersonID = Fam7.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch8.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch8 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch8.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch8 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam8 ON Fam7.PersonID = Fam8.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch9.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch9 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch9.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch9 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam9 ON Fam8.PersonID = Fam9.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch10.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch10 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch10.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch10 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam10 ON Fam9.PersonID = Fam10.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch11.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch11 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch11.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch11 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam11 ON Fam10.PersonID = Fam11.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch12.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch12 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch12.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch12 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam12 ON Fam11.PersonID = Fam12.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch13.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch13 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch13.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch13 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam13 ON Fam12.PersonID = Fam13.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch14.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch14 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch14.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch14 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam14 ON Fam13.PersonID = Fam14.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch15.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch15 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch15.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch15 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam15 ON Fam14.PersonID = Fam15.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch16.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch16 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch16.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch16 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam16 ON Fam15.PersonID = Fam16.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch17.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch17 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch17.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch17 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam17 ON Fam16.PersonID = Fam17.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch18.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch18 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch18.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch18 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam18 ON Fam17.PersonID = Fam18.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch19.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch19 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch19.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch19 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam19 ON Fam18.PersonID = Fam19.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch20.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch20 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch20.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch20 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam20 ON Fam19.PersonID = Fam20.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch21.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch21 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch21.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch21 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam21 ON Fam20.PersonID = Fam21.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch22.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch22 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch22.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch22 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam22 ON Fam21.PersonID = Fam22.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch23.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch23 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch23.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch23 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam23 ON Fam22.PersonID = Fam23.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch24.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch24 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch24.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch24 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam24 ON Fam23.PersonID = Fam24.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch25.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch25 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch25.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch25 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam25 ON Fam24.PersonID = Fam25.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch26.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch26 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch26.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch26 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam26 ON Fam25.PersonID = Fam26.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch27.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch27 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch27.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch27 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam27 ON Fam26.PersonID = Fam27.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch28.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch28 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch28.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch28 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam28 ON Fam27.PersonID = Fam28.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch29.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch29 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch29.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch29 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam29 ON Fam28.PersonID = Fam29.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch30.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch30 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch30.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch30 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam30 ON Fam29.PersonID = Fam30.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch31.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch31 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch31.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch31 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam31 ON Fam30.PersonID = Fam31.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch32.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch32 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch32.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch32 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam32 ON Fam31.PersonID = Fam32.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch33.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch33 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch33.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch33 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam33 ON Fam32.PersonID = Fam33.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch34.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch34 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch34.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch34 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam34 ON Fam33.PersonID = Fam34.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch35.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch35 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch35.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch35 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam35 ON Fam34.PersonID = Fam35.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch36.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch36 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch36.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch36 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam36 ON Fam35.PersonID = Fam36.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch37.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch37 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch37.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch37 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam37 ON Fam36.PersonID = Fam37.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch38.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch38 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch38.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch38 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam38 ON Fam37.PersonID = Fam38.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch39.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch39 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch39.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch39 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam39 ON Fam38.PersonID = Fam39.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch40.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch40 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch40.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch40 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam40 ON Fam39.PersonID = Fam40.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch41.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch41 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch41.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch41 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam41 ON Fam40.PersonID = Fam41.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch42.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch42 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch42.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch42 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam42 ON Fam41.PersonID = Fam42.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch43.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch43 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch43.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch43 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam43 ON Fam42.PersonID = Fam43.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch44.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch44 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch44.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch44 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam44 ON Fam43.PersonID = Fam44.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch45.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch45 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch45.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch45 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam45 ON Fam44.PersonID = Fam45.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch46.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch46 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch46.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch46 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam46 ON Fam45.PersonID = Fam46.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch47.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch47 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch47.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch47 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam47 ON Fam46.PersonID = Fam47.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch48.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch48 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch48.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch48 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam48 ON Fam47.PersonID = Fam48.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch49.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch49 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch49.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch49 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam49 ON Fam48.PersonID = Fam49.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch50.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch50 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch50.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch50 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam50 ON Fam49.PersonID = Fam50.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch51.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch51 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch51.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch51 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam51 ON Fam50.PersonID = Fam51.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch52.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch52 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch52.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch52 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam52 ON Fam51.PersonID = Fam52.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch53.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch53 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch53.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch53 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam53 ON Fam52.PersonID = Fam53.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch54.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch54 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch54.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch54 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam54 ON Fam53.PersonID = Fam54.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch55.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch55 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch55.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch55 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam55 ON Fam54.PersonID = Fam55.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch56.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch56 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch56.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch56 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam56 ON Fam55.PersonID = Fam56.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch57.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch57 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch57.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch57 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam57 ON Fam56.PersonID = Fam57.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch58.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch58 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch58.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch58 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam58 ON Fam57.PersonID = Fam58.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch59.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch59 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch59.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch59 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam59 ON Fam58.PersonID = Fam59.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch60.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch60 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch60.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch60 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam60 ON Fam59.PersonID = Fam60.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch61.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch61 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch61.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch61 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam61 ON Fam60.PersonID = Fam61.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch62.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch62 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch62.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch62 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam62 ON Fam61.PersonID = Fam62.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch63.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch63 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch63.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch63 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam63 ON Fam62.PersonID = Fam63.ChildID LEFT JOIN 
      ( 
        SELECT 
          Ch64.ChildID , 
          FatherID AS PersonID , 
          0 AS Sex 
        FROM 
          ChildTable AS Ch64 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          FatherID > 0 AND RelFather = 0 
        UNION 
        SELECT 
          Ch64.ChildID , 
          MotherID AS PersonID , 
          1 AS Sex 
        FROM 
          ChildTable AS Ch64 INNER JOIN 
          FamilyTable USING 
            ( 
              FamilyID 
            ) 
        WHERE 
          MotherID > 0 AND RelMother = 0 
      ) AS Fam64 ON Fam63.PersonID = Fam64.ChildID 
    WHERE 
      CASE 
        WHEN @StartRIN NOT LIKE '' THEN Gen1 = @StartRIN 
        ELSE 1 
      END 
  ) ;
