1
votes

What SPARQL query can the number of identical triples?

SELECT ?s ?p ?o .

s p o
-----
a b c
x y z
a b c
r s t
x y z
a b c

Wanted output:

s p o num
----------
a b c 3
x y z 2
r s t 1

Hoped for something like

SELECT ?s ?p ?o (COUNT(*) AS ?num)
GROUP BY ?s ?p ?o

but can't get it to work...

1
The RDF stores don't really allow duplicated triplesWilliam Kinaan
The first query should never return duplicate triples. What triple store do you use? Do you have multiple graphs that contain the same triple? I cannot image that this output is really returned.UninformedUser
And for the second query, the whole query pattern is missing, i.e. the is no triple pattern or something similar, so what should match then and what can be counted if nothing is selected?UninformedUser
In an RDF graph, there is no such thing as duplicate triples.scotthenninger

1 Answers

2
votes

Some of the comments address why you won't actually have duplicate triples in a single graph, but if you could, you'd use a query similar to the one you proposed:

select ?s ?p ?o (count (*) as ?n) {
  ?s ?p ?o
}
group by ?s ?p ?o

A single graph is a set (i.e., no duplicates) of triples, so never contains "duplicate triples". In a dataset with multiple graphs, a pattern like this would essentially count how many graphs contain the triple:

graph ?g { ?s ?p ?o }