query-acca899b45fbb99e2de860d94f33be49

rq turtle/ttl

table of 2020 (2021) Summer Olympics medal winners (gold/silver/bronze medalists) SELECT DISTINCT ?competition ?competitionLabel ?sportLabel ?competition_classLabel ?sexLabel ?start ?end ?rank ?medalist ?medalistLabel ?countryLabel 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} # optional: show ?competition's sport (P641) as ?sport OPTIONAL {?competition wdt:P641 ?sport} # optional: show ?competition's competition class (P2094) as ?competition_class OPTIONAL {?competition wdt:P2094 ?competition_class # optional: show ?competition_class's competition class (P2094) as ?sex OPTIONAL {?competition_class wdt:P2094 ?sex # define ?sex as male (Q6581097), female (Q6581072), or mixed-sex sports (Q1940854) ### Note: VALUES has to be inside, not outside, of OPTIONAL. Values ?sex {wd:Q6581097 wd:Q6581072 wd:Q1940854} } } # optional: show ?competition's start time (P580) as ?start OPTIONAL {?competition wdt:P580 ?start} # optional: show ?competition's end time (P582) as ?end OPTIONAL {?competition wdt:P582 ?end} # optional: ?competition's "participant " (P710) or "participating team" (P1923) statement is ?x_medalist OPTIONAL {?competition (p:P710|p:P1923) ?x_medalist. # ?x_medalist's value is ?medalist ?x_medalist (ps:P710|ps:P1923) ?medalist; # ?x_medalist's qualifier "ranking" (P1352) is ?rank pq:P1352 ?rank # ?rank is less than 4 ### Note: ?rank<4 can be changed to ?rank=1 (or 2 or 3) for gold (or silver or bronze) medalists. FILTER (?rank<4) # optional: ?x_medalist's qualifier "country for sport" (P1532) is ?country1 OPTIONAL {?x_medalist pq:P1532 ?country1} # optional: ?medalist's country for sport (P1532) is ?country2 OPTIONAL {?medalist wdt:P1532 ?country2} # use IF and Exists to bind ?country1 or ?country2 as ?country: # if ?x_medalist'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_medalist pq:P1532 ?country1}, ?country1, ?country2) AS ?country) } # ?country is United States (Q30) ### Note: This line can be removed, and, if not removed, United States (Q30) can be replaced with another country. FILTER (?country=wd:Q30) # show label in auto language as default, and English when no default label exists SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } }

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#>
# table of 2020 (2021) Summer Olympics medal winners (gold/silver/bronze medalists)
SELECT DISTINCT ?competition ?competitionLabel ?sportLabel ?competition_classLabel ?sexLabel ?start ?end ?rank ?medalist ?medalistLabel ?countryLabel
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}
  # optional: show ?competition's sport (P641) as ?sport
  OPTIONAL {?competition wdt:P641 ?sport}
  # optional: show ?competition's competition class (P2094) as ?competition_class
  OPTIONAL {?competition wdt:P2094 ?competition_class
            # optional: show ?competition_class's competition class (P2094) as ?sex
            OPTIONAL {?competition_class wdt:P2094 ?sex
                      # define ?sex as male (Q6581097), female (Q6581072), or mixed-sex sports (Q1940854)
                      ### Note: VALUES has to be inside, not outside, of OPTIONAL.
                      Values ?sex {wd:Q6581097 wd:Q6581072 wd:Q1940854} } }
  # optional: show ?competition's start time (P580) as ?start
  OPTIONAL {?competition wdt:P580 ?start}
  # optional: show ?competition's end time (P582) as ?end
  OPTIONAL {?competition wdt:P582 ?end}
  # optional: ?competition's "participant " (P710) or "participating team" (P1923) statement is ?x_medalist
  OPTIONAL {?competition (p:P710|p:P1923) ?x_medalist.
            # ?x_medalist's value is ?medalist
            ?x_medalist (ps:P710|ps:P1923) ?medalist;
                        # ?x_medalist's qualifier "ranking" (P1352) is ?rank
                        pq:P1352 ?rank
            # ?rank is less than 4
            ### Note: ?rank<4 can be changed to ?rank=1 (or 2 or 3) for gold (or silver or bronze) medalists.
            FILTER (?rank<4)
            # optional: ?x_medalist's qualifier "country for sport" (P1532) is ?country1
            OPTIONAL {?x_medalist pq:P1532 ?country1}
            # optional: ?medalist's country for sport (P1532) is ?country2
            OPTIONAL {?medalist wdt:P1532 ?country2}
            # use IF and Exists to bind ?country1 or ?country2 as ?country:
            # if ?x_medalist'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_medalist pq:P1532 ?country1}, ?country1, ?country2) AS ?country) }
  # ?country is United States (Q30)
  ### Note: This line can be removed, and, if not removed, United States (Q30) can be replaced with another country.
  FILTER (?country=wd:Q30)
  # 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; v3("?competition"):::projected v6("?competition_class") v14("?country") v12("?country1") v13("?country2") v9("?end"):::projected v11("?medalist"):::projected v2("?rank"):::projected v8("?sex") v5("?sport") v8("?start"):::projected v4("?x") v10("?x_medalist") a3((" ")) a2((" ")) a1((" ")) c7(["wd:Q13406554"]):::iri c5(["wd:Q106635272"]):::iri c8(["wd:Q181278"]):::iri c21(["bd:serviceParam"]):::iri c23(["#91;AUTO_LANGUAGE#93;,en"]):::literal f0[["?country = 'wd:Q30'"]] f0 --> v14 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 --> v3 f1 --> c3 f1 --> a1 f1 --> c4 f1 --> c5 f1 --> c6 v3 --"p:direct/P31"--> a1 subgraph union0[" Union "] subgraph union0l[" "] style union0l fill:#abf,stroke-dasharray: 3 3; a1 --"p:direct/P279"--> c5 end subgraph union0r[" "] style union0r fill:#abf,stroke-dasharray: 3 3; a1 --"p:direct/P361"--> c5 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 --> v4 f2 --> c3 f2 --> a2 f2 --> c4 f2 --> c5 f2 --> c6 f2 --> v3 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 --> v4 f3 --> c3 f3 --> a2 f3 --> c4 f3 --> c5 f3 --> c6 v4 --"p:direct/P31"--> a2 subgraph union1[" Union "] subgraph union1l[" "] style union1l fill:#abf,stroke-dasharray: 3 3; a2 --"p:direct/P279"--> c5 end subgraph union1r[" "] style union1r fill:#abf,stroke-dasharray: 3 3; a2 --"p:direct/P361"--> c5 end union1r <== or ==> union1l end v4 --"p:direct/P361"--> v3 v3 --"p:direct/P31"--> a3 a3 --"p:direct/P279"--> c7 v3 --"p:direct/P361"--> c8 subgraph optional0["(optional)"] style optional0 fill:#bbf,stroke-dasharray: 5 5; v3 -."p:direct/P641".-> v5 end subgraph optional1["(optional)"] style optional1 fill:#bbf,stroke-dasharray: 5 5; v3 -."p:direct/P2094".-> v6 subgraph optional2["(optional)"] style optional2 fill:#bbf,stroke-dasharray: 5 5; v6 -."p:direct/P2094".-> v8 bind4[/VALUES ?sex/] bind4-->v8 bind40(["wd:Q6581097"]) bind40 --> bind4 bind41(["wd:Q6581072"]) bind41 --> bind4 bind42(["wd:Q1940854"]) bind42 --> bind4 end end subgraph optional3["(optional)"] style optional3 fill:#bbf,stroke-dasharray: 5 5; v3 -."p:direct/P580".-> v8 end subgraph optional4["(optional)"] style optional4 fill:#bbf,stroke-dasharray: 5 5; v3 -."p:direct/P582".-> v9 end subgraph optional5["(optional)"] style optional5 fill:#bbf,stroke-dasharray: 5 5; subgraph union2[" Union "] subgraph union2l[" "] style union2l fill:#abf,stroke-dasharray: 3 3; v3 -."p:P1923".-> v10 end subgraph union2r[" "] style union2r fill:#abf,stroke-dasharray: 3 3; v3 --"p:P710"--> v10 end union2r <== or ==> union2l end subgraph union3[" Union "] subgraph union3l[" "] style union3l fill:#abf,stroke-dasharray: 3 3; v10 --"p:statement/P1923"--> v11 end subgraph union3r[" "] style union3r fill:#abf,stroke-dasharray: 3 3; v10 --"p:statement/P710"--> v11 end union3r <== or ==> union3l end v10 --"p:qualifier/P1352"--> v2 subgraph optional6["(optional)"] style optional6 fill:#bbf,stroke-dasharray: 5 5; v10 -."p:qualifier/P1532".-> v12 end subgraph optional7["(optional)"] style optional7 fill:#bbf,stroke-dasharray: 5 5; v11 -."p:direct/P1532".-> v13 end v10 --"p:qualifier/P1532"--> v12 bind5[/"if( ,?country1,?country2)"/] subgraph bind5e3["Exists Clause"] e3v1 --"p:qualifier/P1532"--> e3v2 e3v2("?country1"):::projected e3v1("?x_medalist"):::projected end bind5--EXISTS--> bind5e3 v10 --o bind5 c18 --o bind5 v12 --o bind5 v13 --o bind5 bind5 --as--o v14 end subgraph s1["http://wikiba.se/ontology#label"] style s1 stroke-width:4px; c21 --"wikibase:language"--> c23 end