Combining arbitrary property path, distinct, and count [duplicate] - sparql

This question already has answers here:
SPARQL Query "COUNT" in Virtuoso Jena API - QueryParseException
(2 answers)
Closed 6 years ago.
I have the bellow SPARQL query and would like to get the sum of ?myInt for all the unique ?z values. Is it possible to express such a query in SPARQL 1.1?
SELECT ?z SUM(xsd:int(?myInt))
where{
?x property1+ ?y
?x property2 ?k
?k property3 ?z
?x property4 ?myInt
} group by distinct(?z)
I run this in Jena ARQ and get the following error:
Exception in thread "main" com.hp.hpl.jena.query.QueryParseException: Encountered " "sum" "SUM "" at line 1, column 11.
Here also an example data:
<http://a.com/6> <http://aq.com/p> <http://e.com/c5>.
<http://a.com/6> <http://aq.com/q> <http://a.com/5>.
<http://e.com/c5> <http://aq.com/a> <http://eoq.com/u1>.
<http://a.com/6> <http://aq.com/num> "10"^^<http://www.w3.org/2001/XMLSchema#integer> .
<http://a.com/5> <http://aq.com/p> <http://e.com/c4>.
<http://a.com/5> <http://aq.com/q> <http://a.com/4>.
<http://e.com/c4> <http://aq.com/a> <http://eoq.com/u1>.
<http://a.com/5> <http://aq.com/num> "10"^^<http://www.w3.org/2001/XMLSchema#integer>.

You can't select expressions directly, you have to select them as variables. I.e., you need to do:
SELECT ?z (SUM(xsd:int(?myInt)) as ?sum)
This is a common mistake because some endpoints (e.g., the public DBpedia endpoint, which is running Virtuoso) do allow your original form, even though it's not legal SPARQL.
As mentioned in a comment, you should group by ?zero, not by distinct(?z).

Related

SPARQL Query to Identify Predicates from One of Many Graphs [duplicate]

This question already has an answer here:
SPARQL - Restricting Result Resource to Certain Namespace(s)
(1 answer)
Closed 4 years ago.
I have created an ontology by grouping together many ontologies.
I want to use SPARQL to identify all middle terms (relationships) from one ontology from the group ontology.
The following approach only produces a pyparsing error.
g = rdflib.Graph()
result = g.parse("without-bfo.owl")
qres = g.query(
""" PREFIX sudo: <http://purl.url/sudo/ontology#>
SELECT ?v
WHERE {
?s sudo:?v ?o.
}""")
If I remove the sudo: prefix, this query returns all triples.
You can check if the relation starts with your namespace with CONTAINS
SELECT ?v
WHERE {
?s ?v ?o.
FILTER CONTAINS(?v, "http://purl.url/sudo/ontology#")
}
You can also try STRSTARTS
see w3 documentation
#Arcturus was close.
The following worked for me. One has to declare ?v as a string using STR. This SO post suggested the syntax.
qres = g.query(
""" SELECT DISTINCT ?v
WHERE {
?s ?v ?o .
FILTER CONTAINS(STR(?v), "sudo")
}""")
for row in qres:
print row

SPARQL BIND(COUNT(...) AS ...) not working [duplicate]

This question already has an answer here:
Sparql BIND result of count WITH A VARIABLE
(1 answer)
Closed 6 years ago.
I'm trying to assign a count to a variable for later use in the query (SPARQL).
I can't even get the following to work:
SELECT ?resultsCount
WHERE{
?subject ?predicate ?object.
BIND(COUNT(?object) AS ?resultsCount)
}
There is something wrong with my syntax or semantics here, as I simply get an empty result repeated ?resultsCount many times, instead of simply e.g. 86 (number of results).
However, I get the number of results (e.g. 86) when I do the following:
SELECT (COUNT(?object) AS ?resultsCount)
WHERE{
?subject ?predicate ?object
}
Is there any way to get BIND to work with COUNT like in the first example? If not, is there a different correct way to get the same sort of functionality?
I'm using Blazegraph 2.1.2. Could it be a bug with that?
count is an aggregate function. To use count, you need to use group by in your query, and use count in the projection. Count will count the number of results within each group. You can also use distinct with count, so you could do something like the following to get the number of distinct objects for each subject:
select ?subject (count(?object) as ?numObjects) {
?subject ?predicate ?object
}
group by ?subject
If you're just trying to count the number of matches, then you can use count without a group by (which gives you a single, implicit, group):
select (count(*) as ?numResults) {
?subject ?predicate ?object
}

SPARQL Query final label [duplicate]

This question already has an answer here:
Why does my SPARQL query return the URI of a resource instead of its name?
(1 answer)
Closed 6 years ago.
I have a SPARQL Query that returns the Europe capitals and their population. The query looks like this:
select ?s ?pop
where {
?s <http://dbpedia.org/ontology/populationTotal> ?pop .
?s a <http://dbpedia.org/ontology/place>
}
In this state, it returns the names of the cities in the following form: e.g. "<http://dbpedia.org/resource/London>" and what I want is to display only London in this case. So, is there a way I can tell SPARQL that I want only the final label?
I am querying against this endpoint: https://rdf.s4.ontotext.com/4730361296/demo01/repositories/test01
The advice here is similar to other questions - use SPARQL to inspect the data. So first try this query to see if there are any label properties defined:
select *
where {
?s <http://dbpedia.org/ontology/populationTotal> ?pop .
?s a <http://dbpedia.org/ontology/place> .
?s ?p ?o .
}
In this case you'll find that no label properties have been defined for place class definitions. If desired you can take the local name - the text after the last slash (or hash) as the name. Try this query:
select *
where {
?s <http://dbpedia.org/ontology/populationTotal> ?pop .
?s a <http://dbpedia.org/ontology/place> .
BIND(REPLACE(xsd:string(?s), ".*[/#]", "") AS ?label)
}

SPARQL - Max() isn't working

I'm trying to execute this SPARQL query in PROTEGE 2000, but the MAX function isn't working. it's like Max() isn't accepting the type of ?cpt.
SELECT ?searcher (COUNT(?publication) AS ?cpt)
WHERE {ont:GradeP ont:isFor ?searcher.
?publication ont:isPublishedBy ?searcher.}
GROUP BY ?searcher
HAVING (MAX(?cpt))
here's the exception :
SparqlReasonerException: org.openrdf.query.QueryEvaluationException: Unsupported value expr type: class org.openrdf.query.algebra.Max
The request is working without HAVING, so i guess there is no problem
with my ontology.
So can please anyone tell me where is the issue. Thanks!
I'm not sure why you are using group by here, but maybe you have a reason. One way to do it is by using order by. I have provided an example on dbpedia.
select distinct ?x count(?y) as ?count
where{
?x a dbpedia-owl:Person.
?x dbpprop:author ?y
}
order by desc(?count)
limit 1
The result is here.

Select All That Has The Same Prefix SPARQL [duplicate]

This question already has an answer here:
How to check prefix of variables in SPARQL?
(1 answer)
Closed 7 years ago.
I would like to return all triples where all subjects have the same prefix.
PREFIX dv: <http://example.org/example_vocabulary:>
SELECT DISTINCT *
FROM <http://example.org/dataset.example>
WHERE {
?s ?p ?o .
}
You should treat the URI as a string and basically filter your variable based on what you need. Since you are looking for a prefix, you can use strstarts. For example, something along these lines will work:
PREFIX dv: <http://example.org/example_vocabulary>
SELECT DISTINCT *
FROM <http://example.org/dataset.example>
WHERE {
?s ?p ?o .
filter strstarts(str(?s),str(dv:))
}
You should read up on string function.