2

我正在尝试使用 SPARQL 从 Wikidata 中检索一些城市,但是尽管这些项目具有这些数据,但返回的几个项目的大部分字段都是空的。我不明白下面的查询有什么问题(指向 WQS 的链接)。例如,自治市Almelo的坐标 ( P625) 和父地点 ( P131) 在结果中错误地缺失:

SELECT ?mun ?munLabel ?coords ?parentPlace ?area WHERE {
  ?mun p:P31 ?instanceOf # Get statement because we need this later
       .
  ?instanceOf ps:P31/wdt:279* wd:Q2039348.
  
  OPTIONAL {
    ?mun wdt:P625 ?coords;
      wdt:P131 ?parentPlace; 
      wdt:P2046 ?area
      .
  }
  
  MINUS { ?instanceOf pq:P582 ?endTime. } # Don't show municipalities that have an end time
  
  service wikibase:label { bd:serviceParam wikibase:language "en". }
} ORDER BY ?munLabel
4

2 回答 2

3

您必须OPTIONAL独立声明每个语句:

  OPTIONAL { ?mun wdt:P625 ?coords . }
  OPTIONAL { ?mun wdt:P131 ?parentPlace . }
  OPTIONAL { ?mun wdt:P2046 ?area . }

否则,如果其中一个缺失,则忽略整个OPTIONAL块。

另请参阅多个可选图形模式

于 2022-02-11T11:10:10.020 回答
2

这是因为您正在使用一个OPTIONAL语句而不是分别使用 3 个语句。在这种情况下,Almelo 没有“区域” wdt:P2046,因此整个OPTIONAL语句的计算结果为假,因此它不绑定任何变量。

以下查询有效:请注意,我们有 3 个不同的可选语句,因此它们可能无法相互独立地绑定变量。

SELECT ?mun ?munLabel ?coords ?parentPlace ?area WHERE {
  ?mun p:P31 ?instanceOf # Get statement because we need this later
       .
  ?instanceOf ps:P31/wdt:279* wd:Q2039348.
  
  OPTIONAL {?mun wdt:P625 ?coords }
  OPTIONAL {?mun wdt:P131 ?parentPlace }
  OPTIONAL {?mun wdt:P2046 ?area }

  
  MINUS { ?instanceOf pq:P582 ?endTime. } # Don't show municipalities that have an end time
  
  service wikibase:label { bd:serviceParam wikibase:language "en". }
} ORDER BY ?munLabel
于 2022-02-11T11:14:35.187 回答