SPARQL - Max() isn't working - sparql

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.

Related

Sparql filter on prefix does not work on dbpedia

I'm new to SPARQL hope someone could help me.
The problem is that if I run the following query on dbpedia sparql:
SELECT DISTINCT ?class WHERE {
?s a ?class.
}
it returns:
result query
I would like to remove the results that has this two prefix: "http://www.w3.org/2002/07/owl" and "http://www.w3.org/2000/01/rdf-schema", now the query is:
SELECT DISTINCT ?class WHERE {
?s a ?class.
FILTER ( !strstarts(str(?class), "http://www.w3.org/2002/07/owl") ).
FILTER ( !strstarts(str(?class), "http://www.w3.org/2000/01/rdf-schema") ).}
but it returns only one result:
http://www.w3.org/1999/02/22-rdf-syntax-ns#Property
If I execute the same query on istat sparql these queries work fine.
So the question is, why on dbpedia my queries does not work as expected?
Thanks

SPARQL query failing to match literal on Pubchem RDF data

I have loaded part of the PubChem RDF data in Virtuoso and am trying to use SPARQL to query it, through iSQL.
While the following query works:
SELECT ?syno ?type ?value
WHERE {
?syno sio:is-attribute-of <http://rdf.ncbi.nlm.nih.gov/pubchem/compound/CID1829049> .
?syno rdf:type ?type .
?syno sio:has-value ?value .
} LIMIT 10;
I am not able to get any results for a query like (the value being taken from one of the above results):
SELECT ?syno
WHERE {?syno sio:has-value "AC1LXI26"};
In the previous case, I am simply trying to match a litteral.
Do I need to build an extra index? Is exact text match not supported in Virtuoso?
I solved my problem by simply adding #en at the end of the query string!
SELECT ?syno
WHERE {?syno sio:has-value "AC1LXI26"#en};

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

SPARQL Query problem -> wrong answer

I want to select a triple using SPARQL. To do it, i'm using following query:
SELECT count (*)
WHERE {?s ?p ?o}
FILTER (?s=http://kjkhlsa.net && ?p=http://lkasdjlkjas.com && ?o=Test)
As answer i get fully wrong triple :( subject ist not equal to "http://kjkhlsa.net", predicate is not equal to "http://lkasdjlkjas.com" and object ist also not equal to "Test". Can someone explain me, what I'm doing wrong :(
edit1:
I have put the query into php file:
$inst_query = 'SELECT * { <http://kjkhlsa.net> <http://lkasdjlkjas.com> "Test"}';
echo $inst_query;
The answer from the echo was "SELECT * { "Test"}". Then i tried it with WHERE:
$inst_query = 'SELECT * WHERE { <http://kjkhlsa.net> <http://lkasdjlkjas.com> "Test"}';
echo $inst_query;
Here was the answer "SELECT * WHERE { "Test"}"...so, i'm missing the URIs, but this seems for me as php issue and not sparql problem.
edit2:
I've put the query into SPARQL Query editor and i get the response "no result"....but I'm sure, that i have this triple.
In its current form the question is not very clear (see my comment above).
Since you are essentially trying to get triples matching a pattern, it is more efficient to use a graph pattern instead of FILTER. Many SPARQL implementations first match candidate triples by graph patterns and only then apply the FILTER expression. In essence, with a ?s ?p ?o graph pattern, you're doing a linear scan over all your triples.
So, here's something that should work, using graph patterns instead of FILTER.
SELECT * { <http://kjkhlsa.net> <http://lkasdjlkjas.com> "Test" }
Notes: I didn't include COUNT(*) which is not standard SPARQL. <> around URIs. "" around literal.
Try to use this :
SELECT count (*) as ?count
WHERE {
?s ?p ?o
FILTER (?s=<http://kjkhlsa.net> && ?p=<http://lkasdjlkjas.com> && ?o=Test)
}
The following query uses the count function to count the number of distinct URI(s) returned to the ?s variable.
SELECT ?s (COUNT (DISTINCT ?s) as ?count)
WHERE {?s ?p ?o}
FILTER (?o="Test")