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/10/01 21:46:31 UTC

sparql query: how to force a blank variable to be 0?

Hi all,

I'm trying to perform sparql query to find <video_id, category, score>
triples with an union
aggregating all the matching scores over a video_id.
However, the score is returned as blank. Here's how I construct the query

select ?v (sum(?s99*2+?s0) AS ?ss) where {
 { ?v <http://onescreen.com/video_freebase#/music/track>  ?s99 }
union
 { ?v <http://onescreen.com/video_freebase#/music>  ?s0 }
} group by ?v order by desc (?ss) limit 100

---------------------------------------------
| v                                    | ss |
=============================================
| <http://onescreen.com/video#1732760> |    |
| <http://onescreen.com/video#1732780> |    |
| <http://onescreen.com/video#1732800> |    |


but selecting with the specific video_id, the record does have both, but on
different columns:

select ?s0 ?s99 where {
 { <http://onescreen.com/video#1732760> <
http://onescreen.com/video_freebase#/music>  ?s0 }
union
{ <http://onescreen.com/video#1732760> <
http://onescreen.com/video_freebase#/music/track>  ?s99 }
}

-------------------------------------------------------------------------------------------------------------
| s0                                                  | s99
                                     |
=============================================================================================================
| "0.01532"^^<http://www.w3.org/2001/XMLSchema#float> |
                                     |
|                                                     |
"0.00739"^^<http://www.w3.org/2001/XMLSchema#float> |
-------------------------------------------------------------------------------------------------------------

looks like it is caused by blank variable, either sum() or + with a blank
causes the result to be blank.
I noticed that I couldn't map the two columns into the same variable name,
as I'd like to apply different weights ?s99 and ?s0
to achieve sum(?s99*2+?s0)

so, with which function could I make a variable to be 0 when it is blank?
what is the correct way to construct this query?


Thank you.

Yuhan

Re: sparql query: how to force a blank variable to be 0?

Posted by Yuhan Zhang <yz...@onescreen.com>.
Hi Andy and all,

I finally found the name of the function "bound" as checking for undefined
variable. :)
combined with the "if" function to map undefined to value to 0, the query
becomes:

select ?v (sum(if(!bound(?s99), 0, ?s99)*2+ if(!bound(?s0), 0, ?s0)) AS
?ss) where {
 { ?v <http://onescreen.com/video_freebase#/music/track>  ?s99 }
union
 { ?v <http://onescreen.com/video_freebase#/music>  ?s0 }
} group by ?v order by desc (?ss) limit 100


Thanks for the help!


Yuhan

On Mon, Oct 1, 2012 at 2:36 PM, Yuhan Zhang <yz...@onescreen.com> wrote:

> Hi Andy
>
> I think this query will give me the videos that have both ?s0 and ?s99. but
> I'm also interested at the records having either of the two category (or
> both)
> (union is used as an or operation)
>
> {
>  ?v <http://onescreen.com/video_freebase#/music/track>  ?s99 .
>  ?v <http://onescreen.com/video_freebase#/music>  ?s0
> }
>
> is there a function to detect undefined value and return zero in sparql?
>
>
> Thank you.
>
> Yuhan
>
>
>
>
> On Mon, Oct 1, 2012 at 1:38 PM, Andy Seaborne <an...@apache.org> wrote:
>
>> On 01/10/12 20:46, Yuhan Zhang wrote:
>>
>>> Hi all,
>>>
>>> I'm trying to perform sparql query to find <video_id, category, score>
>>> triples with an union
>>> aggregating all the matching scores over a video_id.
>>> However, the score is returned as blank. Here's how I construct the query
>>>
>>> select ?v (sum(?s99*2+?s0) AS ?ss) where {
>>>   { ?v <http://onescreen.com/video_**freebase#/music/track<http://onescreen.com/video_freebase#/music/track>>
>>>  ?s99 }
>>> union
>>>   { ?v <http://onescreen.com/video_**freebase#/music<http://onescreen.com/video_freebase#/music>>
>>>  ?s0 }
>>> } group by ?v order by desc (?ss) limit 100
>>>
>>
>> Do you really mean that?
>>
>> To do sum(?s99*2+?s0) you want ?s99 and ?s0 in the same row for each ?v
>> which is:
>>
>> {
>>  ?v <http://onescreen.com/video_**freebase#/music/track<http://onescreen.com/video_freebase#/music/track>>
>>  ?s99
>>  ?v <http://onescreen.com/video_**freebase#/music<http://onescreen.com/video_freebase#/music>>
>>  ?s0
>> }
>>
>> The score is returned as blank because eval of the SUM is an error (the
>> expression (?s99*2+?s0) always has one variable or the other unbound so the
>> evaluation has an undef i.e error.
>>
>>
>>
>>> ------------------------------**---------------
>>> | v                                    | ss |
>>> ==============================**===============
>>> | <http://onescreen.com/video#**1732760<http://onescreen.com/video#1732760>>
>>> |    |
>>> | <http://onescreen.com/video#**1732780<http://onescreen.com/video#1732780>>
>>> |    |
>>> | <http://onescreen.com/video#**1732800<http://onescreen.com/video#1732800>>
>>> |    |
>>>
>>>
>>> but selecting with the specific video_id, the record does have both, but
>>> on
>>> different columns:
>>>
>>> select ?s0 ?s99 where {
>>>   { <http://onescreen.com/video#**1732760<http://onescreen.com/video#1732760>>
>>> <
>>> http://onescreen.com/video_**freebase#/music<http://onescreen.com/video_freebase#/music>>
>>>  ?s0 }
>>> union
>>> { <http://onescreen.com/video#**1732760<http://onescreen.com/video#1732760>>
>>> <
>>> http://onescreen.com/video_**freebase#/music/track<http://onescreen.com/video_freebase#/music/track>>
>>>  ?s99 }
>>> }
>>>
>>> ------------------------------**------------------------------**
>>> ------------------------------**-------------------
>>> | s0                                                  | s99
>>>                                       |
>>> ==============================**==============================**
>>> ==============================**===================
>>> | "0.01532"^^<http://www.w3.org/**2001/XMLSchema#float<http://www.w3.org/2001/XMLSchema#float>>
>>> |
>>>                                       |
>>> |                                                     |
>>> "0.00739"^^<http://www.w3.org/**2001/XMLSchema#float<http://www.w3.org/2001/XMLSchema#float>>
>>> |
>>> ------------------------------**------------------------------**
>>> ------------------------------**-------------------
>>>
>>> looks like it is caused by blank variable, either sum() or + with a blank
>>> causes the result to be blank.
>>> I noticed that I couldn't map the two columns into the same variable
>>> name,
>>> as I'd like to apply different weights ?s99 and ?s0
>>> to achieve sum(?s99*2+?s0)
>>>
>>> so, with which function could I make a variable to be 0 when it is blank?
>>> what is the correct way to construct this query?
>>>
>>>
>>> Thank you.
>>>
>>> Yuhan
>>>
>>>
>>
>
>
>


-- 
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: sparql query: how to force a blank variable to be 0?

Posted by Yuhan Zhang <yz...@onescreen.com>.
Hi Andy

I think this query will give me the videos that have both ?s0 and ?s99. but
I'm also interested at the records having either of the two category (or
both)
(union is used as an or operation)

{
 ?v <http://onescreen.com/video_freebase#/music/track>  ?s99 .
 ?v <http://onescreen.com/video_freebase#/music>  ?s0
}

is there a function to detect undefined value and return zero in sparql?


Thank you.

Yuhan



On Mon, Oct 1, 2012 at 1:38 PM, Andy Seaborne <an...@apache.org> wrote:

> On 01/10/12 20:46, Yuhan Zhang wrote:
>
>> Hi all,
>>
>> I'm trying to perform sparql query to find <video_id, category, score>
>> triples with an union
>> aggregating all the matching scores over a video_id.
>> However, the score is returned as blank. Here's how I construct the query
>>
>> select ?v (sum(?s99*2+?s0) AS ?ss) where {
>>   { ?v <http://onescreen.com/video_**freebase#/music/track<http://onescreen.com/video_freebase#/music/track>>
>>  ?s99 }
>> union
>>   { ?v <http://onescreen.com/video_**freebase#/music<http://onescreen.com/video_freebase#/music>>
>>  ?s0 }
>> } group by ?v order by desc (?ss) limit 100
>>
>
> Do you really mean that?
>
> To do sum(?s99*2+?s0) you want ?s99 and ?s0 in the same row for each ?v
> which is:
>
> {
>  ?v <http://onescreen.com/video_**freebase#/music/track<http://onescreen.com/video_freebase#/music/track>>
>  ?s99
>  ?v <http://onescreen.com/video_**freebase#/music<http://onescreen.com/video_freebase#/music>>
>  ?s0
> }
>
> The score is returned as blank because eval of the SUM is an error (the
> expression (?s99*2+?s0) always has one variable or the other unbound so the
> evaluation has an undef i.e error.
>
>
>
>> ------------------------------**---------------
>> | v                                    | ss |
>> ==============================**===============
>> | <http://onescreen.com/video#**1732760<http://onescreen.com/video#1732760>>
>> |    |
>> | <http://onescreen.com/video#**1732780<http://onescreen.com/video#1732780>>
>> |    |
>> | <http://onescreen.com/video#**1732800<http://onescreen.com/video#1732800>>
>> |    |
>>
>>
>> but selecting with the specific video_id, the record does have both, but
>> on
>> different columns:
>>
>> select ?s0 ?s99 where {
>>   { <http://onescreen.com/video#**1732760<http://onescreen.com/video#1732760>>
>> <
>> http://onescreen.com/video_**freebase#/music<http://onescreen.com/video_freebase#/music>>
>>  ?s0 }
>> union
>> { <http://onescreen.com/video#**1732760<http://onescreen.com/video#1732760>>
>> <
>> http://onescreen.com/video_**freebase#/music/track<http://onescreen.com/video_freebase#/music/track>>
>>  ?s99 }
>> }
>>
>> ------------------------------**------------------------------**
>> ------------------------------**-------------------
>> | s0                                                  | s99
>>                                       |
>> ==============================**==============================**
>> ==============================**===================
>> | "0.01532"^^<http://www.w3.org/**2001/XMLSchema#float<http://www.w3.org/2001/XMLSchema#float>>
>> |
>>                                       |
>> |                                                     |
>> "0.00739"^^<http://www.w3.org/**2001/XMLSchema#float<http://www.w3.org/2001/XMLSchema#float>>
>> |
>> ------------------------------**------------------------------**
>> ------------------------------**-------------------
>>
>> looks like it is caused by blank variable, either sum() or + with a blank
>> causes the result to be blank.
>> I noticed that I couldn't map the two columns into the same variable name,
>> as I'd like to apply different weights ?s99 and ?s0
>> to achieve sum(?s99*2+?s0)
>>
>> so, with which function could I make a variable to be 0 when it is blank?
>> what is the correct way to construct this query?
>>
>>
>> Thank you.
>>
>> Yuhan
>>
>>
>

Re: sparql query: how to force a blank variable to be 0?

Posted by Andy Seaborne <an...@apache.org>.
On 01/10/12 20:46, Yuhan Zhang wrote:
> Hi all,
>
> I'm trying to perform sparql query to find <video_id, category, score>
> triples with an union
> aggregating all the matching scores over a video_id.
> However, the score is returned as blank. Here's how I construct the query
>
> select ?v (sum(?s99*2+?s0) AS ?ss) where {
>   { ?v <http://onescreen.com/video_freebase#/music/track>  ?s99 }
> union
>   { ?v <http://onescreen.com/video_freebase#/music>  ?s0 }
> } group by ?v order by desc (?ss) limit 100

Do you really mean that?

To do sum(?s99*2+?s0) you want ?s99 and ?s0 in the same row for each ?v 
which is:

{
  ?v <http://onescreen.com/video_freebase#/music/track>  ?s99
  ?v <http://onescreen.com/video_freebase#/music>  ?s0
}

The score is returned as blank because eval of the SUM is an error (the 
expression (?s99*2+?s0) always has one variable or the other unbound so 
the evaluation has an undef i.e error.

>
> ---------------------------------------------
> | v                                    | ss |
> =============================================
> | <http://onescreen.com/video#1732760> |    |
> | <http://onescreen.com/video#1732780> |    |
> | <http://onescreen.com/video#1732800> |    |
>
>
> but selecting with the specific video_id, the record does have both, but on
> different columns:
>
> select ?s0 ?s99 where {
>   { <http://onescreen.com/video#1732760> <
> http://onescreen.com/video_freebase#/music>  ?s0 }
> union
> { <http://onescreen.com/video#1732760> <
> http://onescreen.com/video_freebase#/music/track>  ?s99 }
> }
>
> -------------------------------------------------------------------------------------------------------------
> | s0                                                  | s99
>                                       |
> =============================================================================================================
> | "0.01532"^^<http://www.w3.org/2001/XMLSchema#float> |
>                                       |
> |                                                     |
> "0.00739"^^<http://www.w3.org/2001/XMLSchema#float> |
> -------------------------------------------------------------------------------------------------------------
>
> looks like it is caused by blank variable, either sum() or + with a blank
> causes the result to be blank.
> I noticed that I couldn't map the two columns into the same variable name,
> as I'd like to apply different weights ?s99 and ?s0
> to achieve sum(?s99*2+?s0)
>
> so, with which function could I make a variable to be 0 when it is blank?
> what is the correct way to construct this query?
>
>
> Thank you.
>
> Yuhan
>