将多个结果折叠为一个

时间:2017-05-28 00:37:08

标签: sparql wikidata

我有以下维基数据SPARQL查询:

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)

问题是,例如,当对象有两个平台或两个发布者时,查询会返回多个结果。如何更改它,以便每个游戏只返回一个结果,并将多个平台和发布者连接到一个字符串。例如“xbox,playstation”或“square,eidos”。感谢。

您可以在此处试验查询:

https://query.wikidata.org/

[编辑]

作为实验,我创建了这个脚本:

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

但是,propset字段始终为空。

[编辑]

我设法让分组运作:

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)

但是,我希望脚本只返回英文结果,但SERVICE行不再执行其工作。

[编辑]

我在其他地方得到了一些帮助,这是工作脚本:

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)

我仍然想知道一些事情,但会开始一个新问题。

0 个答案:

没有答案