I found the new book titled Learning SPARQL to be extremely weak in their 
coverage of subqueries, devoting less than a single page to the topic. Thanks 
for clarifying Damian!


-----Original Message-----
From: Damian Steer [mailto:[email protected]] 
Sent: Wednesday, October 05, 2011 2:40 PM
To: [email protected]; [email protected]
Subject: Re: Scoping in subqueries


On 5 Oct 2011, at 18:40, Eric Scott wrote:

> Hi all-

Hi Eric,

> I've tried using a subquery like this:
> 
> SELECT ?entity ?arbitraryLabel
> where
> {
>   ?entity my:hasFeature my:VeryImportantFeature.
>   {
>    SELECT ?arbitraryLabel
>    {
>      ?entity my:hasLabelSet ?labelSet.
>      ?labelSet my:hasLabel ?arbitraryLabel.
>    }
>    LIMIT 1
>   }
> }
> 
> on the assumption that the variables in the main query are scoped in such a 
> way that they are shared with the subquery.

What's happening is this: sparql is evaluated from inside out. So your inner 
select happens first, and you get one row:

?arbitraryLabel =  "unrelated label 25"

which is then (cross) joined with the result of  ?entity my:hasFeature 
my:VeryImportantFeature.

You could make the inner select:

{
  SELECT ?entity ?arbitraryLabel
...
}

which gives a more understandable result, perhaps, in that you'll get the 
entity with that label returned.

One way to get what you want is:

SELECT ?entity (SAMPLE(?label) AS ?arbitraryLabel) where {
     ?entity my:hasLabelSet ?labelSet .
     ?labelSet my:hasLabel ?label .
}
GROUP BY ?entity

which says: I want one entity per row. Pick any label.

Damian

Reply via email to