Wikidata:Gruppo Wikidata per Musei, Archivi e Biblioteche/Query

Cfr. anche User:Epìdosis/Queries/Exp e User:Epìdosis/sandbox

Esempi di query complesse edit

#title:Query con tutti i valori (ps:) e non solo il best rank (wdt:)
SELECT ?item ?itemLabel ?datanascita ?rank
WHERE {
  ?item wdt:P106 wd:Q13418253.
  ?item p:P569 ?dichiarazione .
  ?dichiarazione wikibase:rank ?rank .
  ?dichiarazione ps:P569 ?datanascita .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
Query con tutti i valori (ps:) e non solo il best rank (wdt:)
#title:Query con qualificatore (pq:) e uso del rank deprecato
SELECT ?item ?itemLabel ?datanascita ?motivo ?motivoLabel
WHERE {
  ?item wdt:P106 wd:Q13418253.
  ?item p:P569 ?dichiarazione .
  ?dichiarazione wikibase:rank wikibase:DeprecatedRank .
  ?dichiarazione ps:P569 ?datanascita .
  OPTIONAL { ?dichiarazione pq:P2241 ?motivo } .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
Query con qualificatore (pq:) e uso del rank deprecato
#title:Query con riferimento (prov:wasDerivedFrom) e parte di riferimento (pr:)
SELECT ?item ?itemLabel ?datanascita ?sbn
WHERE {
  ?item wdt:P106 wd:Q13418253.
  ?item p:P569 ?dichiarazione .
  ?dichiarazione ps:P569 ?datanascita .
  ?dichiarazione prov:wasDerivedFrom ?riferimento .
  ?riferimento pr:P396 ?sbn .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
Query con riferimento (prov:wasDerivedFrom) e parte di riferimento (pr:)
#title:Query con filtro (FILTER) basato sul REGEX applicato a un URL
SELECT ?item ?itemLabel ?datanascita ?url
WHERE {
  ?item wdt:P106 wd:Q13418253.
  ?item p:P569 ?d . 
  ?d a wikibase:BestRank . #per ottenere il BestRank si usa "a" come predicato anziché "wikibase:rank"
  ?d ps:P569 ?datanascita .
  ?d prov:wasDerivedFrom ?riferimento .
  ?riferimento pr:P854 ?url .
  FILTER(REGEX(STR(?url), "treccani"))
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
Query con filtro (FILTER) basato sul REGEX applicato a un URL
#title:Query con filtro (FILTER) basato sul REGEX applicato a una descrizione italiana
SELECT ?item ?itemLabel ?desc
WHERE {
  ?item wdt:P106 wd:Q13418253.
  ?item schema:description ?desc . 
  FILTER(LANG(?desc) = 'it')
  FILTER(REGEX(?desc, "^[A-Z]"))
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
Query con filtro (FILTER) basato sul REGEX applicato a una descrizione italiana
#title:Query con filtro (FILTER) basato sul REGEX applicato a un alias italiano
SELECT ?item ?itemLabel ?alias
WHERE {
  ?item wdt:P106 wd:Q13418253.
  ?item skos:altLabel ?alias . 
  FILTER(LANG(?alias) = 'it')
  FILTER(REGEX(?alias, ","))
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
Query con filtro (FILTER) basato sul REGEX applicato a un alias italiano
#title:Query con filtro (FILTER) basato sul REGEX applicato a un'etichetta italiana
SELECT ?item ?itemLabel ?label
WHERE {
  ?item wdt:P106 wd:Q13418253.
  ?item rdfs:label ?label .
  FILTER(LANG(?label) = 'it')
  FILTER(REGEX(?label, "Carlo"))
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
Query con filtro (FILTER) basato sul REGEX applicato a un'etichetta italiana
#title:Query per elementi senza etichetta in italiano
SELECT ?item ?itemLabel ?label
WHERE {
  ?item wdt:P106 wd:Q13418253.
  MINUS { ?item rdfs:label ?label . FILTER(LANG(?label) = 'it') }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
Query per elementi senza etichetta in italiano
#title:Query con sitelink vari (schema:about)
SELECT ?item ?itemLabel ?articolo
WHERE {
  ?item wdt:P106 wd:Q13418253.
  ?articolo schema:about ?item .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
Query con sitelink vari (schema:about)
#title:Query con sitelink a Wikipedia in italiano
SELECT ?item ?itemLabel ?articolo
WHERE {
  ?item wdt:P106 wd:Q13418253.
  ?articolo schema:about ?item .
  ?articolo schema:isPartOf <https://it.wikipedia.org/> .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
Query con sitelink a Wikipedia in italiano
#title:Query con sitelink a Wikipedia in italiano e a Wikipedia in inglese
SELECT ?item ?itemLabel ?articolo ?articolo2
WHERE {
  ?item wdt:P106 wd:Q13418253.
  ?articolo schema:about ?item .
  ?articolo2 schema:about ?item .
  ?articolo schema:isPartOf <https://it.wikipedia.org/> .
  ?articolo2 schema:isPartOf <https://en.wikipedia.org/> .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
Query con sitelink a Wikipedia in italiano e a Wikipedia in inglese
#title:Query con conteggio (COUNT)
SELECT ?item ?itemLabel (COUNT(?figlio) AS ?numero)
WHERE {
  ?item wdt:P106 wd:Q13418253.
  ?item wdt:P40 ?figlio .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
GROUP BY ?item ?itemLabel
Query con conteggio (COUNT)

edit

Disclaimer: posto che un catalogo sia connesso a Wikidata in misura inferiore al 100% dei suoi identificativi, i risultati delle query sono inevitabilmente parziali.

#title:Id presenti in elementi di persone
SELECT ?person ?id
WHERE {
  ?person wdt:P8750 ?id .
  ?person wdt:P31 wd:Q5 .
}
Id presenti in elementi di persone

#title:Persone con almeno un id
SELECT DISTINCT ?person
WHERE {
  ?person wdt:P8750 ?id .
  ?person wdt:P31 wd:Q5 .
}
Persone con almeno un id

#title:Id presenti in elementi di case editrici
SELECT ?publisher ?id
WHERE {
  ?publisher wdt:P8750 ?id .
  ?publisher wdt:P31/wdt:P279* wd:Q2085381 .
}
Id presenti in elementi di case editrici

#title:Case editrici con almeno un id
SELECT DISTINCT ?publisher
WHERE {
  ?publisher wdt:P8750 ?id .
  ?publisher wdt:P31/wdt:P279* wd:Q2085381 .
}
Case editrici con almeno un id

#title:Id presenti in elementi di opere
SELECT ?work ?id
WHERE {
  ?work wdt:P8750 ?id .
  ?work wdt:P31/wdt:P279* wd:Q47461344 .
}
Id presenti in elementi di opere

#title:Opere con almeno un id
SELECT DISTINCT ?work 
WHERE {
  ?work wdt:P8750 ?id .
  ?work wdt:P31/wdt:P279* wd:Q47461344 .
}
Opere con almeno un id

#title:Persone con almeno un id ed etichetta in italiano
SELECT ?person ?label ?id
WHERE {
  ?person wdt:P8750 ?id .
  ?person wdt:P31 wd:Q5 .
  ?person rdfs:label ?label . FILTER(LANG(?label) = "it")
}
Persone con almeno un id ed etichetta in italiano

#title:Persone con almeno un id e senza etichetta in italiano
SELECT ?person ?personLabel ?id
WHERE {
  ?person wdt:P8750 ?id .
  ?person wdt:P31 wd:Q5 .
  MINUS { ?person rdfs:label ?label . FILTER(LANG(?label) = "it") }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en,de,fr,es". }
}
Persone con almeno un id e senza etichetta in italiano

#title:Persone con almeno un id e senza descrizione in italiano
SELECT ?person ?personDescription
WHERE {
  ?person wdt:P8750 ?id .
  ?person wdt:P31 wd:Q5 .
  MINUS { ?person schema:description ?itdesc . FILTER(LANG(?description) = "it") }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en,de,fr,es". }
}
Persone con almeno un id e senza descrizione in italiano

#title:Persone con almeno un id e senza descrizione in italiano e in inglese
SELECT ?person ?personDescription
WHERE {
  ?person wdt:P8750 ?id .
  ?person wdt:P31 wd:Q5 .
  MINUS { ?person schema:description ?itdesc . FILTER(LANG(?description) = "it") }
  MINUS { ?person schema:description ?endesc . FILTER(LANG(?description) = "en") }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "de,fr,es". }
}
Persone con almeno un id e senza descrizione in italiano e in inglese

#title:Persone con almeno un id ed etichetta o alias italiani contenenti una virgola e non contenenti "di" o "de"
SELECT DISTINCT ?person ?label_alias
WHERE {
  ?person wdt:P8750 ?id .
  ?person wdt:P31 wd:Q5 .
  ?person ?s ?label_alias . FILTER(LANG(?label_alias) = "it")
  VALUES ?s { rdfs:label skos:altLabel } .
  FILTER(REGEX(?label_alias, ","))
  FILTER(!REGEX(?label_alias, " d(i|e)"))
}
Persone con almeno un id ed etichetta o alias italiani contenenti una virgola e non contenenti "di" o "de"

#title:Persone con almeno un id e descrizione che comincia con parentesi, numero o maiuscola, esclusi i papi
SELECT DISTINCT ?person ?label ?description
WHERE {
  ?person wdt:P8750 ?id .
  ?person wdt:P31 wd:Q5 .
  ?person rdfs:label ?label . FILTER(LANG(?label) = "it")
  ?person schema:description ?description . FILTER(LANG(?description) = "it")
  FILTER(REGEX(?description, "^[(0-9A-Z]"))
  FILTER(!REGEX(?description, "vescovo di Roma"))
}
ORDER BY ?description
Persone con almeno un id e descrizione che comincia con parentesi, numero o maiuscola, esclusi i papi

#title:Persone con almeno un id e almeno un alias in italiano
SELECT ?person ?alias ?id
WHERE {
  ?person wdt:P8750 ?id .
  ?person wdt:P31 wd:Q5 .
  ?person skos:altLabel ?alias . FILTER(LANG(?alias) = "it")
}
Persone con almeno un id e almeno un alias in italiano

#title:Persone con almeno un id e nessun alias in italiano
SELECT ?person ?personLabel ?id
WHERE {
  ?person wdt:P8750 ?id .
  ?person wdt:P31 wd:Q5 .
  MINUS { ?person skos:altLabel ?alias . FILTER(LANG(?alias) = "it") }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "it,en,de,fr,es". }
}
Persone con almeno un id e nessun alias in italiano

#title:Persone con almeno un id nate a Roma
SELECT ?person ?personLabel
WHERE {
  ?person wdt:P8750 ?id .
  ?person wdt:P19/wdt:P131* wd:Q220 .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "it,en,de,fr,es". }
}
Persone con almeno un id nate a Roma

#title:Mappa dei luoghi di nascita delle persone con almeno un id
#defaultView:Map{ "markercluster": true}
SELECT ?person ?personLabel ?birthplace ?birthplaceLabel ?coord
WHERE {
  ?person wdt:P8750 ?id .
  ?person wdt:P19 ?birthplace .
  ?birthplace wdt:P625 ?coord .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "it,en". }
}
Mappa dei luoghi di nascita delle persone con almeno un id

#title:Persone con almeno un id ed eventuali date di nascita e/o morte
SELECT DISTINCT ?person ?personLabel ?birthdate ?deathdate
WHERE {
  ?person wdt:P8750 ?id .
  ?person wdt:P31 wd:Q5 .
  OPTIONAL { ?person wdt:P569 ?birthdate } .
  OPTIONAL { ?person wdt:P570 ?deathdate } .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "it,en,de,fr,es". }
}
ORDER BY ?birthdate ?deathdate
Persone con almeno un id ed eventuali date di nascita e/o morte

#title:Persone con almeno un id e data di nascita e/o morte senza riferimenti
SELECT DISTINCT ?person ?personLabel
WHERE {
  ?person wdt:P8750 ?id .
  ?person ?date ?st .
  VALUES ?date { p:P569 p:P570 } .
  MINUS { ?st prov:wasDerivedFrom ?ref } .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "it,en,de,fr,es". }
}
Persone con almeno un id e data di nascita e/o morte senza riferimenti

Query SPARQL su data.bnf.fr/sparql edit

N.B. Queste query devono essere copiate e incollate su https://data.bnf.fr/sparql Il link "Provalo!" NON funziona

# Estrarre da data.bnf.fr gli autori persona # N.B. L'endpoint restituisce al massimo 1 milione di risultati
SELECT ?autore 
WHERE {
  ?autore <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Person> . # pone la condizione di essere persona in data.bnf.fr
  ?opera <http://purl.org/dc/terms/creator> ?autore . # pone la condizione di essere autore di un'opera
}
Try it!
# Estrarre data.bnf.fr gli autori persona nati a Roma e ordinarli alfabeticamente per cognome # N.B. L'endpoint restituisce al massimo 1 milione di risultati
SELECT ?autore 
WHERE {
  ?autore <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Person> . # pone la condizione di essere persona in data.bnf.fr
  ?opera <http://purl.org/dc/terms/creator> ?autore . # pone la condizione di essere autore di un'opera
  ?autore foaf:name ?nome . # chiama il nome e cognome dell'autorela come variabile ?nome
  ?autore <http://rdvocab.info/ElementsGr2/placeOfBirth> "Rome" . # pone la condizione di essere nati in un luogo etichettato "Rome"
  ?autore foaf:familyName ?cognome . # chiama il solo cognome con la variabile ?cognome, da usare per l'ordinamento
}
ORDER BY ?cognome # ordina gli autori per cognome A-Z
}
Try it!
# Estrarre da data.bnf.fr le opere di Victor Hugo, identificato con il numero VIAF, che hanno un eventuale match con Wikidata
PREFIX bio: <http://vocab.org/bio/0.1/>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX bio: <http://vocab.org/bio/0.1/>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX bnf-onto: <http://data.bnf.fr/ontology/bnf-onto/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT DISTINCT ?nomeautore ?DescrOp ?titop ?datacreaz ?match 
WHERE {
 
  ?DescrOp dcterms:creator ?persona .
  ?URIOp foaf:focus ?DescrOp .
  ?URIOp <http://www.w3.org/2004/02/skos/core#prefLabel> ?titop .
  ?DescrOp       <http://rdvocab.info/Elements/dateOfWork> ?datacreaz .
  ?DescrOp <http://data.bnf.fr/ontology/bnf-onto/subject> "Litt\u00E9ratures" .
  OPTIONAL { ?URIOp <http://www.w3.org/2004/02/skos/core#exactMatch> ?match . 
  FILTER regex(str(?match), "wikidata") . }
  ?persona owl:sameAs <http://viaf.org/viaf/9847974> .
  ?persona  bnf-onto:firstYear ?natoBNF .
  ?persona  foaf:name ?nomeautore .
  ?persona foaf:familyName ?cognome .      
}
ORDER BY ?datacreaz ?titop
LIMIT 1000
Try it!

Query federate edit

dati.beniculturali.it edit

# Associo agli Elementi Wikidata (Musei) la descrizione estesa presente nel db unico Mibact (beniculturali.it)
# Tramite id Wikidata in ICCD
PREFIX lO: <https://w3id.org/italia/onto/l0/>  # Creo il prefisso per referenziare l'IRI della proprietà 'Descrizione' del sito dati.beniculturali.it

SELECT ?item ?itemLabel ?desc ?itemID WHERE {
    ?item wdt:P31 wd:Q33506 .  # Recupera tutte le istanze di Musei
    ?item wdt:P131 wd:Q13369 . # Che appartengono all'area amministrativa di Rimini
  
    SERVICE <http://dati.beniculturali.it/sparql> {
      ?itemID owl:sameAs ?item .      # Sul sito beniculturali recupera gli ID degli Elementi che hanno lo stesso Item ID del where Wikidata
      ?itemID lO:description ?desc .  # Sul sito beniculturali recupera la Descrizinoe degli Elementi con ID trovato
    }

    SERVICE wikibase:label { bd:serviceParam wikibase:language "it". }
}
Try it!
# Associo agli Elementi Wikidata (Musei) la corrispondente descrizione estesa presente nel db unico Mibact (beniculturali.it)
# Tramite id ICCD in Wikidata
# Query ottimizzata e più funzionale rispetto alla query federata inversa 'id Wikidata in ICCD'
PREFIX lO: <https://w3id.org/italia/onto/l0/>    # Creo il prefisso per referenziare l'IRI della proprietà 'Descrizione' del sito dati.beniculturali.it

SELECT ?item ?itemLabel ?desc ?url WHERE {
    ?item wdt:P31 wd:Q33506 .  # Recupera tutte le istanze di Musei
    ?item wdt:P131 wd:Q13369 . # Che appartengono all'area amministrativa di Rimini
    ?item wdt:P5782 ?id .      # Recupera per ogni elemento l'Identificativo sul db unico Mibact
    BIND(URI(CONCAT("http://dati.beniculturali.it/mibact/luoghi/resource/CulturalInstituteOrSite/",?id)) AS ?url)  # Crea l'URI relativo del service Beniculturali.it
                                                                                                        
    SERVICE <http://dati.beniculturali.it/sparql> {
      ?url lO:description ?desc . # Recupera la descrizione degli Elementi tramite l'URI generato
    } 

    SERVICE wikibase:label { bd:serviceParam wikibase:language "it". }
}
Try it!