Project:SPARQL/examples: Difference between revisions

Line 1,659: Line 1,659:
WHERE {   
WHERE {   
   ?item bnwdt:P12 bnwd:Q86 .
   ?item bnwdt:P12 bnwd:Q86 .
   ?item bnwdt:P2  ?location .  # item has property P2 location
    
  { 
    ?item bnwdt:P2  ?location .  # item has property P2 location
    
    
   optional { ?location bnwdt:P33 ?adminterr .  
   optional { ?location bnwdt:P33 ?adminterr .  
Line 1,675: Line 1,677:
     bind(iri(concat("http://www.wikidata.org/entity/", str(?ws))) as ?wikidata) .
     bind(iri(concat("http://www.wikidata.org/entity/", str(?ws))) as ?wikidata) .
         SERVICE <https://query.wikidata.org/sparql> {
         SERVICE <https://query.wikidata.org/sparql> {
             ?wikidata wdt:P625 ?wd_geo1 .  
             ?wikidata wdt:P625 ?wd_geo1 .
         } # /wikidata service     
         } # /wikidata service     
   }
   }
   
    }
 
  union
 
  {
      filter not exists {?item bnwdt:P2  ?location . } # item doesn't have property P2 location
  ?item bnwdt:P117 ?wikidata_id .  # but has wikidata id 
  bind(iri(concat("http://www.wikidata.org/entity/", str(?wikidata_id))) as ?wikidata) .
    
    
  SERVICE <https://query.wikidata.org/sparql> {
              ?wikidata wdt:P625 ?wd_geo1 .  # which has geo coords
            } # /wikidata service   
  }
   
   BIND(COALESCE(?wd_geo1, ?wd_geo2) AS ?wd_geo).  # if geo1 available use that, then try geo2
   BIND(COALESCE(?wd_geo1, ?wd_geo2) AS ?wd_geo).  # if geo1 available use that, then try geo2
    
    
579

edits