SPARQL counts occurrences in multiple graphs

I am trying to write a SPARQL query that counts the occurrences of an object in multiple graphs. Sample data and expected result below:

Name graph g1:

@prefix g1: <http://example.com/g1#> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . g1:ex1 rdfs:label "a" . g1:ex2 rdfs:label "a" . g1:ex3 rdfs:label "b" . g1:ex3 rdfs:label "d" . 

Name graph g2:

 @prefix g2: <http://example.com/g2#> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . g2:ex1 rdfs:label "a" . g2:ex2 rdfs:label "b" . g2:ex3 rdfs:label "c" . 

Expected result of the SPARQL query:

 ?label ?g1count ?g2count a 2 1 b 1 1 c 0 1 d 1 0 

I can get the total for both graphs by combining rdfs: labels and counting occurrences:

 prefix g1: <http://example.com/g1#> prefix g2: <http://example.com/g2#> prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT ?label count(?label) as ?count { { GRAPH g1: { ?s rdfs:label ?label } } UNION { GRAPH g2: { ?s rdfs:label ?label } } } 

I thought that from here I could use subqueries in each UNION block to get separate calculations, but, apart from the possible inefficiency of such a query, I was not lucky to get the expected results.

+4
source share
2 answers

Golf here from RobV's answer (too big for comment):

 prefix g1: <http://example.com/g1#> prefix g2: <http://example.com/g2#> prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT ?label (count(?s1) as ?g1count) (count(?s2) AS ?g2count) { { GRAPH g1: { ?s1 rdfs:label ?label } } UNION { GRAPH g2: { ?s2 rdfs:label ?label } } } group by ?label order by ?label 

Result:

 --------------------- | label | g1c | g2c | ===================== | "a" | 2 | 1 | | "b" | 1 | 1 | | "c" | 0 | 1 | | "d" | 1 | 0 | --------------------- 
+4
source

You can take advantage of the fact that the COUNT function ignores unrelated values ​​and simply gives your variables different names i.e.

 prefix g1: <http://example.com/g1#> prefix g2: <http://example.com/g2#> prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT (COALESCE(?label, ?label2) AS ?label) (count(?label1) as ?g1count) (count(?label2) AS ?g2count) { { GRAPH g1: { ?s rdfs:label ?label1 } } UNION { GRAPH g2: { ?s rdfs:label ?label2 } } } 

The COALESCE function is used to combine the actual value into labels into a single variable, since COALESCE returns the first non-zero of the arguments

+2
source

All Articles