I'm using SPARQL and I wonder if I can put an sparql inside in clause? To be more specific, I need to get entities(s1,s2) who have specific condition for this sparql query[s1's aggregate value over a field is more than say 5]
select
?s1 ?x ?s2.
WHERE {
{?s1 rdf:type dbpedia-owl:Scientist.}
{?s2 rdf:type dbpedia-owl:Scientist.}
{?s2 dbpedia-owl:field ?x.}
{?s1 dbpedia-owl:field ?x.}
}
so I need to add an extra IN clause like this
SELECT
?s1 ?x ?s2.
WHERE {
{?s1 rdf:type dbpedia-owl:Scientist.}
{?s2 rdf:type dbpedia-owl:Scientist.}
{?s2 dbpedia-owl:field ?x.}
{?s1 dbpedia-owl:field ?x.}
{?s1 IN
{
SELECT ?s1 WHERE {
SELECT ?s1 (COUNT(?p) AS ?prizes) {
?s1 dbpprop:prizes ?p.
} group by (?s1)
}FILTER (?prizes > 2)
}
}
}
But I got error on the sparql query parser..... does anybody know how to fix it?
2.1 Writing a Simple Query The query consists of two parts: the SELECT clause identifies the variables to appear in the query results, and the WHERE clause provides the basic graph pattern to match against the data graph.
RDF is a directed, labeled graph data format for representing information in the Web. This specification defines the syntax and semantics of the SPARQL query language for RDF. SPARQL can be used to express queries across diverse data sources, whether the data is stored natively as RDF or viewed as RDF via middleware.
BIND. SPARQL's BIND function allows us to assign a value to a variable.
Both of these languages give the user access to create, combine, and consume structured data. SQL does this by accessing tables in relational databases, and SPARQL does this by accessing a web of Linked Data.
IN
has a somewhat different usage in SPARQL than SQL, it can only be used within a FILTER
like so:
FILTER(?s IN (<this>, <that>, <another>))
However just using the sub-query on it's own should give you the desired result because of the bottom up join semantics of SPARQL evaluation:
SELECT ?s1 ?x ?s2
WHERE
{
?s1 rdf:type dbpedia-owl:Scientist.
?s2 rdf:type dbpedia-owl:Scientist.
?s2 dbpedia-owl:field ?x.
?s1 dbpedia-owl:field ?x.
{
SELECT ?s1 WHERE
{
?s1 dbpprop:prizes ?p.
}
GROUP BY ?s1
HAVING (COUNT(?p) > 2)
}
}
You may notice I simplified some other parts of your query as well. There is no need to use two nested sub-queries because you can specify an aggregate condition using the HAVING
clause.
Also you do not need to put { }
around each individual triple pattern and in fact doing so may significantly harm performance.
As per official W3C documentation,
IN
boolean rdfTerm IN (expression, ...)
The IN operator tests whether the RDF term on the left-hand side is found in the values of list of expressions on the right-hand side. The test is done with "=" operator, which tests for the same value, as determined by the operator mapping.
A list of zero terms on the right-hand side is legal.
Errors in comparisons cause the IN expression to raise an error if the RDF term being tested is not found elsewhere in the list of terms.
The IN operator is equivalent to the SPARQL expression:
(lhs = expression1) || (lhs = expression2) || ...
Examples:
So, IN operator accepts list of vales, where as when we use a nested SPARQL query using select operator (As shown in your example), it returns Resultset, you can think of like list of statements. Hence you can not do it in that way.
However below is an example, you can try with as per SPARQL FILTER +IN syntax:
SELECT
?s1 ?x ?s2.
WHERE {
{?s1 rdf:type dbpedia-owl:Scientist.}
{?s2 rdf:type dbpedia-owl:Scientist.}
{?s2 dbpedia-owl:field ?x.}
{?s1 dbpedia-owl:field ?x.}
FILTER (?s1 IN (<http://example.com/#1>,<http://example.com/#2>, <http://example.com/#3>))
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With