query-db0ce71a4dae59e73e403daec93a6e28

rq turtle/ttl

GenresI have the following query which among other things gets the genres of a list of video games.

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
      (GROUP_CONCAT(DISTINCT     ?dateYear ; separator = ", ") AS     ?dateYears)
      (GROUP_CONCAT(DISTINCT  ?gameENLabel ; separator = ", ") AS  ?gameENLabels)
      (GROUP_CONCAT(DISTINCT   ?genreLabel ; separator = ", ") AS   ?genreLabels)
      WHERE {
        # test values
        #VALUES ?game {
        #  wd:Q4850488    # Baldur's Gate III: The Black Hound (should have "no value" due to being cancelled)
        #  wd:Q4931588    # Bob's Game (should have "no value" due to not being released yet)
        #  wd:Q5315330    # Dunjonquest (1979)
        #  wd:Q5250229    # Deep Labyrinth (should have multiple dates)
        #  wd:Q1462499    # Starflight (1986, has multiple genres)
        #  wd:Q23647080   # God Wars: Beyond Time (should have blank date since the property does not exist)
        #}
              {?game wdt:P136 wd:Q744038}   # regular RPGs
        UNION {?game wdt:P136 wd:Q1529437}  # tactical RPGs
        UNION {?game wdt:P136 wd:Q1422746}  # action RPGs
        UNION {?game wdt:P136 wd:Q1143132}  # roguelikes
        ?game wdt:P31 wd:Q7889.             # instance of video game

        ?game rdfs:label ?gameENLabel.
        FILTER(LANG(?gameENLabel) = "en").  # we mainly want English labels

        OPTIONAL {?game wdt:P577       ?date}
        BIND(YEAR(?date) AS ?dateYear).    # faster substitute

        OPTIONAL {
          ?game wdt:P136 ?genre.
          ?genre rdfs:label ?genreString.
          FILTER(?genre != wd:Q744038).
          FILTER(LANG(?genreString) = "en").
          BIND(STR(?genreString) AS ?genreLabel).    # faster substitute
        }

        SERVICE wikibase:label {
          bd:serviceParam wikibase:language "en".
        }
      }
    GROUP BY $game
    ORDER BY asc (?dateYears) ASC (?gameENLabels)
    #limit 100

Query found at

graph TD classDef projected fill:lightgreen; classDef literal fill:orange; classDef iri fill:yellow; v7("?date") v8("?dateYear"):::projected v10("?dateYears") v6("?game") v5("?gameENLabel"):::projected v10("?gameENLabels") v3("?genre") v9("?genreLabel"):::projected v10("?genreLabels") v4("?genreString") c1(["wd:Q744038"]):::iri c2(["en"]):::literal c5(["wd:Q1422746"]):::iri c4(["wd:Q1529437"]):::iri c12(["bd:serviceParam"]):::iri c8(["wd:Q7889"]):::iri c6(["wd:Q1143132"]):::iri f0[["?gameENLabel = 'en'"]] f0 --> v5 subgraph union0[" Union "] subgraph union0l[" "] style union0l fill:#abf,stroke-dasharray: 3 3; subgraph union1[" Union "] subgraph union1l[" "] style union1l fill:#abf,stroke-dasharray: 3 3; subgraph union2[" Union "] subgraph union2l[" "] style union2l fill:#abf,stroke-dasharray: 3 3; v6 --"wdt:P136"--> c6 end subgraph union2r[" "] style union2r fill:#abf,stroke-dasharray: 3 3; v6 --"wdt:P136"--> c5 end union2r <== or ==> union2l end end subgraph union1r[" "] style union1r fill:#abf,stroke-dasharray: 3 3; v6 --"wdt:P136"--> c4 end union1r <== or ==> union1l end end subgraph union0r[" "] style union0r fill:#abf,stroke-dasharray: 3 3; v6 --"wdt:P136"--> c1 end union0r <== or ==> union0l end v6 --"wdt:P31"--> c8 v6 --"rdfs:label"--> v5 subgraph optional0["(optional)"] style optional0 fill:#bbf,stroke-dasharray: 5 5; v6 -."wdt:P577".-> v7 end bind1[/"year-from-dateTime(?date)"/] v7 --o bind1 bind1 --as--o v8 subgraph optional1["(optional)"] style optional1 fill:#bbf,stroke-dasharray: 5 5; v6 -."wdt:P136".-> v3 v3 --"rdfs:label"--> v4 bind2[/"str(?genreString)"/] v4 --o bind2 bind2 --as--o v9 end subgraph s1["http://wikiba.se/ontology#label"] style s1 stroke-width:4px; c12 --"wikibase:language"--> c2 end bind6[/"?dateYear"/] v8 --o bind6 bind6 --as--o v10 bind7[/"?gameENLabel"/] v5 --o bind7 bind7 --as--o v10 bind8[/"?genreLabel"/] v9 --o bind8 bind8 --as--o v10