2

I'm trying to get information on Prefectures in Japan out of Wikidata. I succeeded to read the population statement, however there are occasionally several values with different point in time fields.

SELECT ?itemLabel ?population ?date 
WHERE {
   ?item wdt:P31 wd:Q50337;
         p:P1082 [pq:P585 ?date; ps:P1082 ?population];

   SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}

I'm was trying to get a single row per prefecture using groups, but to no avail. How do you select the lastest point in time?

4
SELECT ?itemLabel ?population ?date {
   ?item wdt:P31 wd:Q50337; p:P1082 [pq:P585 ?date; ps:P1082 ?population]
   FILTER NOT EXISTS {?item p:P1082 [pq:P585 ?date_] FILTER (?date_ > ?date)}
   SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}

Try it

If you really need GROUP BY:

SELECT ?itemLabel ?date ?population {
  {
  SELECT ?item ?date ?pop {
    ?item wdt:P31 wd:Q50337; p:P1082 [pq:P585 ?date; ps:P1082 ?pop]
  }
  } .
  {
  SELECT ?item (MAX(?date) AS ?date) {
    ?item wdt:P31 wd:Q50337; p:P1082 [pq:P585 ?date]
  } GROUP BY ?item
  }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}

Try it

With Blazegraph's named subqueries:

SELECT ?item ?date ?population
WITH {
  SELECT * { ?item wdt:P31 wd:Q50337; p:P1082 [pq:P585 ?date; ps:P1082 ?population] }} AS %all {
  {
  SELECT ?item (MAX(?date) AS ?date) { include %all } GROUP BY ?item
  }
  include %all
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}

Try it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.