query-0d5c640803015f6fdb77a6565fda455b

rq turtle/ttl

Steer on combining subquery resultsThe aim of this query is to find candidate town/parish items that match town council items based on a string match. First subquery finds the councils and extracts the town name from the label as ?town_name. Second subquery finds town and parish items, extracting their labels into ?town_name. The grand idea is that the query delivers rows linking council items to town/parish items based on a ?town_name match, but instead, horrow. I've tried a variety of other approaches, but timeout. Clearly I don't know how to combine two disparate subquery sets in a useful fashion.

Use at

PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?item ?itemLabel ?town ?town_name 
WHERE {
   { { 
  SELECT ?item ?itemLabel ?town ?town_name WHERE {
    ?item wdt:P31 wd:Q3507268.
    ?item rdfs:label ?itemLabel . filter(lang(?itemLabel)="en")
    ?item wdt:P17 wd:Q145  .
    FILTER NOT EXISTS { ?item wdt:P1001 []. }
    BIND(REPLACE( ?itemLabel, " Town Council","") as ?town_name)
  } }} 
   UNION 
   { {
  SELECT DISTINCT ?town ?town_name WHERE {
    ?town wdt:P17 wd:Q145 .
    {?town wdt:P31 wd:Q1115575.}
    UNION
    {?town wdt:P31 wd:Q3957 .}
    ?town rdfs:label ?town_name . filter(lang(?town_name)="en")
  } }}
}

Query found at

graph TD classDef projected fill:lightgreen; classDef literal fill:orange; classDef iri fill:yellow; v1("?item"):::projected v2("?itemLabel"):::projected v4("?town"):::projected v3("?town_name"):::projected a1((" ")) c9(["wd:Q3957"]):::iri c4(["wd:Q3507268"]):::iri c8(["wd:Q1115575"]):::iri c7(["wd:Q145"]):::iri subgraph union0[" Union "] subgraph union0l[" "] style union0l fill:#abf,stroke-dasharray: 3 3; f0[["?town_name = 'en'"]] f0 --> v3 v4 --"wdt:P17"--> c7 subgraph union1[" Union "] subgraph union1l[" "] style union1l fill:#abf,stroke-dasharray: 3 3; v4 --"wdt:P31"--> c9 end subgraph union1r[" "] style union1r fill:#abf,stroke-dasharray: 3 3; v4 --"wdt:P31"--> c8 end union1r <== or ==> union1l end v4 --"rdfs:label"--> v3 end subgraph union0r[" "] style union0r fill:#abf,stroke-dasharray: 3 3; f1[["not "]] subgraph f1e0["Exists Clause"] e0v1 --"wdt:P1001"--> e0a1 e0v1("?item"):::projected e0a1((" ")):::projected end f1--EXISTS--> f1e0 f1 --> v1 f1 --> c1 f1 --> a1 v1 --"wdt:P1001"--> a1 f2[["?itemLabel = 'en'"]] f2 --> v2 v1 --"wdt:P31"--> c4 v1 --"rdfs:label"--> v2 v1 --"wdt:P17"--> c7 bind3[/"replace(?itemLabel,' Town Council','')"/] v2 --o bind3 bind3 --as--o v3 end union0r <== or ==> union0l end