WikiData Query For Cities / Towns in the US With Population and Inception Date

1.8k Views Asked by At

I'm looking for help getting the following table out of WikiData...

[City, County (if exists), State, Country, Population, Inception Date, Inception Type]

I have a query working that can get me a list of all Cities (Q515) or Towns (Q3957) within the United States. I was able to add a subquery to get the optional max Point in Time relating to Populations for each city so that I can hopefully get the latest population value. From there though I have no idea how to actually get the matching population for that date, let alone the state, county(ies) as a single field, and then finally the inception dates for each city.

#Cities in US with population and inception dates
SELECT DISTINCT ?city ?cityLabel ?countryLabel ?popDate ?population ?date WHERE {
  {?city (wdt:P31/wdt:P279*) wd:Q515 . }
  UNION
  {?city (wdt:P31/wdt:P279*) wd:Q3957 .}
  ?city (wdt:P17) wd:Q30 .
  ?city wdt:P17 ?country .
  ?city wdt:P571 ?date .

  { 
    SELECT (MAX(?popDate) AS ?popDate) ?city WHERE {
      {?city (wdt:P31/wdt:P279*) wd:Q515 . }
      UNION
      {?city (wdt:P31/wdt:P279*) wd:Q3957 .}
      ?city (wdt:P17) wd:Q30 .
      ?city wdt:P17 ?country .

      ?city p:P1082 ?pop_statement .
      OPTIONAL {
        ?pop_statement pq:P585 ?popDate .
      }
      ?pop_statement ?p ?population .
    }
    GROUP BY ?city
    ORDER BY DESC(?popDate)
  }

  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
ORDER BY ASC(?cityLabel)

Unfortunately this is my first try at any SPARQL code and I have no idea what to do next. Still needed are...

  • Any number of instances of "county of the United States (Q47168)" concatenated into a single column
  • One instance of "state of the United States (Q35657)" that should either exist as the "administrative territorial entity (P131)" of a county, or the city/town itself
  • The population related to the ?popDate value for the row
  • The label/alias used on wikipedia for the Inception property (P571)
0

There are 0 best solutions below