ImagineDragon
ImagineDragon

Reputation: 307

bind multiple sparql variables into one so this one variable can be used to order results

Basically I have some data like:

:student1 :hasAScore "4.1"
:student2 :hasAScore "2.7"
:student2 :hasBScore "2.1"

but in my query, I would like to

select * where{
  ?a :hasAScore ?score1
  ?b :hasBScore ?score2
  //bind ?score1 as ?score and bind ?score2 as ?score too, so they can be ranked by "order by"
} order by(?score)

basically i am saying i want to be able to rank students by a score which can be either AScore or BScore. is there anyway i can have some kind of union of ?score1 and ?score2 as ?score so that I can rank by ?score

Upvotes: 0

Views: 704

Answers (1)

Joshua Taylor
Joshua Taylor

Reputation: 85823

is there anyway i can have some kind of union of ?score1 and ?score2 as ?score so that I can rank by ?score

Sure, you can use the appropriately named union, as in the following. I'm assuming that you meant that the same student has both A scores and B scores, so that you actually wanted one variable in the subject position.

select * where { 
  { ?student :hasAScore ?score } 
  union 
  { ?student :hasBScore ?score }
}

You don't even need to use a union here, though. In SPARQL 1.1, property paths were introduced, which means that you can write :hasScoreA|:hasScoreB, as in:

select * where { 
  ?student :hasAScore|:hasBScore ?score
}

Upvotes: 4

Related Questions