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:
[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.