Project:SPARQL/examples: Difference between revisions

Line 1,940: Line 1,940:


<sparql tryit="1">
<sparql tryit="1">
## **NOTE** a few has_wikidata="yes" have a wikidata "Q" rather than "P" ID. Not sure if this is intentional. I can try to make the query exclude the Qs if preferred.
 
## **NOTES**  
## a few properties have a linked wikidata "Q" rather than "P" ID. These will unfortunately not link correctly to Wikidata so wd_label/wd_description won't be present.
## a few other properties may have wikidata=yes but no wd_label and/or wd_description. This could be because the property on Wikidata has been deleted (eg performed at).


PREFIX bnwd: <https://beyond-notability.wikibase.cloud/entity/>
PREFIX bnwd: <https://beyond-notability.wikibase.cloud/entity/>
Line 1,950: Line 1,953:
PREFIX bnpq: <https://beyond-notability.wikibase.cloud/prop/qualifier/>
PREFIX bnpq: <https://beyond-notability.wikibase.cloud/prop/qualifier/>


SELECT DISTINCT ?property ?label
SELECT DISTINCT ?property (if(bound(?wikidata_id), "yes", "no") as ?has_wikidata) ?bn_label ?wd_label ?bn_description
(if(bound(?wikidata_id), "yes", "no") as ?has_wikidata) ?wikidata_id ?propertyType
  ?wd_description ?wikidata ?bn_propertyType


WHERE {
WHERE {
   ?property a wikibase:Property ;
   ?property a wikibase:Property ;
               wikibase:propertyType ?propertyType . # datatype
               wikibase:propertyType ?bn_propertyType . # datatype
  service wikibase:label { 
            bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en-gb,en".
            ?property rdfs:label ?bn_label .
            ?property schema:description ?bn_description .
                            }
    # wikidata P117 
    optional { ?property bnp:P117 ?wikidata_id .
                ?wikidata_id bnpq:P14 ?wikidata_url .
            # the string to replace needs to be different for properties! 
              BIND(IRI(REPLACE(?wikidata_url, "https://www.wikidata.org/wiki/Property:", "http://www.wikidata.org/entity/")) as ?wikidata )


  # nb to get round slight inconsistencies in use of en and en-gb language labels
            SERVICE <https://query.wikidata.org/sparql> {
  # optional to fetch en-gb (and/or) en labels separately, then coalesce to pick just one. #win.
                    SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en-gb,en".  
  optional { ?property rdfs:label ?en . filter(lang(?en)="en"). }
                            ?wikidata schema:description ?wd_description .
  optional { ?property rdfs:label ?engb . filter(lang(?engb)="en-gb").  }  
                            ?wikidata rdfs:label ?wd_label .  
  BIND(COALESCE(?en, ?engb) AS ?label).
                    } # / label service
    
              } # /wikidata service    
  # wikidata P117
          } #/wikidata optional
  optional { ?property bnwdt:P117 ?wikidata_id . }
 
}
}
order by ?property
order by ?bn_label
</sparql>
</sparql>


579

edits