query-9840d7c426fa2c2c96aa482b4cdcf3a1

rq turtle/ttl

Propertiessubclass of (P279)country (P17)instance of (P31)located in the administrative territorial entity (P131)

Use at

PREFIX wikibase: <http://wikiba.se/ontology#>
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#>
PREFIX bd: <http://www.bigdata.com/rdf#>
SELECT DISTINCT ?subclassOccurrence ?subclassOccurrenceLabel ?country ?countryLabel ?administrativeContainerLabel (COUNT(DISTINCT ?instance) AS ?instanceCount) (GROUP_CONCAT(DISTINCT ?instanceLabelQ;SEPARATOR=" | ") AS ?instances)
WHERE {
  VALUES ?level {
    wd:Q13220204
  }
  VALUES ?country {
    wd:Q30 #United States of America
  }
  ?subclassOccurrence wdt:P279 ?level;
                      wdt:P17 ?country.
  OPTIONAL {
    ?instance wdt:P31/wdt:P279* ?subclassOccurrence.

    OPTIONAL {
      ?instance wdt:P131 ?administrativeContainer .
    }
    OPTIONAL {
      ?instance rdfs:label ?enLabel .
      FILTER(LANG(?enLabel)="en")
      BIND(CONCAT(STR(?enLabel), " (", STRAFTER(STR(?instance), STR(wd:)), ")") AS ?instanceLabelQ)
    }
  }
  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en,zh". 
    ?subclassOccurrence rdfs:label ?subclassOccurrenceLabel .
    ?country rdfs:label ?countryLabel .
    ?administrativeContainer rdfs:label ?administrativeContainerLabel .
  }
}
GROUP BY ?subclassOccurrence ?subclassOccurrenceLabel ?country ?countryLabel ?administrativeContainerLabel
ORDER BY DESC(?instanceCount)

Query found at

graph TD classDef projected fill:lightgreen; classDef literal fill:orange; classDef iri fill:yellow; v7("?administrativeContainer") v11("?administrativeContainerLabel"):::projected v3("?country"):::projected v10("?countryLabel"):::projected v5("?enLabel") v6("?instance"):::projected v12("?instanceCount") v8("?instanceLabelQ"):::projected v12("?instances") v2("?level") v4("?subclassOccurrence"):::projected v9("?subclassOccurrenceLabel"):::projected a1((" ")) c10(["#91;AUTO_LANGUAGE#93;,en,zh"]):::literal c8(["bd:serviceParam"]):::iri bind0[/VALUES ?level/] bind0-->v2 bind00(["wd:Q13220204"]) bind00 --> bind0 bind1[/VALUES ?country/] bind1-->v3 bind10(["wd:Q30"]) bind10 --> bind1 v4 --"wdt:P279"--> v2 v4 --"wdt:P17"--> v3 subgraph optional0["(optional)"] style optional0 fill:#bbf,stroke-dasharray: 5 5; v6 -."wdt:P31".-> a1 a1 --"wdt:P279"--> v4 subgraph optional1["(optional)"] style optional1 fill:#bbf,stroke-dasharray: 5 5; v6 -."wdt:P131".-> v7 end subgraph optional2["(optional)"] style optional2 fill:#bbf,stroke-dasharray: 5 5; v6 -."rdfs:label".-> v5 bind2[/"concat(str(?enLabel),' (',substring-after(str(?instance),str('wd:')),')')"/] v5 --o bind2 v6 --o bind2 bind2 --as--o v8 end end subgraph s1["http://wikiba.se/ontology#label"] style s1 stroke-width:4px; c8 --"wikibase:language"--> c10 v4 --"rdfs:label"--> v9 v3 --"rdfs:label"--> v10 v7 --"rdfs:label"--> v11 end bind5[/"count(?instance)"/] v6 --o bind5 bind5 --as--o v12 bind6[/"?instanceLabelQ"/] v8 --o bind6 bind6 --as--o v12