I would like to get cities in Canada with a location concatenation as cross-feature that is sortable. This works :
SELECT
(GROUP_CONCAT(?locationLabel; separator = ' < ') as ?locationLabels)
?city ?cityLabel
#?locationLabel
WHERE
{
?city wdt:P17 wd:Q16. # country Canada.
?city wdt:P131* ?location. #located in the administrative territorial entity (P131)
#?city wdt:P31 wd:Q515. # city (Q515)
?city wdt:P31 wd:Q21507383. #provincial or territorial capital city in Canada (Q21507383)
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en".
?city rdfs:label ?cityLabel.
?location rdfs:label ?locationLabel .
}
}
GROUP BY ?city ?cityLabel
Some results:
<html><head><meta charset="utf-8"></head><body><table><thead><tr><th>locationLabels</th><th>city</th><th>cityLabel</th></tr></thead><tbody><tr><td>Whitehorse < Yukon < Canada</td><td>http://www.wikidata.org/entity/Q2058</td><td>Whitehorse</td></tr><tr><td>Yellowknife < Northwest Territories < Canada</td><td>http://www.wikidata.org/entity/Q2061</td><td>Yellowknife</td></tr><tr><td>St. John's < Newfoundland and Labrador < Dominion of Newfoundland < Newfoundland Colony < Canada</td><td>http://www.wikidata.org/entity/Q2082</td><td>St. John's</td></tr><tr><td>Edmonton < Alberta < Canada</td><td>http://www.wikidata.org/entity/Q2096</td><td>Edmonton</td></tr><tr><td>Regina < Division No. 6 < Saskatchewan < Canada</td><td>http://www.wikidata.org/entity/Q2123</td><td>Regina</td></tr><tr><td>Charlottetown < Queens County < Prince Edward Island < Canada</td><td>http://www.wikidata.org/entity/Q2127</td><td>Charlottetown</td></tr><tr><td>Winnipeg < Winnipeg Metropolitan Region < Manitoba < Canada</td><td>http://www.wikidata.org/entity/Q2135</td><td>Winnipeg</td></tr><tr><td>Victoria < British Columbia < Capital Regional District < Canada</td><td>http://www.wikidata.org/entity/Q2132</td><td>Victoria</td></tr><tr><td>Fredericton < York County < New Brunswick < Canada</td><td>http://www.wikidata.org/entity/Q2138</td><td>Fredericton</td></tr><tr><td>Halifax < Halifax County < Nova Scotia < Canada</td><td>http://www.wikidata.org/entity/Q2141</td><td>Halifax</td></tr><tr><td>Quebec City < Quebec < Capitale-Nationale < Quebec < Canada</td><td>http://www.wikidata.org/entity/Q2145</td><td>Quebec City</td></tr><tr><td>Toronto < Ontario < Canada</td><td>http://www.wikidata.org/entity/Q172</td><td>Toronto</td></tr></tbody></table></body></html>
Nevertheless, I would like to reverse the order in which the location labels appear. For instance, I would prefer to have "Canada > Alberta > Edmonton" than "Edmonton < Alberta < Canada".
Thank you very much for your help !
EDIT
I did another attempt from provinces in Canada and a kind of inverse property 'contains administrative territorial entity (P150)' but the returned results are not convincing:
SELECT
(GROUP_CONCAT(?locationLabel; separator = ' > ') as ?locationLabels)
?province ?provinceLabel
WHERE
{
?province wdt:P31 wd:Q11828004. #province of Canada (Q11828004)
?province wdt:P150* ?location.
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en".
?province rdfs:label ?provinceLabel.
?location rdfs:label ?locationLabel .
}
}
GROUP BY ?province ?provinceLabel