I'm trying to query DBPedia for a list of properties relating to a given class in the ontology, but since the human-readable "labels" aren't always clear, I'd also like to provide an example from the database. The problem is that while I want to select all distinct properties, I only want a single example of each property. Here's how my query looks without capturing the example:
SELECT DISTINCT ?prop ?title WHERE {
?thing ?prop [].
?thing a <http://dbpedia.org/ontology/Currency>.
?prop rdf:type rdf:Property.
?prop rdfs:label ?title.
} ORDER BY DESC(COUNT(DISTINCT ?thing))
LIMIT 100
If I change it in this way, I start getting duplicate values for ?prop:
SELECT DISTINCT ?prop ?title ?example WHERE {
?thing ?prop ?example.
?thing a <http://dbpedia.org/ontology/Currency>.
?prop rdf:type rdf:Property.
?prop rdfs:label ?title.
} ORDER BY DESC(COUNT(DISTINCT ?thing))
LIMIT 100
I'm very new to using SPARQL and database queries in general, so it's not at all clear to me how to do this. Ideally, I'd have something like DISTINCT(?prop) ?title ?example, which selects every unique value for prop, and returns its title and an example.
In your second queries the distinct applies to the combination of values of ?prop
?title
and ?example
. Therefore you're not getting any duplicates, for instance for the following two rows obtained in the second query:
dbpedia2:subunitName "subunit name "@en "cent"@en
dbpedia2:subunitName "subunit name "@en "centavo"@en
they aren't duplicates because the third row ?example
has two different values "cent"@en
and "centavo"@en
One posible way to solve this is to use GROUP BY
and MIN
to get just the lowest ranked value for ?label
and ?example
, i.e:
SELECT ?prop MIN(?title) MIN(?example) WHERE {
?thing ?prop ?example.
?thing a <http://dbpedia.org/ontology/Currency>.
?prop rdf:type rdf:Property.
?prop rdfs:label ?title.
} GROUP BY ?prop