query-afd1780870945f9bcfae23affa7f4f67

rq turtle/ttl

title: Ten generations of MPs SELECT DISTINCT ?MP1Label ?MP1 ?MP10Label ?MP10

WITH { SELECT DISTINCT ?MP1 ?MP2 WHERE { {wd:Q16707842 ^wdt:P279/^wdt:P39 ?MP1} UNION {wd:Q18015642 ^wdt:P279/^wdt:P39 ?MP1} UNION {wd:Q18018860 ^wdt:P279/^wdt:P39 ?MP1} {wd:Q16707842 ^wdt:P279/^wdt:P39 ?MP2} UNION {wd:Q18015642 ^wdt:P279/^wdt:P39 ?MP2} UNION {wd:Q18018860 ^wdt:P279/^wdt:P39 ?MP2} ?MP1 wdt:P31 wd:Q5 ; ^wdt:P40 ?MP2 . } } AS %results1

WITH { SELECT DISTINCT ?MP1 ?MP2 ?MP6 ?MP10 WHERE { INCLUDE %results1 . ?MP2 ^wdt:P40/^wdt:P40/^wdt:P40/^wdt:P40 ?MP6 . ?MP6 wdt:P39 [] . ?MP6 ^wdt:P40/^wdt:P40/^wdt:P40/^wdt:P40 ?MP10 . ?MP10 wdt:P39 [] . } } AS %results2

WITH { SELECT DISTINCT ?MP1 ?MP2 ?MP6 ?MP10 WHERE { INCLUDE %results2 . {wd:Q16707842 ^wdt:P279/^wdt:P39 ?MP6} UNION {wd:Q18015642 ^wdt:P279/^wdt:P39 ?MP6} UNION {wd:Q18018860 ^wdt:P279/^wdt:P39 ?MP6} {wd:Q16707842 ^wdt:P279/^wdt:P39 ?MP10} UNION {wd:Q18015642 ^wdt:P279/^wdt:P39 ?MP10} UNION {wd:Q18018860 ^wdt:P279/^wdt:P39 ?MP10} } } AS %results3

WITH { SELECT DISTINCT ?MP1 ?MP2 ?MP4 ?MP6 ?MP8 ?MP10 WHERE { INCLUDE %results3 . ?MP2 ^wdt:P40/^wdt:P40 ?MP4 . ?MP4 ^wdt:P40/^wdt:P40 ?MP6; wdt:P39 [] . ?MP6 ^wdt:P40/^wdt:P40 ?MP8 . ?MP8 ^wdt:P40/^wdt:P40 ?MP10; wdt:P39 [] . } } AS %results4

WITH { SELECT DISTINCT ?MP1 ?MP2 ?MP4 ?MP6 ?MP8 ?MP10 WHERE { INCLUDE %results4 . {wd:Q16707842 ^wdt:P279/^wdt:P39 ?MP4} UNION {wd:Q18015642 ^wdt:P279/^wdt:P39 ?MP4} UNION {wd:Q18018860 ^wdt:P279/^wdt:P39 ?MP4} {wd:Q16707842 ^wdt:P279/^wdt:P39 ?MP8} UNION {wd:Q18015642 ^wdt:P279/^wdt:P39 ?MP8} UNION {wd:Q18018860 ^wdt:P279/^wdt:P39 ?MP8} } } AS %results5

WITH { SELECT DISTINCT ?MP1 ?MP2 ?MP3 ?MP4 ?MP5 ?MP6 ?MP7 ?MP8 ?MP9 ?MP10 WHERE { INCLUDE %results5 . ?MP2 ^wdt:P40 ?MP3 . ?MP3 ^wdt:P40 ?MP4; wdt:P39 [] . ?MP4 ^wdt:P40 ?MP5 . ?MP5 ^wdt:P40 ?MP6; wdt:P39 [] . ?MP6 ^wdt:P40 ?MP7 . ?MP7 ^wdt:P40 ?MP8; wdt:P39 [] . ?MP8 ^wdt:P40 ?MP9 . ?MP9 ^wdt:P40 ?MP10; wdt:P39 [] . } } AS %results6

WITH { SELECT DISTINCT ?MP1 ?MP2 ?MP3 ?MP4 ?MP5 ?MP6 ?MP7 ?MP8 ?MP9 ?MP10 WHERE { INCLUDE %results6 . {wd:Q16707842 ^wdt:P279/^wdt:P39 ?MP3} UNION {wd:Q18015642 ^wdt:P279/^wdt:P39 ?MP3} UNION {wd:Q18018860 ^wdt:P279/^wdt:P39 ?MP3} {wd:Q16707842 ^wdt:P279/^wdt:P39 ?MP5} UNION {wd:Q18015642 ^wdt:P279/^wdt:P39 ?MP5} UNION {wd:Q18018860 ^wdt:P279/^wdt:P39 ?MP5} {wd:Q16707842 ^wdt:P279/^wdt:P39 ?MP7} UNION {wd:Q18015642 ^wdt:P279/^wdt:P39 ?MP7} UNION {wd:Q18018860 ^wdt:P279/^wdt:P39 ?MP7} {wd:Q16707842 ^wdt:P279/^wdt:P39 ?MP9} UNION {wd:Q18015642 ^wdt:P279/^wdt:P39 ?MP9} UNION {wd:Q18018860 ^wdt:P279/^wdt:P39 ?MP9} } } AS %results7

WHERE { INCLUDE %results7 . FILTER NOT EXISTS { ?MP10 ^wdt:P40 ?MP11 . {wd:Q16707842 ^wdt:P279/^wdt:P39 ?MP11} UNION {wd:Q18015642 ^wdt:P279/^wdt:P39 ?MP11} UNION {wd:Q18018860 ^wdt:P279/^wdt:P39 ?MP11} } FILTER NOT EXISTS { ?MP0 ^wdt:P40 ?MP1 . {wd:Q16707842 ^wdt:P279/^wdt:P39 ?MP0} UNION {wd:Q18015642 ^wdt:P279/^wdt:P39 ?MP0} UNION {wd:Q18018860 ^wdt:P279/^wdt:P39 ?MP0} } # MP10 does not have a parent, and MP1 does not have a child

SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } }

Use at

PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX bd: <http://www.bigdata.com/rdf#>
#title: Ten generations of MPs
SELECT DISTINCT ?MP1Label ?MP1 ?MP10Label ?MP10








WHERE {
   {
  SELECT DISTINCT ?MP1 ?MP2 ?MP3 ?MP4 ?MP5 ?MP6 ?MP7 ?MP8 ?MP9 ?MP10 WHERE {
     {
  SELECT DISTINCT ?MP1 ?MP2 ?MP3 ?MP4 ?MP5 ?MP6 ?MP7 ?MP8 ?MP9 ?MP10 WHERE {
    INCLUDE %results5 .
    ?MP2 ^wdt:P40 ?MP3 .
    ?MP3 ^wdt:P40 ?MP4; wdt:P39 [] .
    ?MP4 ^wdt:P40 ?MP5 .
    ?MP5 ^wdt:P40 ?MP6; wdt:P39 [] .
    ?MP6 ^wdt:P40 ?MP7 .
    ?MP7 ^wdt:P40 ?MP8; wdt:P39 [] .
    ?MP8 ^wdt:P40 ?MP9 .
    ?MP9 ^wdt:P40 ?MP10; wdt:P39 [] .
  }
}.
    {wd:Q16707842 ^wdt:P279*/^wdt:P39 ?MP3} UNION {wd:Q18015642 ^wdt:P279*/^wdt:P39 ?MP3} UNION {wd:Q18018860 ^wdt:P279*/^wdt:P39 ?MP3}
    {wd:Q16707842 ^wdt:P279*/^wdt:P39 ?MP5} UNION {wd:Q18015642 ^wdt:P279*/^wdt:P39 ?MP5} UNION {wd:Q18018860 ^wdt:P279*/^wdt:P39 ?MP5}
    {wd:Q16707842 ^wdt:P279*/^wdt:P39 ?MP7} UNION {wd:Q18015642 ^wdt:P279*/^wdt:P39 ?MP7} UNION {wd:Q18018860 ^wdt:P279*/^wdt:P39 ?MP7}
    {wd:Q16707842 ^wdt:P279*/^wdt:P39 ?MP9} UNION {wd:Q18015642 ^wdt:P279*/^wdt:P39 ?MP9} UNION {wd:Q18018860 ^wdt:P279*/^wdt:P39 ?MP9}
  }
}.
  FILTER NOT EXISTS 
  { ?MP10 ^wdt:P40 ?MP11 . {wd:Q16707842 ^wdt:P279*/^wdt:P39 ?MP11} 
   UNION {wd:Q18015642 ^wdt:P279*/^wdt:P39 ?MP11} UNION {wd:Q18018860 ^wdt:P279*/^wdt:P39 ?MP11} }
  FILTER NOT EXISTS 
  { ?MP0 ^wdt:P40 ?MP1 . {wd:Q16707842 ^wdt:P279*/^wdt:P39 ?MP0} 
   UNION {wd:Q18015642 ^wdt:P279*/^wdt:P39 ?MP0} UNION {wd:Q18018860 ^wdt:P279*/^wdt:P39 ?MP0} }
  # MP10 does not have a parent, and MP1 does not have a child

  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

Query found at