fangio
fangio

Reputation: 1786

SPARQL same predicates with different values

I am using SPARQL to query my data from the triplestore. In the triplestore I have a form containing a table, the table has different table entries. Each entry has a number of predicates. I want to use the predicates index and cost. My query needs to catch the first 2 rows, indices 0 and 1 and save the cost under a specific variable for each.

So I have tried the following (prefix ext is ommited) :

SELECT DISTINCT ?entry ?priceOne ?priceTwo
  WHERE {
    ?form ext:table ?table.
    ?table ext:entry ?entry.
    ?entry ext:index 0;
       ext:cost ?priceOne.
    ?entry ext:index 1;
       ext:cost ?priceTwo.
}

This however does not show any values, if I remove the second part (with index 1) than I do get ?priceOne. How can I get both values?

Upvotes: 0

Views: 220

Answers (1)

Your current query finds each ?entry that has both ext:index values, 0 and 1. You could avoid this by using something like ?entry0 and ?entry1, essentially duplicating your triple patterns.

But typically, you would match alternatives with UNION:

SELECT DISTINCT ?entry ?priceOne ?priceTwo
WHERE {

    # a shorter way to specify this, if you don’t need the ?table variable
    ?form ext:table/ext:entry ?entry .

    {
      ?entry ext:index 0 ;
             ext:cost ?priceOne .
    }
    UNION
    {
      ?entry ext:index 1 ;
             ext:cost ?priceTwo .
    }

}

Upvotes: 1

Related Questions