query-60c1df0c6df241536f5d515084464c5b

rq turtle/ttl

Sister cities whose Wikidata IDs represent adjacent numbers SELECT ?city1 ?city1Label ?id1 ?id2 ?city2 ?city2Label ?diff WITH { SELECT ?city1 ?id1 ?id2 ?city2 ?diff WHERE { ?city1 wdt:P190 ?city2. # sister cities bind(xsd:integer(substr(str(?city1),33)) as ?id1) # get the numeric element of the QID, cast as an integer bind(xsd:integer(substr(str(?city2),33)) as ?id2) # get the numeric element of the QID, cast as an integer bind(abs(?id1 - ?id2) as ?diff ) filter(?diff =1) } } AS %cities WHERE { INCLUDE %cities SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } }

Use at

PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX bd: <http://www.bigdata.com/rdf#>
#Sister cities whose Wikidata IDs represent adjacent numbers
SELECT ?city1 ?city1Label ?id1 ?id2 ?city2 ?city2Label ?diff
WHERE 
{
   {
  SELECT ?city1 ?id1 ?id2 ?city2 ?diff WHERE {
    ?city1 wdt:P190 ?city2.         # sister cities
    bind(xsd:integer(substr(str(?city1),33)) as ?id1) # get the numeric element of the QID, cast as an integer
    bind(xsd:integer(substr(str(?city2),33)) as ?id2) # get the numeric element of the QID, cast as an integer
    bind(abs(?id1 - ?id2) as ?diff )
    filter(?diff =1)
  }
}  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

Query found at

graph TD classDef projected fill:lightgreen; classDef literal fill:orange; classDef iri fill:yellow; v2("?city1"):::projected v3("?city2"):::projected v6("?diff"):::projected v4("?id1"):::projected v5("?id2"):::projected c4(["bd:serviceParam"]):::iri c6(["#91;AUTO_LANGUAGE#93;,en"]):::literal f0[["?diff = '1^^xsd:integer'"]] f0 --> v6 v2 --"wdt:P190"--> v3 bind1[/"http://www.w3.org/2001/XMLSchema#integer(substring(str(?city1),'33^^xsd:integer'))"/] v2 --o bind1 bind1 --as--o v4 bind2[/"http://www.w3.org/2001/XMLSchema#integer(substring(str(?city2),'33^^xsd:integer'))"/] v3 --o bind2 bind2 --as--o v5 bind3[/"numeric-abs(?id1 - ?id2)"/] v4 --o bind3 v5 --o bind3 bind3 --as--o v6 subgraph s1["http://wikiba.se/ontology#label"] style s1 stroke-width:4px; c4 --"wikibase:language"--> c6 end