SPARQL BIND
Also BIND
`BIND (expr AS ?v)` in a SPARQL WHERE clause evaluates the expression expr for every result row and adds its value as a new column ?v.
Know these firstSPARQL Query Structure
Properties#
expris built from variables, constants and functions. The lecture usesSTR(x)(the IRI as a string),REPLACE(s, regex, replacement)(replace the matches of a regular expression in ), andLANG(x)from theFILTERexamples.?vcan then be selected, filtered and sorted like any other variable.- The
(… AS ?v)syntax is the same as for aggregates in theSELECTclause (SPARQL Aggregation).BINDcomputes a value per row, an aggregate one value per group. - In the lecture,
BINDreplaces the triple?venue rdfs:label ?nameof the sub-SELECT query: the “name” is derived from the IRI itself instead of being looked up in the knowledge graph.
Example#
REPLACE(STR(?venue), ".*/", ""): STR turns the IRI into a string, and the regular expression .*/ matches the longest prefix ending in /. Replacing it by "" leaves the part after the last /. For the toy IRI <http://example.org/streams/conf/xyz>:
Lecture query (Slide 23):
SELECT ?venue ?name ?num_papers WHERE {
{
SELECT ?venue (COUNT(?paper) AS ?num_papers) WHERE {
?paper dblp:publishedInStream ?venue .
} GROUP BY ?venue
}
BIND (REPLACE(STR(?venue), ".*/", "") AS ?name)
}
ORDER BY DESC(?num_papers)