Wikidata SPARQL查询结果冗余求助(实际结果为预期平方值)
Hey there! Let's figure out why your query is returning 289 results instead of the expected 17. The core issue here is a classic cartesian product problem—your current query combines every property value from wdt:P2573 with every language label for the entity (wd:Q2) and every language label for the property itself. If each entity/property has 17 language labels, 17 × 17 = 289, which exactly matches the extra results you're seeing.
The Simple Fix: Restrict Labels to a Single Language
Most use cases don't need every language version of an entity or property label. Adding a language filter ensures each entity and property only returns one label, eliminating unwanted cross-combinations. Here's how to adjust your query:
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> SELECT * WHERE { wd:Q2 wdt:P2573 ?o. wd:Q2 rdfs:label ?entName. ?realAtt wikibase:directClaim wdt:P2573. ?realAtt rdfs:label ?attName. # Filter labels to your preferred language (swap "en" for "zh" or others as needed) FILTER(LANG(?entName) = "en") FILTER(LANG(?attName) = "en") }
Adaptation for Your Variable Property Scenario
Since your actual use case uses a variable ?p instead of hardcoded wdt:P2573, here's the flexible version that works for any property while avoiding redundancy:
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> SELECT * WHERE { wd:Q2 ?p ?o. ?realAtt wikibase:directClaim ?p. wd:Q2 rdfs:label ?entName. ?realAtt rdfs:label ?attName. # Lock labels to one language for consistent, non-redundant pairs FILTER(LANG(?entName) = "en") FILTER(LANG(?attName) = "en") # Optional: If you only want properties with unit values, add this constraint # ?o wikibase:quantityAmount ?amount; wikibase:quantityUnit ?unit. }
Why This Works
By filtering labels to a single language, you ensure each property value pairs with exactly one entity name and one property name—no more multiplying all available language labels together. This will shrink your result set down to the expected 17 entries, each showing the value alongside relevant, non-duplicate labels.
内容的提问来源于stack exchange,提问作者William

