tags:

views:

411

answers:

1

I am getting comfortable writing regular queries in SPARQL, but I'm still having trouble with fancier stuff. My latest problem is trying to select everything except stuff that matches the where clause. For instance, say I want to find all the husbands who like a car color that their wife doesn't like (I'm working on a proprietary model, so excuse the example and just trust that it makes sense in the real model).

I might have:

What's the query that selects carl and eric, but not bob? Bonus points if you can select blue and black in the same query. Selecting bob would be simply:

select ?husband ?color where {?husband ?wife . ?husband ?color . ?wife ?color}

What I'm looking for is:

select ?husband ?color where {?husband ?wife . ?husband ?color . NOT (?wife ?color)}

but obviously that's wrong. So what's right?

+2  A: 

One correct answer I found through other sources is something like this:

select ?husband ?color where {?husband ?wife . ?husband ?color . OPTIONAL {?wife ?wifecolor FILTER (?wifecolor = ?color)} FILTER (!BOUND(?wifecolor))}

It at least works for eric, but I haven't test carl.

PlexLuthor
This is the conventional solution.
tialaramex