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

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
}

Related

Comparing two sets in SPARQL to find instances in Set 1 that do not exist in Set 2

I am trying to create two sets in SPARQL, and return the number of instances in ?set1 that do not exist in ?set2. I am trying to do this all in SPARQL, and I was not able to find a good resource on how to do this through FILTER, as well as using MINUS, but not having any luck.
SELECT COUNT(?set1)
WHERE {
?S ?P ?set1 .
?Q ?R ?set2 .
FILTER (member in ?set1 doesn't exist in ?set2
}
You could use FILTER NOT EXISTS:
SELECT (COUNT(?electronic) AS ?count)
WHERE {
?employee :usesElectronic ?electronic .
FILTER NOT EXISTS { ?company :ownsElectronic ?electronic . }
}
This counts the devices that
are used by any employee, and
are not owned by any company.
If you just need to know if such a case exists, and you are not interested in the count, you could use an ASK query instead, which returns TRUE or FALSE. For that, replace the whole SELECT line with ASK.

Why does this SPARQL query generate an error

My ontology IRI is "http://mycompany.com/ontologies/quality". I want to find "Find all subjects with a given object property (hasSolution)"
SELECT ?subject
WHERE { ?subject hasSolution <http://mycompany.com/ontologies/quality#Issue> } LIMIT 10
It caused this error message:
Caused by: org.openrdf.query.parser.sparql.ast.TokenMgrError: Lexical error at line 8, column 30. Encountered: " " (32), after : "hasSolution"
at org.openrdf.query.parser.sparql.ast.SyntaxTreeBuilderTokenManager.getNextToken(SyntaxTreeBuilderTokenManager.java:3499)
at org.openrdf.query.parser.sparql.ast.SyntaxTreeBuilder.jj_ntk(SyntaxTreeBuilder.java:8861)
I suspect the issue here is how you've expressed your predicate - it should be in IRI form, either using angle-brackets like this:
SELECT ?subject
WHERE {
?subject <http://mycompany.com/ontologies/hasSolution> <http://mycompany.com/ontologies/quality#Issue>
}
LIMIT 10
Or, using the PREFIX statement to give you something like this:
PREFIX mycoont: <http://mycompany.com/ontologies/>
SELECT ?subject
WHERE
{
?subject mycoont:hasSolution <http://mycompany.com/ontologies/quality#Issue>
}
LIMIT 10
Or, more generally, if you don't know what the predicate IRI is exactly, you can make it part of a query:
SELECT ?subject ?predicate
WHERE {
?subject ?predicate <http://mycompany.com/ontologies/quality#Issue>
}
LIMIT 10
And explore the results - the returned ?predicate values should be expressed as being full IRIs, or, depending on your query engine, as some form of prefix:suffix arrangement.
On exploring the results, you should just be able to copy-paste the appropriate value into the ?predicate triple of your WHERE clause, and remove the ?predicate triple from your SELECT clause.

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

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).

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.

"Sparql filter by number of objects return for a given predicate

In the following query I'm trying to get a list of all entries ?s that include more than 3 objects for the predicate sctap:mentionedBy. However, I keep getting a malformed query error for this search. Does anyone see anything wrong with my query?
Thanks
SELECT ?s
WHERE {
?s sctap:mentionedBy ?o
FILTER (count(?o) > 3)
}
The sparql error says: "Aggregate expression not legal". I'm not sure what that means.
Does anyone see anything wrong with my query?
Sure. Just like the error message says, you're using an aggregate expression (count(?o)) where one isn't legal. You can see in the table of contents of SPARQL 1.1 Query Language what things are filter functions that you can use in a filter, and what things are aggregates, and where you can use each. You can also try parsing queries at sparql.org's query validator. For your query, it will give you the line and column numbers where something went wrong. It's at count(?o).
In this case, you're trying to count the number of ?o values for each s, which means that you need to group by ?s, and that your filter will need to be father out. E.g.,
select ?s where {
?s sctap:mentionedBy ?o
}
group by ?s
having (count(?o) > 3)
It's unlikely to make a difference in this case, but you probably only want to count distinct values of ?o, so you could also consider:
select ?s where {
?s sctap:mentionedBy ?o
}
group by ?s
having (count(distinct ?o) > 3)