query-358932841b767825af51d1125ecc436a

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) 
(GROUP_CONCAT(DISTINCT ?instanceTypeLabel;SEPARATOR=" | ") AS ?instanceTypeLabels)
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 wdt:P31 ?instanceType}
    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 .
    ?instanceType rdfs:label ?instanceTypeLabel .
  }
}
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") v12("?administrativeContainerLabel"):::projected v3("?country"):::projected v11("?countryLabel"):::projected v5("?enLabel") v6("?instance"):::projected v14("?instanceCount") v9("?instanceLabelQ"):::projected v8("?instanceType") v13("?instanceTypeLabel"):::projected v15("?instanceTypeLabels") v14("?instances") v2("?level") v4("?subclassOccurrence"):::projected v10("?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 -."wdt:P31".-> v8 end subgraph optional3["(optional)"] style optional3 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 v9 end end subgraph s1["http://wikiba.se/ontology#label"] style s1 stroke-width:4px; c8 --"wikibase:language"--> c10 v4 --"rdfs:label"--> v10 v3 --"rdfs:label"--> v11 v7 --"rdfs:label"--> v12 v8 --"rdfs:label"--> v13 end bind6[/"count(?instance)"/] v6 --o bind6 bind6 --as--o v14 bind7[/"?instanceLabelQ"/] v9 --o bind7 bind7 --as--o v14 bind8[/"?instanceTypeLabel"/] v13 --o bind8 bind8 --as--o v15