Thomas Kimber
Thomas Kimber

Reputation: 11057

Select literal in SPARQL?

I want to construct a SPARQL query populated with values I'm setting as literals.

e.g.

SELECT 'A', 'B', attribute
FROM 
    TABLE

Would return a table that might look like this:

  A    |    B    |    attribute
-------|---------|--------------
  A    |    B    |    Mary
  A    |    B    |    has
  A    |    B    |    a
  A    |    B    |    little
  A    |    B    |    lamb

What I want to do is run a query like this to get all the object types in a triplestore:

select distinct ?o ("class" as ?item_type) 
where {
    ?s rdf:type ?o.
} 

and then (ideally) UNION it with a second query that pulls out all the distinct predicate values:

select distinct ?p ("predicate" as ?item_type) 
where {
    ?s ?p ?o.
} 

the results of which might look like:

  item           |    item_type    
-----------------|-----------------
 a_thing         |    class
another_thing    |    class
a_relation       |    predicate
another_relation |    predicate

But a UNION in SPARQL only links in an additional where clause, which means I can't specify the item_type literal I want to inject into my results-set.

Upvotes: 3

Views: 2103

Answers (1)

scotthenninger
scotthenninger

Reputation: 4001

I think the following should get you what you want:

SELECT DISTINCT ?item ?item_type
WHERE {
   { ?s a ?item .
     BIND("class" AS ?item_type)
   }
   UNION
   { ?s ?item ?o
     BIND("predicate" AS ?item_type)
   }

}

Upvotes: 5

Related Questions