Select only the first object from sparql query

2019-02-12 18:08发布

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

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.

标签: rdf sparql
1条回答
何必那么认真
2楼-- · 2019-02-12 18:34

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.

For example, something like this:

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
查看更多
登录 后发表回答