SPARQL count number of relations at once - sparql

Is it possible to get, in a single query, a count of the number of multiple relations? e.g.
SELECT (COUNT(?friendid) as ?friends) (COUNT(?cousinid) as ?cousins) (COUNT(?sonid) as ?sons)
WHERE
{
ex:person1 ex:friendOf ?friendid .
ex:person1 ex:cousinOf ?cousinid .
ex:person1 ex:fatherOf ?sonid .
}
If a complex query with multiple queries is needed, is this -in theory, of course- supposed to be faster than executing different SELECTs?

Following query retrieves ALL the predicates and their numbers:
SELECT ?p (COUNT(?p) as ?pCount) WHERE { ex:person1 ?p ?o} GROUP BY ?p
This one restricts the predicates (AKSW's suggestion):
SELECT ?p (COUNT(?p) as ?pCount) WHERE { ex:person1 ?p ?o. VALUES (?p) {(:p1)}} GROUP BY ?p
Here is an example:
SELECT ?p (COUNT(?p) as ?pCount) WHERE
{
<http://dbpedia.org/resource/Category:Museums_in_Italy> ?p ?o .
VALUES (?p) {(skos:altLabel) (owl:sameAs)}
}
GROUP BY ?p
And here are the results:
Results

Related

SPARQL Query to second and third level with concat

[I have a Tree Structure with three levels currently I am able to get the first level data from the the query written below I am looking for a query that can help me to concat data for ?o and their respective ?s ?p and ?o and even drill down to third level if needed
I have tried it with construct query but want it in a specific format do trying it out with select and concat
construct{ ?CR ?p ?cOBJECT .}
where {
select ?CR ?p (group_concat(?o;separator=',') as ?cOBJECT)
where{ VALUES ?p {abc:hasPart abc:hasLog abc:Description abc:CRStatus abc:hasSB abc:hasTB}
?CR ?p ?o .
}
GROUP BY ?CR ?p
}

SPARQL: How to get all triples with certain predicate where only one direction exists

I want to query all triples with a certain predicate p. The query should only return triples (s,p,o) where the other direction (o,p,s) does not exist.
How can I make such a query?
That's pretty easy:
SELECT ?s ?p ?o {
?s ?p ?o
MINUS { ?o ?p ?s }
}
FILTER NOT EXISTS instead of MINUS would also work. Replace ?p in the query with the desired predicate, or use something like FILTER (?p=ex:myPredicate) if you want ?p in the result.

sparql how to count variable pairs

I have the following query that gets instances of a class and their label/names. I want to count how many total results there are. However, I do not know how to formulate the count statement.
select ?s ?l {
?s a <http://dbpedia.org/ontology/Ship> .
{?s <http://www.w3.org/2000/01/rdf-schema#label> ?l}
union
{?s <http://xmlns.com/foaf/0.1/name> ?l}
}
I have tried
select ?s ?l (count (?s) as ?count) {
?s a <http://dbpedia.org/ontology/Ship> .
{?s <http://www.w3.org/2000/01/rdf-schema#label> ?l}
union
{?s <http://xmlns.com/foaf/0.1/name> ?l}
}
But that gives the counting for each ?s ?l pair, instead I need to know how many of the ?s ?l pairs there are. Or maybe I should not use count at all? As mentioned all I need to know is how many results in total a query returns (regardless of the hard limit that is put by the server, e.g., DBPedia returns a maximum of 50000 results for each query).
Any suggestions please?
Many thanks!
To count the number of matches, use
SELECT (COUNT(*) AS ?count)
WHERE {
?s <http://www.w3.org/2000/01/rdf-schema#label> | <http://xmlns.com/foaf/0.1/name> ?l .
}
Note I'm using the property path "or" (|) to get the union of the properties.

SPARQL Construct query Segmentation

I run a SPARQL construct query on DBpedia endpoint. The query is for getting all the information describing all movies:
construct
{
?s ?p ?o
}
where
{
?s a <http://dbpedia.org/ontology/Film>.
{
SELECT ?s ?p ?o
{
?s ?p ?o
}
group by ?s ?p
}
}
The query works fine. The problem is DBpedia stops it at 10.000. I tried to make the offset 0 but it does make any difference. Also, I don't think chrome would support to display a million triple. Therefore, I was thinking if there is a solution, or a tip, so I can retrieve the data by segments, meaning I start from where I stopped previously.
You could try ORDER BY, LIMIT and OFFSET. You can also simplify your query significantly:
construct
{
?s ?p ?o
}
where
{
?s a <http://dbpedia.org/ontology/Film> .
?s ?p ?o
} ORDER BY ?s ?p ?o
OFFSET 0 LIMIT 1000
Then change the OFFSET for each "segment".

Behaviour of variables projected out of the sub-select

Given a list of entities (with Persons among them) and their properties how should the following query behave:
select *
where
{
?s ?p ?o.
{
SELECT ?ps WHERE
{
?ps a <http://www.example.org/schema/Person> .
}
limit 1
}
#?ps ?p ?o.
filter (?s =?ps)
}
I tested this in 3 triple stores. Two of them filter on ps with the above query so the result is triples for one person(+ps column).
The 3'rd one returns all database triples because "The variable "ps" that is projected out of the sub-select does not join with anything in the top-level query."
Still since it's projected out and I use it in a FILTER I would expect to apply the filter.
Uncommenting line " #?ps ?p ?o. " will indeed display triples for one person.
The filter will be applied.
The FILTER applies to the whole block. There is a join of results of "?s ?p ?o" with results ?ps (so it's a join that is a cross product at this point - no common variable - but that's OK). That results in solutions with 4 bindings ?s ?p ?o ?ps The filter then applies.
You could write:
WHERE {
?s ?p ?o.
{
SELECT ?s
WHERE { ?s a <http://www.example.org/schema/Person> . }
limit 1
}
}