query-2ab8c85a1191d167d8309596e38ba835

rq turtle/ttl

Stopping duplicates from a P31/P279 hieracy

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#>
SELECT DISTINCT ?item ?itemLabel ?instances ?thumb WHERE {
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en-GB,en". }
  {
    SELECT DISTINCT ?item
      (GROUP_CONCAT(DISTINCT ?instancelist; SEPARATOR = ", ") AS ?instances)
      (SAMPLE (DISTINCT ?thumbs) as ?thumb) WHERE {
        {?item wdt:P31/wdt:P279* wd:Q1785071.
        BIND(STR("fort.png") AS ?thumbs) # fort
        BIND(STR("fort") AS ?instancelist)}
      UNION
        {?item wdt:P31/wdt:P279* wd:Q23413.
        BIND(STR("castle.png") AS ?thumbs) # castle
        BIND(STR("castle") AS ?instancelist)}

      ?item wdt:P7959 wd:Q67479626.
    }
    GROUP by ?item
  }
}

Query found at

graph TD classDef projected fill:lightgreen; classDef literal fill:orange; classDef iri fill:yellow; v4("?instancelist") v4("?instances"):::projected v1("?item"):::projected v5("?thumb"):::projected v4("?thumbs") a1((" ")) a2((" ")) c10(["wd:Q67479626"]):::iri c2(["bd:serviceParam"]):::iri c4(["en-GB,en"]):::literal c7(["wd:Q1785071"]):::iri c8(["wd:Q23413"]):::iri subgraph s1["http://wikiba.se/ontology#label"] style s1 stroke-width:4px; c2 --"wikibase:language"--> c4 end subgraph union0[" Union "] subgraph union0l[" "] style union0l fill:#abf,stroke-dasharray: 3 3; v1 --"wdt:P31"--> a2 a2 --"wdt:P279"--> c8 bind0[/"str('castle.png')"/] bind0 --as--o v4 bind1[/"str('castle')"/] bind1 --as--o v4 end subgraph union0r[" "] style union0r fill:#abf,stroke-dasharray: 3 3; v1 --"wdt:P31"--> a1 a1 --"wdt:P279"--> c7 bind2[/"str('fort.png')"/] bind2 --as--o v4 bind3[/"str('fort')"/] bind3 --as--o v4 end union0r <== or ==> union0l end v1 --"wdt:P7959"--> c10 bind6[/"?instancelist"/] v4 --o bind6 bind6 --as--o v4 bind7[/"sample(?thumbs)"/] v4 --o bind7 bind7 --as--o v5