614
edits
Line 2,355: | Line 2,355: | ||
order by ?item_label ?prop_label | order by ?item_label ?prop_label | ||
</sparql> | |||
===Working with property paths: organisations and locations=== | |||
<sparql tryit="1"> | |||
## problem: sometimes you need to follow property paths using */+ for (eg) locations. BUT this can lead to several results to choose between and they aren't necessarily returned in the "right" order. | |||
## how to determine the "distance" along the path of each result? | |||
## https://stackoverflow.com/questions/61984052/wikidata-get-the-full-subhierachy-of-one-class-as-well-as-all-nodes | |||
## > you can get the distance of a class with SELECT ?cls (count(?tmp) as ?distance) WHERE { ?cls wdt:P279* ?tmp . ?tmp wdt:P279+ wd:Q2095 . } group by ?cls order by asc(?distance) - though there might be corner cases like cycles in the hierarchy resp. multiple paths | |||
PREFIX bnwd: <https://beyond-notability.wikibase.cloud/entity/> | |||
PREFIX bnwds: <https://beyond-notability.wikibase.cloud/entity/statement/> | |||
PREFIX bnwdv: <https://beyond-notability.wikibase.cloud/value/> | |||
PREFIX bnwdt: <https://beyond-notability.wikibase.cloud/prop/direct/> | |||
PREFIX bnp: <https://beyond-notability.wikibase.cloud/prop/> | |||
PREFIX bnps: <https://beyond-notability.wikibase.cloud/prop/statement/> | |||
PREFIX bnpq: <https://beyond-notability.wikibase.cloud/prop/qualifier/> | |||
SELECT ?item ?itemLabel ?location ?locationLabel (count(?item) as ?distance) | |||
WHERE { | |||
?item bnwdt:P12 bnwd:Q2316 . # instance of organisation. | |||
?item bnwdt:P2* ?tmp . | |||
?tmp bnwdt:P2+ ?location . # P2=30. P2+=34 | |||
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en-gb, en". } | |||
} | |||
group by ?item ?itemLabel ?location ?locationLabel | |||
ORDER BY ?item ?distance | |||
</sparql> | </sparql> | ||
edits