Search code examples
propertiesrdfsparqlmatchingdbpedia

Printing matching properties in DBpedia query


The following query will search for matching subjects based on a list of properties and within a given distance. They are ranked by the number of matching properties (?numProperties), which is output as a number. How can I also print each of the properties that are matches?

Run query

select ?subject (count ( distinct ?property) as ?numProperties) ?label ?lat ?long where {
values ?property { dbpedia-owl:crosses dbpedia-owl:vehicle dbpedia-owl:reopened dbpedia-owl:years dbpedia-owl:access dbpedia-owl:third dbpedia-owl:time dbpedia-owl:construction dbpedia-owl:anniversary dbpedia-owl:series dbpedia-owl:length dbpprop:suspension dbpprop:bridge dbpprop:crosses dbpprop:City dbpprop:connecting dbpprop:last dbpprop:three dbpprop:suspension dbpprop:bridges dbpprop:built } 
?subject ?property ?object .
?subject rdfs:label ?label .
?subject geo:lat ?lat .
?subject geo:long ?long .
FILTER (?long > -74.490898 && ?long <  -73.490898 && ?lat >  40.207222  && ?lat <  41.207222  ) .
FILTER(langMatches(lang(?label),"EN")) .
} 
group by ?subject ?label ?lat ?long
order by desc(?numProperties) 
limit 15

Solution

  • #-- I took the liberty of tidying up your query a bit.
    #-- The key is to use the GROUP_CONCAT aggregate function.
    select
      ?subject
      (count(distinct ?property) as ?numProperties)
      ?label
      ?lat
      ?long
      #-- concatenate distinct properties into a ', ' separated string
      (group_concat(distinct ?property;separator=', ') as ?properties)
    where {
      values ?property {
        dbpedia-owl:crosses dbpedia-owl:vehicle dbpedia-owl:reopened
        dbpedia-owl:years dbpedia-owl:access dbpedia-owl:third
        dbpedia-owl:time dbpedia-owl:construction dbpedia-owl:anniversary
        dbpedia-owl:series dbpedia-owl:length dbpprop:suspension
        dbpprop:bridge dbpprop:crosses dbpprop:City dbpprop:connecting
        dbpprop:last dbpprop:three dbpprop:suspension
        dbpprop:bridges dbpprop:built
      } 
      ?subject ?property ?object ;
               rdfs:label ?label ;
               geo:lat ?lat ;geo:long ?long .
      FILTER ( -74.490898 < ?long && ?long < -73.490898 &&
                40.207222 < ?lat  && ?lat  <  41.207222 )
      FILTER(langMatches(lang(?label),"EN"))
    } 
    group by ?subject ?label ?lat ?long
    order by desc(?numProperties) 
    limit 15
    

    SPARQL results