0

I have the following Wikidata SPARQL query:

SELECT ?game ?gameLabel ?enwiki ?genreLabel ?devLabel ?publisherLabel ?platformLabel ?dateYear WHERE {
    ?game wdt:P31 wd:Q7889;
    wdt:P136 wd:Q744038.
    OPTIONAL {
      ?enwiki schema:about ?game;
      schema:isPartOf <https://en.wikipedia.org/>
    }
    OPTIONAL {?game wdt:P136 ?genre}
    OPTIONAL {?game wdt:P178 ?dev}
    OPTIONAL {?game wdt:P123 ?publisher}
    OPTIONAL {?game wdt:P400 ?platform}
    OPTIONAL {
      ?game wdt:P577 ?date;
      BIND( year(?date) as ?dateYear )
    }
    SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
} ORDER BY ASC (?dateYear) asc(?gameLabel)

The problem is that the query is returning multiple results when, for instance, the object has two platforms or two publishers. How can I change it so that only one results is returned per game, and the multiple platforms and publishers are concatenated to a single string. For instance "xbox, playstation" or "square, eidos". Thanks.

You can experiment with the query here:

https://query.wikidata.org/

[edit]

As an experiment, I created this script:

SELECT ?game (group_concat(distinct ?gameLabel ; separator = ",") AS ?propset) WHERE {
    ?game wdt:P31 wd:Q7889;
    wdt:P136 wd:Q744038.
    SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
} group by $game

However, the propset field is always empty.

[edit]

I managed to get grouping functioning:

SELECT
  ?game
  (group_concat(distinct ?genreLabel ; separator = ",") AS ?genreLabels)
  WHERE {
    ?game wdt:P31 wd:Q7889;
    wdt:P136 wd:Q744038.
    OPTIONAL {
      ?game wdt:P136 ?genre.
      ?genre rdfs:label ?genreLabel.
    }
    SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
} group by $game
ORDER BY ASC (?dateYear) ASC (?gameLabel)

However, I want the script to only return English results, but the SERVICE line is not doing its job any more.

[edit]

I got some help elsewhere, and here is the working script:

SELECT
  ?game
  (group_concat(distinct         ?enwiki ; separator = ", ") AS         ?enwikis)
  (group_concat(distinct      ?gameLabel ; separator = ", ") AS      ?gameLabels)
  (group_concat(distinct     ?genreLabel ; separator = ", ") AS     ?genreLabels)
  (group_concat(distinct ?developerLabel ; separator = ", ") AS ?developerLabels)
  (group_concat(distinct ?publisherLabel ; separator = ", ") AS ?publisherLabels)
  (group_concat(distinct  ?platformLabel ; separator = ", ") AS  ?platformLabels)
  (group_concat(distinct       ?dateYear ; separator = ", ") AS       ?dateYears)
  WHERE {
    ?game wdt:P31 wd:Q7889;
    wdt:P136 wd:Q744038.
    OPTIONAL {
      ?enwiki schema:about ?game;
      schema:isPartOf <https://en.wikipedia.org/>
    }
    OPTIONAL {?game wdt:P136     ?genre}
    OPTIONAL {?game wdt:P178 ?developer}
    OPTIONAL {?game wdt:P123 ?publisher}
    OPTIONAL {?game wdt:P400  ?platform}
    OPTIONAL {
      ?game wdt:P577 ?date;
      BIND( year(?date) as ?dateYear )
    }
    SERVICE wikibase:label {
      bd:serviceParam wikibase:language "en".
           ?game rdfs:label      ?gameLabel.
          ?genre rdfs:label     ?genreLabel.
      ?developer rdfs:label ?developerLabel.
      ?publisher rdfs:label ?publisherLabel.
       ?platform rdfs:label  ?platformLabel.
    }
} GROUP BY $game
ORDER BY ASC (?dateYear) ASC (?gameLabel)

I still want to know how to some stuff but will start a new question.

posfan12
  • 2,541
  • 8
  • 35
  • 57
  • 1
    Your solution is correct but it does **not** work for the Wikidata label server since this is some special non-standard SPARQL feature. If you try the same query for e.g. platforms it works. – UninformedUser May 28 '17 at 04:12
  • Yeah, it works if I change `?gameLabel` to `?game`. I don't know why it doesn't like the labels (they get created automatically by the software somehow). – posfan12 May 28 '17 at 06:12
  • 1
    Yes, and that's a non-standard SPARQL way, thus, it's probably not executed in the query but afterwards and simply expands the variable values by its corresponding labels (if exist) – UninformedUser May 28 '17 at 07:46
  • I was able to get the labels working using `rdfs:label`, but the script is returning results for every language. I just want English results. The `SERVICE` line supposed to limit the results, but it's not happening. – posfan12 May 28 '17 at 08:06
  • 1
    No, as I said, the `SERVICE` is some special feature of the Blazegraph triple store. What you'Re doing now is the standard SPARQL way, e.g. you have to filter it by `FILTER(LANGMATCHES(LANG(?genreLabel), "en"))` – UninformedUser May 28 '17 at 08:28

0 Answers0