title: Force Wikidata Query Service to remove duplicate items
body: <p>I am using Wikidata Query Service (which uses SPARQL) to retrieve some cities, but get duplicate items with different property values (even though these items pertain to the same city). It appears the error comes from some of the cities properties such as population or coordinates not having a &quot;High (or preferred) rank&quot;. In the example query below (<a href="https://query.wikidata.org/#SELECT%20DISTINCT%20%3Fitem%20%3FitemLabel%20%3FinstanceOfLabel%20%3Fpopulation%20%3Fcoords%20WHERE%20%7B%0A%20%20%3Fitem%20wdt%3AP17%20wd%3AQ928%3B%20%23%20country%20%28P17%29%20of%20Philippines%20%28Q928%29%0A%20%20%20%20%20%20%20%20wdt%3AP31%2Fwdt%3AP279%2A%20%3FinstanceOf.%0A%20%20%20%0A%20%20OPTIONAL%20%7B%20%23%20error%20appears%20to%20come%20from%20here%0A%20%20%20%20%3Fitem%20wdt%3AP1082%20%3Fpopulation%3B%0A%20%20%20%20%20%20%20%20%20%20wdt%3AP625%20%3Fcoords%3B%20%23%20ERROR%20also%20occurs%20here%3A%20example%20Janiuay%20has%203%20results%20because%20of%203%20different%20coords%2C%20all%20with%20a%20normal%20rank%0A%20%20%7D.%0A%20%20%0A%20%20VALUES%20%3FinstanceOf%20%7B%0A%20%20%20%20wd%3AQ104157%0A%20%20%7D%20%23%20municipality%20of%20the%20Philippines%20%28Q24764%29%3B%20city%20of%20the%20Philippines%20%28Q104157%29%0A%0A%20%20MINUS%20%7B%0A%20%20%20%20%3Fitem%20wdt%3AP576%20%3FdissolvedDate%3B%0A%20%20%20%20%20%20%20%20%20%20wdt%3AP7888%20%3FmergedInto.%0A%20%20%7D%0A%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22%5BAUTO_LANGUAGE%5D%2Cen%22.%20%7D%0A%7D%20ORDER%20BY%20%3FitemLabel%0A%0A" rel="nofollow noreferrer">link to WQS</a>), the city &quot;<a href="http://www.wikidata.org/entity/Q205956" rel="nofollow noreferrer">Candon</a>&quot; appears 15 times in the query results, probably because the city has its property population (<a href="https://www.wikidata.org/wiki/Property:P1082" rel="nofollow noreferrer"><code>P1082</code></a>), including the old population, all set with a &quot;Normal rank&quot;. The 2020 census population should be set as &quot;High rank&quot;.</p>
  <p>How can I force the query service to retrieve distinct cities (items) and get only their latest population, without having to edit the Wikidata item itself to set its property rank?</p>
  <pre><code>SELECT DISTINCT ?item ?itemLabel ?instanceOfLabel ?population ?coords WHERE {
    ?item wdt:P17 wd:Q928;
          wdt:P31/wdt:P279* ?instanceOf.
     
    OPTIONAL { # error appears to come from here due to Wikidata property rank issues
      ?item wdt:P1082 ?population;
            wdt:P625 ?coords;
    }.
    
    VALUES ?instanceOf {
      wd:Q104157
    }
  
    MINUS {
      ?item wdt:P576 ?dissolvedDate;
            wdt:P7888 ?mergedInto.
    }
  
    SERVICE wikibase:label { bd:serviceParam wikibase:language &quot;[AUTO_LANGUAGE],en&quot;. }
  } ORDER BY ?itemLabel
  </code></pre>