简体   繁体   中英

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?

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 .
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM