'Wikidata SPARQL returns too many results

This is the query that can be executed in https://query.wikidata.org, I am supposed to get only 17 values, but it returns 289 results (17 * 17 = 289). I want to get property value with its unit. I am specifying wdt:P2573 just to demonstrate the issue, in real application that property is a variable ?p.

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.
  ?realAtt wikibase:propertyType ?wdDataType.

  OPTIONAL {
    ?realAtt wikibase:statementValue ?psv.
    ?realAtt wikibase:claim ?pp.
    wd:Q2 ?pp ?quantityNode.
    ?quantityNode ?psv ?wdv.
    ?wdv wikibase:quantityUnit ?unit.
    ?wdv wikibase:quantityAmount ?qAmount.
    BIND(?qAmount AS ?val)
  }
  BIND(COALESCE(?val, ?o) AS ?val)


  BIND(COALESCE(?unit, "") AS ?unit)
  FILTER(((LANG(?o)) = "en") || ((LANG(?o)) = "") || (!ISLITERAL(?o)))
  FILTER(((LANG(?attName)) = "en") || ((LANG(?attName)) = ""))
  FILTER(((LANG(?entName)) = "en") || ((LANG(?entName)) = ""))
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source