Selecting some single and some fuzzy tags in SPARQL

I am trying to query DBPedia for a list of properties related to this class in the ontology, but since human readable โ€œlabelsโ€ are not always clear, I would also like to give an example from a database. The problem is that although I want to select all the individual properties, I only need one example of each property. Here is what my query looks like without capturing an 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 to 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 am very new to using SPARQL queries and databases in general, so I donโ€™t understand how to do this. Ideally, would I have something like DISTINCT (? Prop)? Title? Example, which selects each unique value for prop and returns its title and example.

+8
distinct rdf dbpedia sparql
source share
2 answers

In your second queries, the difference relates to a combination of ?prop ?title and ?example . Therefore, you do not get duplicates, for example, for the following two lines received in the second query:

 dbpedia2:subunitName "subunit name "@en "cent"@en dbpedia2:subunitName "subunit name "@en "centavo"@en 

they are not duplicated because the third line ?example has two different meanings "cent"@en and "centavo"@en

One possible way to solve this is to use GROUP BY and MIN to get only the lowest ranked value for ?label and ?example , ie:

 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 
+6
source share

Here is an alternative way to achieve what you want with subqueries:

 SELECT ?prop ?title ?example WHERE { ?thing a <http://dbpedia.org/ontology/Currency>. ?prop rdf:type rdf:Property. { SELECT ?title ?example WHERE { ?thing ?prop ?example . ?prop rdfs:label ?title. } LIMIT 1 } } LIMIT 100 

This has the advantage that the SPARQL 1.1 standard is compatible, as I indicated that aggregation is not allowed by the standard in my comment order, so you use a special provider extension that limits the portability of your query.

If you want to order an aggregated value in a way that is portable in SPARQL 1.1 implementations, you must first design it like this:

 SELECT ?s (COUNT(?p) AS ?predicates) WHERE { ?s ?p ?o } GROUP BY ?s ORDER BY DESC(?predicates) 
+4
source share

All Articles