Search code examples
sparqlrdfdbpediaturtle-rdf

SPARQL: Exclude double resources in COUNT


I am trying to write a SPARQL query that only counts the number of dutch politicians for each university. How ever, for each dbo:almaMater there is for some politicians one extra resource (their major, dbr:Sociology for example). This is also reflected in the COUNT. For example. I get count 49 for Leiden University where this should be only 42. Any idea how I can resolve this? I've tried FILTER NOT EXISTS and MINUS but both do nothing to the count. Thank you.

My query:

SELECT distinct  ?education (COUNT(?education) AS ?edu_count) 
WHERE { ?name <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://dbpedia.org/ontology/Person>.
        ?name <http://dbpedia.org/ontology/birthPlace> <http://dbpedia.org/resource/Netherlands>.
        ?name <http://dbpedia.org/ontology/party> ?party.
        ?name <http://dbpedia.org/ontology/almaMater> ?education.
        ?education <http://dbpedia.org/ontology/type> <http://dbpedia.org/resource/Public_university>.

        
} GROUP BY ?education 
ORDER BY DESC(?edu_count)

Solution

  • Revised query, based on comments, with extra whitespace for clarity. Query in form, and results --

    SELECT                               ?education
           ( COUNT ( DISTINCT ?name ) AS ?cnt )
    WHERE { ?name      <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://dbpedia.org/ontology/Person> .
            ?name      <http://dbpedia.org/ontology/birthPlace>          <http://dbpedia.org/resource/Netherlands> .
            ?name      <http://dbpedia.org/ontology/party>               ?party .
            ?name      <http://dbpedia.org/ontology/almaMater>           ?education .
            ?education <http://dbpedia.org/ontology/type>                <http://dbpedia.org/resource/Public_university> .
          } 
    GROUP BY ?education 
    ORDER BY DESC ( ?cnt ) 
             ASC ( ?education )