You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by Yuhan Zhang <yz...@onescreen.com> on 2012/09/28 03:17:02 UTC
is it possible to apply min(a,b) function in sparql
Hi all,
I'm trying to perform a dot product between two values, and sum them up.
Here's the query:
select ?v ( sum(?s1 * ?s2) AS ?ss)
where {
{ <http://onescreen.com/video#2389800> <
http://onescreen.com/video_freebase#/american_football> ?s1 . ?v <
http://onescreen.com/video_freebase#/american_football> ?s2 }
UNION
{ <http://onescreen.com/video#2389800> <
http://onescreen.com/video_freebase#/american_football/football_player> ?s1
. ?v <http://onescreen.com/video_freebase#/american_football/football_player>
?s2 }
} group by ?v order by DESC (?ss) limit 100
However, instead of sum(?s1 * ?s2) , I'd like to have something like
sum(?s1 * min(?s1*?s2) ) , to multiple ?s2 but not more than ?s1. and I
reached syntax error, as min function meant to be used to aggregate across
columns within one variable.
so, my question is, is this calculation possible to achieve easily from
sparql? sum(?s1 * min(?s1*?s2) ) ? How should I make this happen?
Thank you.
Yuhan
Re: is it possible to apply min(a,b) function in sparql
Posted by Yuhan Zhang <yz...@onescreen.com>.
Wow, cool. The IF statement solved my problem. will read into the useful
ARQ functions. thanks for the help, Andy. :)
Yuhan
On Fri, Sep 28, 2012 at 1:18 AM, Andy Seaborne <an...@apache.org> wrote:
> On 28/09/12 02:17, Yuhan Zhang wrote:
>
>> Hi all,
>>
>> I'm trying to perform a dot product between two values, and sum them up.
>> Here's the query:
>>
>> select ?v ( sum(?s1 * ?s2) AS ?ss)
>> where {
>> { <http://onescreen.com/video#**2389800<http://onescreen.com/video#2389800>>
>> <
>> http://onescreen.com/video_**freebase#/american_football<http://onescreen.com/video_freebase#/american_football>>
>> ?s1 . ?v <
>> http://onescreen.com/video_**freebase#/american_football<http://onescreen.com/video_freebase#/american_football>>
>> ?s2 }
>> UNION
>> { <http://onescreen.com/video#**2389800<http://onescreen.com/video#2389800>>
>> <
>> http://onescreen.com/video_**freebase#/american_football/**
>> football_player<http://onescreen.com/video_freebase#/american_football/football_player>>
>> ?s1
>> . ?v <http://onescreen.com/video_**freebase#/american_football/**
>> football_player<http://onescreen.com/video_freebase#/american_football/football_player>
>> >
>> ?s2 }
>> } group by ?v order by DESC (?ss) limit 100
>>
>>
>> However, instead of sum(?s1 * ?s2) , I'd like to have something like
>> sum(?s1 * min(?s1*?s2) ) , to multiple ?s2 but not more than ?s1. and I
>>
>
> sum(?s1 * min(?s1, ?s2) ) presumably
>
>
> reached syntax error, as min function meant to be used to aggregate across
>> columns within one variable.
>>
>> so, my question is, is this calculation possible to achieve easily from
>> sparql? sum(?s1 * min(?s1*?s2) ) ? How should I make this happen?
>>
>>
>> Thank you.
>>
>> Yuhan
>>
>>
> Two ways:
>
> 1/
> IF(?s1<?s2, ?s1, ?s2)
>
> SPARQL IF returns values.
>
>
> 2/
> See
> http://jena.apache.org/**documentation/query/library-**function.html<http://jena.apache.org/documentation/query/library-function.html>
>
> PREFIX afn <http://jena.hpl.hp.com/ARQ/**function#<http://jena.hpl.hp.com/ARQ/function#>
> >
> afn:min(?s1, ?s2)
>
>
> MIN is only an aggregation function and you can't directly put aggregation
> inside an aggregation because the outer aggregator executes the expression
> for each row as it goes along but that expression contains something that
> was calculated using every row of the group. You can do it with a subquery.
>
> Andy
>
>
>
--
Yuhan Zhang
Senior Software Engineer
OneScreen Inc.
yzhang@onescreen.com <eh...@onescreen.com>
www.onescreen.com
(949) 525-4825 Ext: 177
The information contained in this e-mail is for the exclusive use of the
intended recipient(s) and may be confidential, proprietary, and/or legally
privileged. Inadvertent disclosure of this message does not constitute a
waiver of any privilege. If you receive this message in error, please do
not directly or indirectly print, copy, retransmit, disseminate, or
otherwise use the information. In addition, please delete this e-mail and
all copies and notify the sender.
Re: is it possible to apply min(a,b) function in sparql
Posted by Andy Seaborne <an...@apache.org>.
On 28/09/12 02:17, Yuhan Zhang wrote:
> Hi all,
>
> I'm trying to perform a dot product between two values, and sum them up.
> Here's the query:
>
> select ?v ( sum(?s1 * ?s2) AS ?ss)
> where {
> { <http://onescreen.com/video#2389800> <
> http://onescreen.com/video_freebase#/american_football> ?s1 . ?v <
> http://onescreen.com/video_freebase#/american_football> ?s2 }
> UNION
> { <http://onescreen.com/video#2389800> <
> http://onescreen.com/video_freebase#/american_football/football_player> ?s1
> . ?v <http://onescreen.com/video_freebase#/american_football/football_player>
> ?s2 }
> } group by ?v order by DESC (?ss) limit 100
>
>
> However, instead of sum(?s1 * ?s2) , I'd like to have something like
> sum(?s1 * min(?s1*?s2) ) , to multiple ?s2 but not more than ?s1. and I
sum(?s1 * min(?s1, ?s2) ) presumably
> reached syntax error, as min function meant to be used to aggregate across
> columns within one variable.
>
> so, my question is, is this calculation possible to achieve easily from
> sparql? sum(?s1 * min(?s1*?s2) ) ? How should I make this happen?
>
>
> Thank you.
>
> Yuhan
>
Two ways:
1/
IF(?s1<?s2, ?s1, ?s2)
SPARQL IF returns values.
2/
See
http://jena.apache.org/documentation/query/library-function.html
PREFIX afn <http://jena.hpl.hp.com/ARQ/function#>
afn:min(?s1, ?s2)
MIN is only an aggregation function and you can't directly put
aggregation inside an aggregation because the outer aggregator executes
the expression for each row as it goes along but that expression
contains something that was calculated using every row of the group.
You can do it with a subquery.
Andy