仅从 sparql 查询中选择第一个对象 [英] Select only the first object from sparql query
问题描述
我想从 dbpedia 获取 Daft Punk 的唱片,并且对于我想展示的每张专辑:1) 标题2)发布年份3)维基百科页面,所以我写了这个查询:
I would like to get Daft Punk's discography from dbpedia, and for each album I would like to show: 1) The title 2) The release year 3) The wikipedia page, so I wrote this query:
PREFIX d: <http://dbpedia.org/ontology/>
PREFIX prop: <http://dbpedia.org/property/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX : <http://dbpedia.org/resource/>
SELECT DISTINCT str(?name) YEAR(?relDate) AS ?relYear ?wiki WHERE {
?album a d:Album .
?album foaf:name ?name .
?album d:artist :Daft_Punk .
?album foaf:isPrimaryTopicOf ?wiki .
?album d:releaseDate ?relDate .
}
ORDER BY ?relDate
哪个有效,除了一张专辑 (Alive 2007) 有 2 个不同的发行年份(如您所见 此处),但我只想展示最早的二.我应该如何编辑我的查询以使这张专辑在第一次约会时只出现一次?感谢您的回答.
Which works, except for the fact that there is one album (Alive 2007) which has 2 different release years (as you can see here), but I would like to show only the earliest of the two. How should I edit my query to make this album appear only once with the first date? Thanks for your answers.
推荐答案
您可以使用 MIN
aggregate 提取发布年份的最小值,当结果已使用 GROUP BY
特性 SPARQL 1.1.
You could use the MIN
aggregate to extract the minimum value of the release year, when the result has been grouped using the GROUP BY
feature of SPARQL 1.1.
例如,像这样:
PREFIX d: <http://dbpedia.org/ontology/>
PREFIX prop: <http://dbpedia.org/property/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX : <http://dbpedia.org/resource/>
SELECT str(?name) MIN(YEAR(?relDate)) AS ?relYear ?wiki
WHERE {
?album a d:Album .
?album foaf:name ?name .
?album d:artist :Daft_Punk .
?album foaf:isPrimaryTopicOf ?wiki .
?album d:releaseDate ?relDate .
}
GROUP BY ?name ?wiki
ORDER BY ?relYear
这篇关于仅从 sparql 查询中选择第一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!