query-b0b6fdd4d4f9fb6e55750958390cc3e1

rq turtle/ttl

(63) 2020 (2021) Summer Olympics gold-medal countNo.63 (2021.07.28T1507 created)

Use at

PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX bd: <http://www.bigdata.com/rdf#>
# 2020 (2021) Summer Olympics gold-medal count
# viewed as bar chart (also viewable as table)
#defaultView:BarChart
SELECT DISTINCT ?country ?countryLabel ?count
WHERE {
  {SELECT DISTINCT ?country (COUNT(DISTINCT ?competition) AS ?count)
   WHERE {
     # ?competition is instance of (P31) "subclass of (P279)" [ZeroOrMorePath (*)] sports competition (Q13406554)
     ?competition wdt:P31/wdt:P279* wd:Q13406554;
                  # and is part of (P361) [OneOrMorePath (+)] 2020 (2021) Summer Olympics (Q181278)
                  wdt:P361+ wd:Q181278
     # there should not be ?x, which is part of (P361) ?competition
     FILTER NOT EXISTS {?x wdt:P361 ?competition
                        # ?x should not be instance of (P31) "part of (P361) or subclass of (P279)" [ZeroOrMorePath (*)] competition stage (Q106635272)
                        FILTER NOT EXISTS {?x wdt:P31/(wdt:P361|wdt:P279)* wd:Q106635272} }
     # ?competition should not be instance of (P31) "part of (P361) or subclass of (P279)" [ZeroOrMorePath (*)] competition stage (Q106635272)
     FILTER NOT EXISTS {?competition wdt:P31/(wdt:P361|wdt:P279)* wd:Q106635272}
     # ?competition's "participant " (P710) or "participating team" (P1923) statement is ?x_champion
     ?competition (p:P710|p:P1923) ?x_champion.
     # ?x_champion's value is ?champion
     ?x_champion (ps:P710|ps:P1923) ?champion;
                 # ?x_champion's qualifier "ranking" (P1352) is ?rank
                 pq:P1352 ?rank
     # ?rank is 1
     FILTER (?rank=1)
     # optional: ?x_champion's qualifier "country for sport" (P1532) is ?country1
     OPTIONAL {?x_champion pq:P1532 ?country1}
     # optional: ?champion's country for sport (P1532) is ?country2
     OPTIONAL {?champion wdt:P1532 ?country2}
     # use IF and EXISTS to bind ?country1 or ?country2 as ?country:
     # if ?x_champion's qualifier "country for sport" (P1532), ?country1, exists, bind ?country1 as ?country
     # otherwise (?country1 doesn't exist), bind ?country2 as ?country
     BIND(IF(EXISTS{?x_champion pq:P1532 ?country1}, ?country1, ?country2) AS ?country)
   }
   # group COUNT by ?country
   GROUP BY ?country
  }
  # show label in auto language as default, and English when no default label exists
  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; v5("?champion") v2("?competition") v9("?count"):::projected v8("?country"):::projected v6("?country1") v7("?country2") v1("?rank") v3("?x") v4("?x_champion") a3((" ")) a2((" ")) a1((" ")) c6(["wd:Q13406554"]):::iri c4(["wd:Q106635272"]):::iri c7(["wd:Q181278"]):::iri c16(["bd:serviceParam"]):::iri c18(["#91;AUTO_LANGUAGE#93;,en"]):::literal f0[["?rank = '1^^xsd:integer'"]] f0 --> v1 f1[["not "]] subgraph f1e0["Exists Clause"] e0v1 --"p:direct/P31"--> e0a1 subgraph unione00[" Union "] subgraph unione00l[" "] style unione00l fill:#abf,stroke-dasharray: 3 3; e0a1 --"p:direct/P279"--> e0c3 end subgraph unione00r[" "] style unione00r fill:#abf,stroke-dasharray: 3 3; e0a1 --"p:direct/P361"--> e0c3 end unione00r <== or ==> unione00l end e0v1("?competition"):::projected e0a1((" ")):::projected e0c3(["wd:Q106635272"]):::iri end f1--EXISTS--> f1e0 f1 --> v2 f1 --> c2 f1 --> a1 f1 --> c3 f1 --> c4 f1 --> c5 v2 --"p:direct/P31"--> a1 subgraph union0[" Union "] subgraph union0l[" "] style union0l fill:#abf,stroke-dasharray: 3 3; a1 --"p:direct/P279"--> c4 end subgraph union0r[" "] style union0r fill:#abf,stroke-dasharray: 3 3; a1 --"p:direct/P361"--> c4 end union0r <== or ==> union0l end f2[["not "]] subgraph f2e1["Exists Clause"] e1f0[["not "]] subgraph e1f0e1e0["Exists Clause"] e1e0v1 --"p:direct/P31"--> e1e0a1 subgraph unione1e00[" Union "] subgraph unione1e00l[" "] style unione1e00l fill:#abf,stroke-dasharray: 3 3; e1e0a1 --"p:direct/P279"--> e1e0c3 end subgraph unione1e00r[" "] style unione1e00r fill:#abf,stroke-dasharray: 3 3; e1e0a1 --"p:direct/P361"--> e1e0c3 end unione1e00r <== or ==> unione1e00l end e1e0v1("?x"):::projected e1e0a1((" ")):::projected e1e0c3(["wd:Q106635272"]):::iri end e1f0--EXISTS--> e1f0e1e0 e1f0 --> e1v1 e1f0 --> e1c1 e1f0 --> e1a1 e1f0 --> e1c2 e1f0 --> e1c3 e1f0 --> e1c4 e1v1 --"p:direct/P31"--> e1a1 subgraph unione10[" Union "] subgraph unione10l[" "] style unione10l fill:#abf,stroke-dasharray: 3 3; e1a1 --"p:direct/P279"--> e1c3 end subgraph unione10r[" "] style unione10r fill:#abf,stroke-dasharray: 3 3; e1a1 --"p:direct/P361"--> e1c3 end unione10r <== or ==> unione10l end e1v1 --"p:direct/P361"--> e1v2 e1v2("?competition"):::projected e1v1("?x"):::projected e1a1((" ")):::projected e1c3(["wd:Q106635272"]):::iri end f2--EXISTS--> f2e1 f2 --> v3 f2 --> c2 f2 --> a2 f2 --> c3 f2 --> c4 f2 --> c5 f2 --> v2 f3[["not "]] subgraph f3e2["Exists Clause"] e2v1 --"p:direct/P31"--> e2a1 subgraph unione20[" Union "] subgraph unione20l[" "] style unione20l fill:#abf,stroke-dasharray: 3 3; e2a1 --"p:direct/P279"--> e2c3 end subgraph unione20r[" "] style unione20r fill:#abf,stroke-dasharray: 3 3; e2a1 --"p:direct/P361"--> e2c3 end unione20r <== or ==> unione20l end e2v1("?x"):::projected e2a1((" ")):::projected e2c3(["wd:Q106635272"]):::iri end f3--EXISTS--> f3e2 f3 --> v3 f3 --> c2 f3 --> a2 f3 --> c3 f3 --> c4 f3 --> c5 v3 --"p:direct/P31"--> a2 subgraph union1[" Union "] subgraph union1l[" "] style union1l fill:#abf,stroke-dasharray: 3 3; a2 --"p:direct/P279"--> c4 end subgraph union1r[" "] style union1r fill:#abf,stroke-dasharray: 3 3; a2 --"p:direct/P361"--> c4 end union1r <== or ==> union1l end v3 --"p:direct/P361"--> v2 v2 --"p:direct/P31"--> a3 a3 --"p:direct/P279"--> c6 v2 --"p:direct/P361"--> c7 subgraph union2[" Union "] subgraph union2l[" "] style union2l fill:#abf,stroke-dasharray: 3 3; v2 --"p:P1923"--> v4 end subgraph union2r[" "] style union2r fill:#abf,stroke-dasharray: 3 3; v2 --"p:P710"--> v4 end union2r <== or ==> union2l end subgraph union3[" Union "] subgraph union3l[" "] style union3l fill:#abf,stroke-dasharray: 3 3; v4 --"p:statement/P1923"--> v5 end subgraph union3r[" "] style union3r fill:#abf,stroke-dasharray: 3 3; v4 --"p:statement/P710"--> v5 end union3r <== or ==> union3l end v4 --"p:qualifier/P1352"--> v1 subgraph optional0["(optional)"] style optional0 fill:#bbf,stroke-dasharray: 5 5; v4 -."p:qualifier/P1532".-> v6 end subgraph optional1["(optional)"] style optional1 fill:#bbf,stroke-dasharray: 5 5; v5 -."p:direct/P1532".-> v7 end v4 --"p:qualifier/P1532"--> v6 bind4[/"if( ,?country1,?country2)"/] subgraph bind4e3["Exists Clause"] e3v1 --"p:qualifier/P1532"--> e3v2 e3v2("?country1"):::projected e3v1("?x_champion"):::projected end bind4--EXISTS--> bind4e3 v4 --o bind4 c13 --o bind4 v6 --o bind4 v7 --o bind4 bind4 --as--o v8 bind6[/"count(?competition)"/] v2 --o bind6 bind6 --as--o v9 subgraph s1["http://wikiba.se/ontology#label"] style s1 stroke-width:4px; c16 --"wikibase:language"--> c18 end