Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select only the first object from sparql query

Tags:

rdf

sparql

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.

like image 696
user1012480 Avatar asked Mar 18 '13 13:03

user1012480


People also ask

What is a prefix in SPARQL query?

"PREFIX", however (without the "@"), is the SPARQL instruction for a declaration of a namespace prefix. It allows you to write prefixed names in queries instead of having to use full URIs everywhere. So it's a syntax convenience mechanism for shorter, easier to read (and write) queries.

What is optional SPARQL?

OPTIONAL is a binary operator that combines two graph patterns. The optional pattern is any group pattern and may involve any SPARQL pattern types. If the group matches, the solution is extended, if not, the original solution is given (q-opt3. rq).

What is bind in SPARQL?

BIND. SPARQL's BIND function allows us to assign a value to a variable.


1 Answers

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
like image 57
Jukka Matilainen Avatar answered Oct 13 '22 22:10

Jukka Matilainen