KarelG
KarelG

Reputation: 5244

SPARQL query with turtle

i'm pretty new in SPARQL. Querying the linked data is an interesting concept. I have followed tutorials to comprehend it. Now, i'm using the data of European Comission as experiment model. (http://linkedpolitics.ops.few.vu.nl/home)

Now, i want to search amount of seats per country. This query here below gives what i want.

PREFIX lpv: <http://purl.org/linkedpolitics/vocabulary/> 
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

# count seats per country
SELECT ?country (COUNT(DISTINCT ?members) AS ?c)
WHERE {
   ?members lpv:countryOfRepresentation ?cOR .
   ?cOR rdfs:label ?country .
} GROUP BY ?cOR

Well, i thought that it should work, but what i get is the next

  ?country        ?c
1           "70"^^xsd:integer
2           "126"^^xsd:integer
3           "49"^^xsd:integer
4           "20"^^xsd:integer

The country column appears blank. Now when i adapt the query to

SELECT ?cOR (COUNT(DISTINCT ?members) AS ?c)

I get

         ?cOR              ?c
1   lp:EUCountry_AT     "70"^^xsd:integer
2   lp:EUCountry_BE     "126"^^xsd:integer
3   lp:EUCountry_BG     "49"^^xsd:integer
4   lp:EUCountry_CY     "20"^^xsd:integer

These results are URI (like the first one leads to http://linkedpolitics.ops.few.vu.nl/browse/list_resource?r=http%3A//purl.org/linkedpolitics/EUCountry_AT )

When you click on that, you can clearly see rdfs:label pointing to "Austria". But that doesn't appear at the first query result.

My question is, why does ?cOR rdfs:label ?country give a blank result ? Is that caused by that "sameAs" thing ? And how can i solve it ?

Upvotes: 1

Views: 1081

Answers (1)

Joshua Taylor
Joshua Taylor

Reputation: 85873

As written, your query isn't legal SPARQL. You can't put a variable in the SELECT part when you're grouping by if that variable isn't one of the variables that you're grouping on. You can check your query at sparql.org's query validator, which will tell you:

Non-group key variable in SELECT: ?country

You could include ?country in your group by:

SELECT ?country (COUNT(DISTINCT ?members) AS ?c)
WHERE { ... }
GROUP BY ?cOR ?country

The problem with the latter is that there may be different values for the label. E.g., you might "countryName"@en (a language tagged string) and "countryName" (a plain string).

    country         c
1   Austria         "70"^^xsd:integer
2   "Austria"@en    "70"^^xsd:integer
3   Belgium         "126"^^xsd:integer
4   "Belgium"@en    "126"^^xsd:integer
...

A perhaps better option would be to sample the value of ?country:

SELECT (sample(?country) as ?country_) (COUNT(DISTINCT ?members) AS ?c)
WHERE { ... }
GROUP BY ?cOR
    country_        c
1   "Austria"@en    "70"^^xsd:integer
2   "Belgium"@en    "126"^^xsd:integer
3   "Bulgaria"@en   "49"^^xsd:integer

Another alternative would be to filter the country labels to a given language. E.g.,

SELECT ?country (COUNT(DISTINCT ?members) AS ?c)
WHERE { ...
  filter langMatches(lang(?country),"en")
}
GROUP BY ?cOR ?country

This seems to work for your query, but if a country had more than one English label, you'd still have the "duplicated results" issue that happened in the first candidate solution.

Upvotes: 3

Related Questions