You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by Wetz Peter <pe...@tuwien.ac.at> on 2015/11/24 11:24:00 UTC

BIND in CONSTRUCT query with sub-SELECT (SPARQL)

Related to the answer of a previous question of mine (see [1]) I have a new question: I currently have a CONSTRUCT query with a nested SELECT which makes use of aggregates. What I now want to do is to construct new URIs in the CONSTRUCT statement.

What I want to do is basically similar as can be seen in [2]. There I see that BIND is used in the WHERE class to construct new URIs which are then used in the CONSTRUCT clause. Unfortunately this approach does not work for my case (nested SELECT which resides in the CONSTRUCT's WHERE clause). The difference is, that in this example no nested SELECT is present.

My current query looks like this (simplified):

    PREFIXES
    CONSTRUCT { ?s rdfs:label ?var . }
    WHERE {
        SELECT ?s (AVG(?single) as ?agg) ...
            WHERE {
            ...
            }
        GROUP BY ?s ...
    }

The question is: Where to place a BIND statement which is used to bind values to variables, which are then used in the CONSTRUCT statement (e.g. ?var)?

Thanks for your help.

Best,
Peter

[1] https://mail-archives.apache.org/mod_mbox/jena-users/201511.mbox/%3C19440356.622107.1446464087017.JavaMail.yahoo@mail.yahoo.com%3E
[2] http://mail-archives.apache.org/mod_mbox/jena-users/201111.mbox/%3C4ED66723.7030506@googlemail.com%3E

Re: AW: BIND in CONSTRUCT query with sub-SELECT (SPARQL)

Posted by Andy Seaborne <an...@apache.org>.
On 24/11/15 13:14, Wetz Peter wrote:
> Thanks Rob, this also does the job.
> What are advantages of this approach compared to using BIND() ? Performance?
>
> Thanks again and best,
> Peter
>

Not much difference. They are the same operation with same rules as to 
what you can and can't do.  They happen at slightly different places in 
execution but you can get the same effects with (more) subqueries and 
use of {}

Using BIND at the end of the WHERE clause and using AS in SELECT only 
differs by availability of other variables:

SELECT ?s (AVG(?single) as ?agg) (?agg+10 AS ?var) {
}  GROUP BY ?s

==>

SELECT * {
   {SELECT ?s (AVG(?single) as ?agg) { }
    GROUP BY ?s
   }
   BIND(?agg+10 AS ?var)
}


For fixed expressions, there is also VALUES -- inline data in the query.

	Andy

>> -----Ursprüngliche Nachricht-----
>> Von: Rob Vesse [mailto:rvesse@dotnetrdf.org]
>> Gesendet: Dienstag, 24. November 2015 12:02
>> An: users@jena.apache.org
>> Betreff: Re: BIND in CONSTRUCT query with sub-SELECT (SPARQL)
>>
>> Instead of BIND you can simply use a project expression, this is essentially
>> the same as you would do for a BIND but placed on the SELECT line with
>> variables and aggregates e.g.
>>
>>      PREFIXES
>>      CONSTRUCT { ?s rdfs:label ?var . }
>>      WHERE {
>>        SELECT ?s (AVG(?single) as ?agg) (IRI(expr) AS ?var)
>>        WHERE {
>>          ...
>>        }
>>        GROUP BY ?s ...
>>      }
>>
>> Where expr is some expression that generates your desired constant
>>
>>
>> Rob
>>
>> On 24/11/2015 10:24, "Wetz Peter" <pe...@tuwien.ac.at> wrote:
>>
>>> Related to the answer of a previous question of mine (see [1]) I have a
>>> new question: I currently have a CONSTRUCT query with a nested SELECT
>>> which makes use of aggregates. What I now want to do is to construct
>>> new URIs in the CONSTRUCT statement.
>>>
>>> What I want to do is basically similar as can be seen in [2]. There I
>>> see that BIND is used in the WHERE class to construct new URIs which
>>> are then used in the CONSTRUCT clause. Unfortunately this approach does
>>> not work for my case (nested SELECT which resides in the CONSTRUCT's
>>> WHERE clause). The difference is, that in this example no nested SELECT
>>> is present.
>>>
>>> My current query looks like this (simplified):
>>>
>>>     PREFIXES
>>>     CONSTRUCT { ?s rdfs:label ?var . }
>>>     WHERE {
>>>         SELECT ?s (AVG(?single) as ?agg)
>>>             WHERE {
>>>             ...
>>>             }
>>>         GROUP BY ?s ...
>>>     }
>>>
>>> The question is: Where to place a BIND statement which is used to bind
>>> values to variables, which are then used in the CONSTRUCT statement (e.g.
>>> ?var)?
>>>
>>> Thanks for your help.
>>>
>>> Best,
>>> Peter
>>>
>>> [1]
>>> https://mail-archives.apache.org/mod_mbox/jena-
>> users/201511.mbox/%3C194
>>> 403 56.622107.1446464087017.JavaMail.yahoo@mail.yahoo.com%3E
>>> [2]
>>> http://mail-archives.apache.org/mod_mbox/jena-
>> users/201111.mbox/%3C4ED6
>>> 672
>>> 3.7030506@googlemail.com%3E
>>
>>
>>
>


AW: BIND in CONSTRUCT query with sub-SELECT (SPARQL)

Posted by Wetz Peter <pe...@tuwien.ac.at>.
Thanks Rob, this also does the job.
What are advantages of this approach compared to using BIND() ? Performance?

Thanks again and best,
Peter

> -----Ursprüngliche Nachricht-----
> Von: Rob Vesse [mailto:rvesse@dotnetrdf.org]
> Gesendet: Dienstag, 24. November 2015 12:02
> An: users@jena.apache.org
> Betreff: Re: BIND in CONSTRUCT query with sub-SELECT (SPARQL)
> 
> Instead of BIND you can simply use a project expression, this is essentially
> the same as you would do for a BIND but placed on the SELECT line with
> variables and aggregates e.g.
> 
>     PREFIXES
>     CONSTRUCT { ?s rdfs:label ?var . }
>     WHERE {
>       SELECT ?s (AVG(?single) as ?agg) (IRI(expr) AS ?var)
>       WHERE {
>         ...
>       }
>       GROUP BY ?s ...
>     }
> 
> Where expr is some expression that generates your desired constant
> 
> 
> Rob
> 
> On 24/11/2015 10:24, "Wetz Peter" <pe...@tuwien.ac.at> wrote:
> 
> >Related to the answer of a previous question of mine (see [1]) I have a
> >new question: I currently have a CONSTRUCT query with a nested SELECT
> >which makes use of aggregates. What I now want to do is to construct
> >new URIs in the CONSTRUCT statement.
> >
> >What I want to do is basically similar as can be seen in [2]. There I
> >see that BIND is used in the WHERE class to construct new URIs which
> >are then used in the CONSTRUCT clause. Unfortunately this approach does
> >not work for my case (nested SELECT which resides in the CONSTRUCT's
> >WHERE clause). The difference is, that in this example no nested SELECT
> >is present.
> >
> >My current query looks like this (simplified):
> >
> >    PREFIXES
> >    CONSTRUCT { ?s rdfs:label ?var . }
> >    WHERE {
> >        SELECT ?s (AVG(?single) as ?agg)
> >            WHERE {
> >            ...
> >            }
> >        GROUP BY ?s ...
> >    }
> >
> >The question is: Where to place a BIND statement which is used to bind
> >values to variables, which are then used in the CONSTRUCT statement (e.g.
> >?var)?
> >
> >Thanks for your help.
> >
> >Best,
> >Peter
> >
> >[1]
> >https://mail-archives.apache.org/mod_mbox/jena-
> users/201511.mbox/%3C194
> >403 56.622107.1446464087017.JavaMail.yahoo@mail.yahoo.com%3E
> >[2]
> >http://mail-archives.apache.org/mod_mbox/jena-
> users/201111.mbox/%3C4ED6
> >672
> >3.7030506@googlemail.com%3E
> 
> 
> 


Re: BIND in CONSTRUCT query with sub-SELECT (SPARQL)

Posted by Rob Vesse <rv...@dotnetrdf.org>.
Instead of BIND you can simply use a project expression, this is
essentially the same as you would do for a BIND but placed on the SELECT
line with variables and aggregates e.g.

    PREFIXES
    CONSTRUCT { ?s rdfs:label ?var . }
    WHERE {
      SELECT ?s (AVG(?single) as ?agg) (IRI(expr) AS ?var)
      WHERE {
        ...
      }
      GROUP BY ?s ...
    }

Where expr is some expression that generates your desired constant


Rob

On 24/11/2015 10:24, "Wetz Peter" <pe...@tuwien.ac.at> wrote:

>Related to the answer of a previous question of mine (see [1]) I have a
>new question: I currently have a CONSTRUCT query with a nested SELECT
>which makes use of aggregates. What I now want to do is to construct new
>URIs in the CONSTRUCT statement.
>
>What I want to do is basically similar as can be seen in [2]. There I see
>that BIND is used in the WHERE class to construct new URIs which are then
>used in the CONSTRUCT clause. Unfortunately this approach does not work
>for my case (nested SELECT which resides in the CONSTRUCT's WHERE
>clause). The difference is, that in this example no nested SELECT is
>present.
>
>My current query looks like this (simplified):
>
>    PREFIXES
>    CONSTRUCT { ?s rdfs:label ?var . }
>    WHERE {
>        SELECT ?s (AVG(?single) as ?agg)
>            WHERE {
>            ...
>            }
>        GROUP BY ?s ...
>    }
>
>The question is: Where to place a BIND statement which is used to bind
>values to variables, which are then used in the CONSTRUCT statement (e.g.
>?var)?
>
>Thanks for your help.
>
>Best,
>Peter
>
>[1] 
>https://mail-archives.apache.org/mod_mbox/jena-users/201511.mbox/%3C194403
>56.622107.1446464087017.JavaMail.yahoo@mail.yahoo.com%3E
>[2] 
>http://mail-archives.apache.org/mod_mbox/jena-users/201111.mbox/%3C4ED6672
>3.7030506@googlemail.com%3E





Re: BIND in CONSTRUCT query with sub-SELECT (SPARQL)

Posted by Martynas Jusevičius <ma...@graphity.org>.
In your example, the only variable from WHERE which is used in
CONSTRUCT template is ?s, and you don't show how ?s is obtained. You
aggregate ?agg but you don't use it in the template.

A better example would yield a more definite answer.

On Tue, Nov 24, 2015 at 11:44 AM, Wetz Peter <pe...@tuwien.ac.at> wrote:
> Maybe a valid answer, however in my use case that wouldnt work, because I want to bind a variable which is then used in the CONSTRUCT, but it's a new (!) variable and not a variable which comes from the nested SELECT.
>
> Best
>
>> -----Ursprüngliche Nachricht-----
>> Von: Martynas Jusevičius [mailto:martynas@graphity.org]
>> Gesendet: Dienstag, 24. November 2015 11:38
>> An: users@jena.apache.org
>> Betreff: Re: BIND in CONSTRUCT query with sub-SELECT (SPARQL)
>>
>> The BIND could also be within the sub-SELECT, depends on the use case.
>>
>> On Tue, Nov 24, 2015 at 11:35 AM, Wetz Peter <pe...@tuwien.ac.at>
>> wrote:
>> > To answer my own question, I think this is the correct solution:
>> >
>> >     PREFIX
>> >     CONSTRUCT { ... }
>> >     WHERE {
>> >         BIND () {
>> >             SELECT ...
>> >                 WHERE {
>> >                 ...
>> >                 }
>> >             GROUP BY ...
>> >         }
>> >     }
>> >
>> > Best,
>> > Peter
>> >
>> >> -----Ursprüngliche Nachricht-----
>> >> Von: Wetz Peter [mailto:peter.wetz@tuwien.ac.at]
>> >> Gesendet: Dienstag, 24. November 2015 11:24
>> >> An: users@jena.apache.org
>> >> Betreff: BIND in CONSTRUCT query with sub-SELECT (SPARQL)
>> >>
>> >> Related to the answer of a previous question of mine (see [1]) I have
>> >> a new
>> >> question: I currently have a CONSTRUCT query with a nested SELECT
>> >> which makes use of aggregates. What I now want to do is to construct
>> >> new URIs in the CONSTRUCT statement.
>> >>
>> >> What I want to do is basically similar as can be seen in [2]. There I
>> >> see that BIND is used in the WHERE class to construct new URIs which
>> >> are then used in the CONSTRUCT clause. Unfortunately this approach
>> >> does not work for my case (nested SELECT which resides in the
>> >> CONSTRUCT's WHERE clause). The difference is, that in this example no
>> nested SELECT is present.
>> >>
>> >> My current query looks like this (simplified):
>> >>
>> >>     PREFIXES
>> >>     CONSTRUCT { ?s rdfs:label ?var . }
>> >>     WHERE {
>> >>         SELECT ?s (AVG(?single) as ?agg) ...
>> >>             WHERE {
>> >>             ...
>> >>             }
>> >>         GROUP BY ?s ...
>> >>     }
>> >>
>> >> The question is: Where to place a BIND statement which is used to
>> >> bind values to variables, which are then used in the CONSTRUCT
>> statement (e.g.
>> >> ?var)?
>> >>
>> >> Thanks for your help.
>> >>
>> >> Best,
>> >> Peter
>> >>
>> >> [1] https://mail-archives.apache.org/mod_mbox/jena-
>> >>
>> users/201511.mbox/%3C19440356.622107.1446464087017.JavaMail.yahoo@
>> >> mail.yahoo.com%3E
>> >> [2] http://mail-archives.apache.org/mod_mbox/jena-
>> >> users/201111.mbox/%3C4ED66723.7030506@googlemail.com%3E

AW: BIND in CONSTRUCT query with sub-SELECT (SPARQL)

Posted by Wetz Peter <pe...@tuwien.ac.at>.
Maybe a valid answer, however in my use case that wouldnt work, because I want to bind a variable which is then used in the CONSTRUCT, but it's a new (!) variable and not a variable which comes from the nested SELECT.

Best

> -----Ursprüngliche Nachricht-----
> Von: Martynas Jusevičius [mailto:martynas@graphity.org]
> Gesendet: Dienstag, 24. November 2015 11:38
> An: users@jena.apache.org
> Betreff: Re: BIND in CONSTRUCT query with sub-SELECT (SPARQL)
> 
> The BIND could also be within the sub-SELECT, depends on the use case.
> 
> On Tue, Nov 24, 2015 at 11:35 AM, Wetz Peter <pe...@tuwien.ac.at>
> wrote:
> > To answer my own question, I think this is the correct solution:
> >
> >     PREFIX
> >     CONSTRUCT { ... }
> >     WHERE {
> >         BIND () {
> >             SELECT ...
> >                 WHERE {
> >                 ...
> >                 }
> >             GROUP BY ...
> >         }
> >     }
> >
> > Best,
> > Peter
> >
> >> -----Ursprüngliche Nachricht-----
> >> Von: Wetz Peter [mailto:peter.wetz@tuwien.ac.at]
> >> Gesendet: Dienstag, 24. November 2015 11:24
> >> An: users@jena.apache.org
> >> Betreff: BIND in CONSTRUCT query with sub-SELECT (SPARQL)
> >>
> >> Related to the answer of a previous question of mine (see [1]) I have
> >> a new
> >> question: I currently have a CONSTRUCT query with a nested SELECT
> >> which makes use of aggregates. What I now want to do is to construct
> >> new URIs in the CONSTRUCT statement.
> >>
> >> What I want to do is basically similar as can be seen in [2]. There I
> >> see that BIND is used in the WHERE class to construct new URIs which
> >> are then used in the CONSTRUCT clause. Unfortunately this approach
> >> does not work for my case (nested SELECT which resides in the
> >> CONSTRUCT's WHERE clause). The difference is, that in this example no
> nested SELECT is present.
> >>
> >> My current query looks like this (simplified):
> >>
> >>     PREFIXES
> >>     CONSTRUCT { ?s rdfs:label ?var . }
> >>     WHERE {
> >>         SELECT ?s (AVG(?single) as ?agg) ...
> >>             WHERE {
> >>             ...
> >>             }
> >>         GROUP BY ?s ...
> >>     }
> >>
> >> The question is: Where to place a BIND statement which is used to
> >> bind values to variables, which are then used in the CONSTRUCT
> statement (e.g.
> >> ?var)?
> >>
> >> Thanks for your help.
> >>
> >> Best,
> >> Peter
> >>
> >> [1] https://mail-archives.apache.org/mod_mbox/jena-
> >>
> users/201511.mbox/%3C19440356.622107.1446464087017.JavaMail.yahoo@
> >> mail.yahoo.com%3E
> >> [2] http://mail-archives.apache.org/mod_mbox/jena-
> >> users/201111.mbox/%3C4ED66723.7030506@googlemail.com%3E

Re: BIND in CONSTRUCT query with sub-SELECT (SPARQL)

Posted by Martynas Jusevičius <ma...@graphity.org>.
The BIND could also be within the sub-SELECT, depends on the use case.

On Tue, Nov 24, 2015 at 11:35 AM, Wetz Peter <pe...@tuwien.ac.at> wrote:
> To answer my own question, I think this is the correct solution:
>
>     PREFIX
>     CONSTRUCT { ... }
>     WHERE {
>         BIND () {
>             SELECT ...
>                 WHERE {
>                 ...
>                 }
>             GROUP BY ...
>         }
>     }
>
> Best,
> Peter
>
>> -----Ursprüngliche Nachricht-----
>> Von: Wetz Peter [mailto:peter.wetz@tuwien.ac.at]
>> Gesendet: Dienstag, 24. November 2015 11:24
>> An: users@jena.apache.org
>> Betreff: BIND in CONSTRUCT query with sub-SELECT (SPARQL)
>>
>> Related to the answer of a previous question of mine (see [1]) I have a new
>> question: I currently have a CONSTRUCT query with a nested SELECT which
>> makes use of aggregates. What I now want to do is to construct new URIs in
>> the CONSTRUCT statement.
>>
>> What I want to do is basically similar as can be seen in [2]. There I see that
>> BIND is used in the WHERE class to construct new URIs which are then used in
>> the CONSTRUCT clause. Unfortunately this approach does not work for my
>> case (nested SELECT which resides in the CONSTRUCT's WHERE clause). The
>> difference is, that in this example no nested SELECT is present.
>>
>> My current query looks like this (simplified):
>>
>>     PREFIXES
>>     CONSTRUCT { ?s rdfs:label ?var . }
>>     WHERE {
>>         SELECT ?s (AVG(?single) as ?agg) ...
>>             WHERE {
>>             ...
>>             }
>>         GROUP BY ?s ...
>>     }
>>
>> The question is: Where to place a BIND statement which is used to bind
>> values to variables, which are then used in the CONSTRUCT statement (e.g.
>> ?var)?
>>
>> Thanks for your help.
>>
>> Best,
>> Peter
>>
>> [1] https://mail-archives.apache.org/mod_mbox/jena-
>> users/201511.mbox/%3C19440356.622107.1446464087017.JavaMail.yahoo@
>> mail.yahoo.com%3E
>> [2] http://mail-archives.apache.org/mod_mbox/jena-
>> users/201111.mbox/%3C4ED66723.7030506@googlemail.com%3E

AW: BIND in CONSTRUCT query with sub-SELECT (SPARQL)

Posted by Wetz Peter <pe...@tuwien.ac.at>.
To answer my own question, I think this is the correct solution:

    PREFIX
    CONSTRUCT { ... }
    WHERE {
        BIND () {
            SELECT ...
                WHERE {
                ...
                }
            GROUP BY ...
        } 
    }

Best,
Peter

> -----Ursprüngliche Nachricht-----
> Von: Wetz Peter [mailto:peter.wetz@tuwien.ac.at]
> Gesendet: Dienstag, 24. November 2015 11:24
> An: users@jena.apache.org
> Betreff: BIND in CONSTRUCT query with sub-SELECT (SPARQL)
> 
> Related to the answer of a previous question of mine (see [1]) I have a new
> question: I currently have a CONSTRUCT query with a nested SELECT which
> makes use of aggregates. What I now want to do is to construct new URIs in
> the CONSTRUCT statement.
> 
> What I want to do is basically similar as can be seen in [2]. There I see that
> BIND is used in the WHERE class to construct new URIs which are then used in
> the CONSTRUCT clause. Unfortunately this approach does not work for my
> case (nested SELECT which resides in the CONSTRUCT's WHERE clause). The
> difference is, that in this example no nested SELECT is present.
> 
> My current query looks like this (simplified):
> 
>     PREFIXES
>     CONSTRUCT { ?s rdfs:label ?var . }
>     WHERE {
>         SELECT ?s (AVG(?single) as ?agg) ...
>             WHERE {
>             ...
>             }
>         GROUP BY ?s ...
>     }
> 
> The question is: Where to place a BIND statement which is used to bind
> values to variables, which are then used in the CONSTRUCT statement (e.g.
> ?var)?
> 
> Thanks for your help.
> 
> Best,
> Peter
> 
> [1] https://mail-archives.apache.org/mod_mbox/jena-
> users/201511.mbox/%3C19440356.622107.1446464087017.JavaMail.yahoo@
> mail.yahoo.com%3E
> [2] http://mail-archives.apache.org/mod_mbox/jena-
> users/201111.mbox/%3C4ED66723.7030506@googlemail.com%3E