You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何在Wikidata的SPARQL中指定单位类型?解决海拔查询错误

Fixing Your Wikidata SPARQL Query for the World's Tallest Mountain

Hey there, I get why your original query isn't working—Wikidata stores elevation values with mixed units (meters, feet, etc.), so comparing raw ?height values directly doesn't work because they're not just plain numbers, they're quantity objects tied to specific units. Let's tweak this to target meters explicitly and correctly find the tallest peak.

The Issue with Your Original Query

Your filter(?height = MAX(?height)) line fails because ?height is a quantity value with a unit, not a standalone number. When you try to use MAX() on it, SPARQL can't properly compare values that use different units. We need to extract the numeric portion of the elevation and enforce that we only consider entries measured in meters.

Corrected SPARQL Query

Here's the adjusted query that reliably fetches the world's tallest mountain by targeting meter-based elevation data:

SELECT ?mountLabel ?heightValue WHERE {
  ?mount wdt:P31 wd:Q8502.  # Instance of mountain
  # Extract elevation value and enforce meter unit
  ?mount wdt:P2044 ?height.
  ?height wikibase:quantityAmount ?heightValue.
  ?height wikibase:quantityUnit wd:Q11573.  # Unit = meters (Wikidata ID: Q11573)
  
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
} 
ORDER BY DESC(?heightValue) 
LIMIT 1

Key Changes Breakdown

  • We split the ?height quantity into its numeric component (?heightValue) and unit constraint (wd:Q11573 for meters) using Wikidata's built-in quantity properties.
  • Instead of trying to use MAX() in a filter (which doesn't play nice with quantity objects), we sort the numeric elevation values in descending order and grab the top result with LIMIT 1—this is the standard, reliable way to get the maximum value in Wikidata SPARQL.
  • By filtering for wikibase:quantityUnit wd:Q11573, we eliminate any elevation entries recorded in feet or other units, ensuring we're comparing apples to apples.

Why This Works

Wikidata uses quantity values for properties like elevation, which bundle a number and a unit together. By explicitly targeting meters, we avoid unit-based inconsistencies, and sorting the numeric values gives us the tallest mountain without any unexpected results.

内容的提问来源于stack exchange,提问作者Leo103

火山引擎 最新活动