1

I'm trying to fetch French communes from Wikidata using SPARQL. I want to also retrieve the departement code and the region code of the commune.

For now I have this query :

SELECT ?inseeCode ?commune ?nom ?dateFinNom ?dateFinCodeInsee ?adminEntityInseeCode
WHERE {
    ?commune p:P374 ?inseeStmt . 
    ?inseeStmt ps:P374 ?inseeCode . 
    OPTIONAL {?inseeStmt pq:P582 ?dateFinCodeInsee .}

    ?commune wdt:P31 wd:Q484170 .

    ?commune p:P1448 ?nomStmt .
    OPTIONAL {?nomStmt ps:P1448 ?nom .}
    OPTIONAL {?nomStmt pq:P582 ?dateFinNom .}

    ?commune p:P131 ?adminEntitiesStmt .
    ?adminEntitiesStmt ps:P2586 ?adminEntityInseeCode .

}
LIMIT 100

The problem is the line ?adminEntitiesStmt ps:P2586 ?adminEntityInseeCode . which make return 0 results whereas I expect for each commune to retrieve all administrative entities codes (region, departement, etc.).

What did I do wrong?

logi-kal
  • 7,107
  • 6
  • 31
  • 43
fabien-michel
  • 1,873
  • 19
  • 38

1 Answers1

2

You have to use the wdt: prefix (or concatenate p: and ps:) for directly accessing the object of a statement.

SELECT ?inseeCode ?commune ?nom ?dateFinNom ?dateFinCodeInsee ?adminEntityInseeCode
WHERE {
    ?commune p:P374 ?inseeStmt . 
    ?inseeStmt ps:P374 ?inseeCode . 
    OPTIONAL {?inseeStmt pq:P582 ?dateFinCodeInsee .}

    ?commune wdt:P31 wd:Q484170 .

    ?commune p:P1448 ?nomStmt .
    OPTIONAL {?nomStmt ps:P1448 ?nom .}
    OPTIONAL {?nomStmt pq:P582 ?dateFinNom .}

    ?commune wdt:P131 ?adminEntitiesStmt .
    ?adminEntitiesStmt wdt:P2586 ?adminEntityInseeCode .
}
LIMIT 100

wdt:P2586 is similar to p:P2586/ps:P2586, but not exactly equivalent. For example, the first one automatically selects the best statements, while the latter considers all the statements (also the ones marked as deprecated).

logi-kal
  • 7,107
  • 6
  • 31
  • 43