Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SPARQL generate Values for missing fields

Tags:

rdf

sparql

I´m trying to write a SELECT, that gives me all the Values in a Table. I have optional Values, I want those to be filled with a standard value, If they don´t exist.

This is my code:

SELECT * WHERE {
?a nmo:hasObject nm:coin 
OPTIONAL
{ ?a nmo:hasAuthority ?b }
OPTIONAL
{ ?a nmo:hasMaterial ?c }}

What I get id the following:

?a ?b ?c
1  yx 
2     ab
3  xz bc

What I want is to fill it up with the String "missing" if there is no value:

?a ?b        ?c
1  yx        "missing"
2  "missing"  ab
3  xz         bc

Any ideas on how to structure the SELECT to get this output?

like image 275
Xaju Avatar asked Sep 15 '26 08:09

Xaju


1 Answers

I'd probably use coalesce here:

SELECT
  ?a
  (coalesce (?b, ?missing) as ?bb)
  (coalesce (?c, ?missing) as ?cc)
WHERE {
  VALUES ?missing { "missing" }
  ?a nmo:hasObject nm:coin 
  OPTIONAL
  { ?a nmo:hasAuthority ?b }
  OPTIONAL
  { ?a nmo:hasMaterial ?c }
}
like image 164
Joshua Taylor Avatar answered Sep 17 '26 18:09

Joshua Taylor