sparqltriplestoretriplesn-triples

SPARQL: Return subjects w/ more than one specific type of object


Thanks in advance. I have an issue where merging some work produces two guids for a single subject in my triplestore. I need a way to isolate all of those instances so that I can normalize those guids. I'm new to SPARQL. I've tried returning these by COUNT and FILTER, but have so far failed. Here's what we're looking at.

<http://example.com/#bar> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> 
<http://example.com/#choo> .
<http://example.com/#bar> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> 
<http://example.com/#terms> .
<http://example.com/#bar> <http://www.w3.org/2008/05/skos-xl#prefLabel> 
<http://example.com/#bar/bar_en> .
<http://example.com/#bar> <http://www.foo.com/#guid> "17bda3bb-7db9-4afa- 
bb95-29da6464f137" .
<http://example.com/#bar> <http://www.foo.com/#guid> "1fa33bad-a98d-4a7e- 
8679-d8777e690c0c" .

Sorry, that wrapped text makes this harder to read. However, you can see here that I have a subject, http://example.com/#bar which has two guids. I have many cases like this in the same graph and I need a way to identify them all. Thanks again.


Solution

  • As alluded to in @AKSW's comment you should be able to use HAVING to filter on aggregations combined with grouping by the subject to achieve this:

    SELECT ?subject (COUNT(?guid) AS ?numGuids)
    WHERE
    {
      ?subject <http://www.foo.com/#guid> ?guid .
    }
    GROUP BY ?subject
    HAVING (?numGuids > 1)