Wikidata: Filter post codes only current valid

Viewed 67

I would like to filter post codes to show only the current active for today.

Problem is there are are cities with old post codes (Example).

My current query shows the old post codes:

SELECT ?city ?cityLabel ?postcode ?federal_stateLabel ?federal_state_nr WHERE {
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],de". }
  ?city (wdt:P31/(wdt:P279*)) wd:Q7930989;
    wdt:P17 wd:Q183;
    wdt:P281 ?postcode;
    wdt:P131 ?federal_state.
  ?federal_state wdt:P439 ?federal_state_nr.
}
ORDER BY (?postcode)
LIMIT 10

(query.wikidata.org)

I would have to use start time P580 and end time P582 but I don't see how.

1 Answers

You can use this for filtering out the claims which have an end time:

?city p:P281 ?postCodeStmt .
?postCodeStmt ps:P281 ?postcode .
FILTER NOT EXISTS { ?postcode pq:P582 ?endTime . }

The whole query becomes:

SELECT ?city ?cityLabel ?postcode ?federal_stateLabel ?federal_state_nr WHERE {
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],de". }
  ?city (wdt:P31/(wdt:P279*)) wd:Q7930989;
    wdt:P17 wd:Q183;
    p:P281 ?postCodeStmt;
    wdt:P131 ?federal_state.
  ?federal_state wdt:P439 ?federal_state_nr.
  ?postCodeStmt ps:P281 ?postcode.
  FILTER NOT EXISTS { ?postCodeStmt pq:P582 ?endTime . } # Filtering out old postal codes
}
ORDER BY (?postcode)
LIMIT 100

See also Wikidata:SPARQL tutorial§Qualifiers.

Related