Wikidata:Request a query: Difference between revisions
Tagishsimon (talk | contribs) |
Tagishsimon (talk | contribs) |
||
(3 intermediate revisions by 2 users not shown) | |||
Line 1,033: | Line 1,033: | ||
} |
} |
||
}} --[[User:Tagishsimon|Tagishsimon]] ([[User talk:Tagishsimon|<span class="signature-talk">{{int:Talkpagelinktext}}</span>]]) 15:20, 22 February 2024 (UTC) |
}} --[[User:Tagishsimon|Tagishsimon]] ([[User talk:Tagishsimon|<span class="signature-talk">{{int:Talkpagelinktext}}</span>]]) 15:20, 22 February 2024 (UTC) |
||
== Missing german description linked german article == |
|||
Hello together, I'm looking for a quary of all items in Wikidata, where the description is missing but have an article in german Wikipedia. Perhaps quary can show if there is a label too or not? King regards --[[User:Crazy1880|Crazy1880]] ([[User talk:Crazy1880|<span class="signature-talk">{{int:Talkpagelinktext}}</span>]]) 16:41, 22 February 2024 (UTC) |
|||
:{{ping|Crazy1880}} Probably something like this. You'd need to run the report repeatedly with different OFFSETs to slice through all of the qualifying articles; WDQS cannot handle the volume of data in a single query. |
|||
:{{SPARQL|query=SELECT ?item ?itemLabel WHERE |
|||
{ |
|||
SERVICE bd:slice { |
|||
?article schema:isPartOf <https://de.wikipedia.org/> . |
|||
bd:serviceParam bd:slice.offset 0 . # Start at item number (not to be confused with QID) |
|||
bd:serviceParam bd:slice.limit 70000 . # List this many items |
|||
} |
|||
?article schema:about ?item . |
|||
optional {?item rdfs:label ?itemLabel . filter(lang(?itemLabel)="de") } |
|||
FILTER NOT EXISTS {?item schema:description ?itemDescription . filter(lang(?itemDescription)="de") } |
|||
FILTER NOT EXISTS { ?item wdt:P31/wdt:P279* wd:Q4167836 . hint:Prior hint:gearing "forward". } |
|||
FILTER NOT EXISTS { ?item wdt:P31 wd:Q11266439 .} |
|||
FILTER NOT EXISTS { ?item wdt:P31 wd:Q15184295 .} |
|||
FILTER NOT EXISTS { ?item wdt:P31 wd:Q11753321 .} |
|||
FILTER NOT EXISTS { ?item wdt:P31 wd:Q17633526 .} |
|||
FILTER NOT EXISTS { ?item wdt:P31 wd:Q19887878 .} |
|||
} |
|||
}} --[[User:Tagishsimon|Tagishsimon]] ([[User talk:Tagishsimon|<span class="signature-talk">{{int:Talkpagelinktext}}</span>]]) 19:34, 22 February 2024 (UTC) |
|||
::Thanks [[User:Tagishsimon|Tagishsimon]], looks very nice, but what do you mean with to slice? Can you tell me, what i have to change then? Thanks --[[User:Crazy1880|Crazy1880]] ([[User talk:Crazy1880|<span class="signature-talk">{{int:Talkpagelinktext}}</span>]]) 19:52, 22 February 2024 (UTC) |
|||
:::{{ping|Crazy1880}} The value at ''bd:serviceParam bd:slice.offset''. It is set to 0 right now, and is looking at the first 70,000 de.wikipedia articles. To get the next set, change the value to 70000; and then next to 140000, and so on. If is times out, set it to a lower increment. The slice service serves up sets of triples based on a fixed index, so if we imagine there are 1,000,000 de wiki articles, it allows you to select a subset of these based on the OFFSET (where to start in the index) and the LIMIT (how many to look at, starting from the OFFSET value. --[[User:Tagishsimon|Tagishsimon]] ([[User talk:Tagishsimon|<span class="signature-talk">{{int:Talkpagelinktext}}</span>]]) 20:57, 22 February 2024 (UTC) |
|||
::::Or you could run the whole thing in one go on Qlever - https://qlever.cs.uni-freiburg.de/wikidata/v6kNws - and get 369,123 rows --[[User:Tagishsimon|Tagishsimon]] ([[User talk:Tagishsimon|<span class="signature-talk">{{int:Talkpagelinktext}}</span>]]) 20:57, 22 February 2024 (UTC) |
Revision as of 20:57, 22 February 2024
Request a query ![]() This is a page where SPARQL queries [Q114898838] can be requested. Please provide feedback if a query is written for you. You can also request help to rewrite queries that don't work anymore, due to the WDQS graph split. For sample queries, see Examples and Help:Dataset sizing. Property talk pages include also summary queries for these. For help writing your own queries, or other questions about queries, see Wikidata talk:SPARQL query service/queries and Wikidata:SPARQL query service/query optimization. Help resources about Wikidata Query Service (Q20950365) and SPARQL: Wikidata:SPARQL query service/Wikidata Query Help and Category:SPARQL. To report an issue about the Query Service (interface, results views, export...) please see Wikidata:Contact the development team/Query Service and search. |
![]() |
On this page, old discussions are archived. An overview of all archives can be found at this page's archive index. The current archive is located at 2025/05. |
Items using given property
I browsed through all the statistics pages and all the external tools working with Wikidata. I browsed for examples from the Query Builder, but I failed to quickly find and adapt an existing query. I also honestly did read the manual of the query language but found no similar examples.
Could anyone please write a query that would let me analyze how a given property is being used.
So, for example, there is a property P31.
1) select all items having property P31, show labels, descriptions, ..., value of P31, other property names (if possible)
2) sort by the count of items having this or that value of P31 (to see what values count is the highest)
3) exclude from results items having values X and Y of P31 Nikolay Komarov (talk) 19:20, 6 February 2024 (UTC)
- I'm going to assume you didn't want all items with P31 because there are over 100 million of them. What you'll want here is my favorite optimization in Blazegraph: group by and count. It will only fire if specific conditions are met.
- Infrastruktur (talk) 20:56, 6 February 2024 (UTC)Try it!
select ?inst ?instLabel ?instDescription ?count ?search with { select ?inst (count(*) as ?count) where { [] wdt:P31 ?inst . } group by ?inst order by desc(?count) limit 1000 } as %i where { include %i # filter (?inst not in (wd:Q8054)) # unlist protein (WHY?!?) bind(uri(concat("https://www.wikidata.org/w/index.php?title=Special%3ASearch&ns0=1&search=haswbstatement%3AP31%3D", strafter(str(?inst), "http://www.wikidata.org/entity/"))) as ?search) service wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" . } } order by desc(?count)
- Qlever has the grunt for this sort of query: https://qlever.cs.uni-freiburg.de/wikidata/MGmG4E --Tagishsimon (talk) 03:51, 7 February 2024 (UTC)
- This little tidbit is not even a day old: https://github.com/ad-freiburg/qlever/commit/f9c9ff4ff538484c92690143e0e400d4f34f0e70 . Pleasantly surprised. Infrastruktur (talk) 13:20, 10 February 2024 (UTC)
- Qlever has the grunt for this sort of query: https://qlever.cs.uni-freiburg.de/wikidata/MGmG4E --Tagishsimon (talk) 03:51, 7 February 2024 (UTC)
Surnames
I'm looking for a list of all surnames in all languages. --2.205.64.82 15:39, 8 February 2024 (UTC)
Is it as simple as this?
SELECT ?item ?itemLabel WHERE { ?item wdt:P31 wd:Q101352. # <span class="mw-translate-fuzzy">Muss von einer Katze sein</span> SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } # <span lang="en" dir="ltr" class="mw-content-ltr">Helps get the label in your language, if not, then en language</span> }
It makes me run into a timeout and I get a lot of additional data, taht I don't want (which I could filter out…)
Zeitüberschreitungsgrenze erreicht { "head" : { "vars" : [ "item", "itemLabel" ] }, "results" : { "bindings" : [ { "item" : { "type" : "uri", "value" : "http://www.wikidata.org/entity/Q143129" }, "itemLabel" : { "xml:lang" : "de", "type" : "literal", "value" : "Fagan" } }, { "item" : { "type" : "uri", "value" : "http://www.wikidata.org/entity/Q144532" }, "itemLabel" : { "xml:lang" : "de", "type" : "literal", "value" : "Zając" } }, …
--2.205.64.82 15:46, 8 February 2024 (UTC)
- There are about 500,000 surname items, too many for the label service to be able to deliver labels for in the time permitted. Suggest you slice your way through them with this (or else run it on Qlever - https://qlever.cs.uni-freiburg.de/wikidata/ZtVhVA ):
- --Tagishsimon (talk) 19:46, 8 February 2024 (UTC)Try it!
SELECT ?item ?itemLabel WHERE { SERVICE bd:slice { ?item wdt:P31 wd:Q101352 . bd:serviceParam bd:slice.offset 0 . # Start at item number (not to be confused with QID) bd:serviceParam bd:slice.limit 30000 . # List this many items } SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } }
Changes made to a property by a user that are still in place
Sorry, me again. I think I need a list of all the country (P17) changes made by me, User:vicarage to items that are a subclass of (P279) of battle (Q178561) made on the 2nd of February 2024 that are still in place. I will combine it with my contemporary country query above to remove the invalid ones. Vicarage (talk) 09:56, 9 February 2024 (UTC)
- @Vicarage: Don't know how to give you that. Here are all the battle type items with a last edit date of 2 Feb 2024, with their countries. Someone was very busy that day :(
- --Tagishsimon (talk) 10:15, 9 February 2024 (UTC)Try it!
SELECT DISTINCT ?item ?itemLabel ?country ?countryLabel WHERE { ?item wdt:P31/wdt:P279* wd:Q178561 . ?item schema:dateModified ?date . hint:Prior hint:rangeSafe true. FILTER("2024-02-02"^^xsd:dateTime <= ?date && ?date < "2024-02-03"^^xsd:dateTime) ?item wdt:P17 ?country . SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } }
- ... well, some of them were right! Vicarage (talk) 10:52, 9 February 2024 (UTC)
Interactive results different from url results
I'm not sure about if this is the right place to ask, so please forgive me if it isn't.
When I run this query I get 9553 results, which looks fine:
SELECT ?item ?itemLabel ?ipac ?idbcn WHERE {
?item wdt:P131+ wd:Q1492.
?item wdt:P17 wd:Q29.
OPTIONAL { ?item wdt:P1600 ?ipac.}
OPTIONAL { ?item wdt:P11557 ?idbcn.}
SERVICE wikibase:label { bd:serviceParam wikibase:language "ca, en, es" }
}
However, when I try to put the query into an url to download the results and analyse them using R, I only get 6981 results. The url is https://query.wikidata.org/sparql?query=SELECT%20?item%20?itemLabel%20?ipac%20?idbcn%20WHERE%20%7B%0A%20%20?item%20wdt:P131+%20wd:Q1492.%0A%20%20?item%20wdt:P17%20wd:Q29.%0A%20%20OPTIONAL%20%7B%20?item%20wdt:P1600%20?ipac.%7D%0A%20%20OPTIONAL%20%7B%20?item%20wdt:P11557%20?idbcn.%7D%0A%20%20SERVICE%20wikibase:label%20%7B%20bd:serviceParam%20wikibase:language%20%22ca,%20en,%20es%22%20%7D%0A%7D and I've checked that the text raw text it downloads has less than 7000 result and I've identified some prominent missing results that should be there.
Just for context, the R code to produce the URL is just:
query <- 'SELECT ?item ?itemLabel ?ipac ?idbcn WHERE {
?item wdt:P131+ wd:Q1492.
?item wdt:P17 wd:Q29.
OPTIONAL { ?item wdt:P1600 ?ipac.}
OPTIONAL { ?item wdt:P11557 ?idbcn.}
SERVICE wikibase:label { bd:serviceParam wikibase:language "ca, en, es" }
}'
url <- paste0("https://query.wikidata.org/sparql?query=", URLencode(query))
Making changes to the query doesn't solve the difference of behaviours (I've tried removing the optional parts, the labels or P17, and also removing some of them at the same time).
I'm quite lost because I often use queries this way and I always get the same result as interactively. Pere prlpz (talk) 21:09, 9 February 2024 (UTC)
- @Pere prlpz: Seems like it might be running this query, looking for wdt:P131 rather than wdt:P131+ ...
- Try it!
SELECT ?item ?itemLabel ?ipac ?idbcn WHERE { ?item wdt:P131 wd:Q1492. ?item wdt:P17 wd:Q29. OPTIONAL { ?item wdt:P1600 ?ipac.} OPTIONAL { ?item wdt:P11557 ?idbcn.} SERVICE wikibase:label { bd:serviceParam wikibase:language "ca, en, es" } }
- If that is the case, this is the solution? (URL encode for + is %2B). idk why R is not URLencoding the + ...
- https://query.wikidata.org/sparql?query=SELECT%20?item%20?itemLabel%20?ipac%20?idbcn%20WHERE%20%7B%0A%20%20?item%20wdt:P131%2B%20wd:Q1492.%0A%20%20?item%20wdt:P17%20wd:Q29.%0A%20%20OPTIONAL%20%7B%20?item%20wdt:P1600%20?ipac.%7D%0A%20%20OPTIONAL%20%7B%20?item%20wdt:P11557%20?idbcn.%7D%0A%20%20SERVICE%20wikibase:label%20%7B%20bd:serviceParam%20wikibase:language%20%22ca,%20en,%20es%22%20%7D%0A%7D --Tagishsimon (talk) 21:34, 9 February 2024 (UTC)
- Thank you. I didn't expect the + to be the problem but now I remember that I had a similar problem some time ago with PetScan, which also encoded wrongly the + sign. Maybe encoding + is difficult for some reason and both R and PetScan get it wrong.
- Now that I know that the + is the problem, my workaround for this particular case will be using * - which seems to work fine.--Pere prlpz (talk) 22:26, 9 February 2024 (UTC)
Insight into tranlated Wikinews articles
The en.wikinews is exploring an upgrade of the Creative Commons license. One of the concerns is that English articles are often used as source content for other languages. I am trying to see if Wikidata can be used to better understand how many English articles are translated to other languages.
I have very little experience with Wikidata and no experience with SPARQL. I worked with ChatGPT to build a few simple queries that might help.
Can someone take a look at this page[1] and see if I'm completely off track or suggest improvements?
Thank you in advance.
Michael.C.Wright (talk) 16:22, 11 February 2024 (UTC)
- Below is one of the queries I'm using in an attempt to see en.Wikinews articles that are linked to at least one other language.
- Try it!
SELECT (COUNT(DISTINCT ?wikidataItem) AS ?totalArticlesWithEnglishPlusOneOtherLanguage) WHERE { # Find articles linked to the Wikidata item in English Wikinews ?enArticle schema:about ?wikidataItem; schema:isPartOf <https://en.wikinews.org/>. # Find articles linked to the same Wikidata item in any other language ?otherArticle schema:about ?wikidataItem; schema:isPartOf ?otherWikinewsSite. FILTER (?otherWikinewsSite != <https://en.wikinews.org/>) FILTER NOT EXISTS { ?enArticle schema:name ?enName. FILTER(CONTAINS(?enName, "Template:")|| CONTAINS(?enName, "Category:") || CONTAINS(?enName, "Portal:") || CONTAINS(?enName, "Page:")) } }
- I have a couple of questions:
- 1. Are there any problems with this query that I am not seeing that should be considered?
- 2. Should Wikinewsies be using the Wikidata statement "has edition or translation" to indicate translations of articles?
- Thanks again for any help.
- Michael.C.Wright (talk) 16:41, 20 February 2024 (UTC)
Commons Query - sort videos by longest to shortest
I've been able to figure out from example queries how to get the commons query service to show me all the files in a category, however now I want to sort by longest to shortest (which also implies that they are videos). All I have at the moment
SELECT ?file ?title
WHERE
{
SERVICE wikibase:mwapi
{
bd:serviceParam wikibase:api "Generator" .
bd:serviceParam wikibase:endpoint "commons.wikimedia.org" .
bd:serviceParam mwapi:gcmtitle "Category:Media of the day" .
bd:serviceParam mwapi:generator "categorymembers" .
bd:serviceParam mwapi:gcmtype "file" .
bd:serviceParam mwapi:gcmlimit "max" .
?title wikibase:apiOutput mwapi:title .
?pageid wikibase:apiOutput "@pageid" .
}
BIND (URI(CONCAT('https://commons.wikimedia.org/entity/M', ?pageid)) AS ?file)
}
Carlinmack (talk) 18:16, 11 February 2024 (UTC)
- Infrastruktur (talk) 08:51, 12 February 2024 (UTC)Try it!
SELECT ?media ?title ?duration with { select ?media ?title where { SERVICE wikibase:mwapi { bd:serviceParam wikibase:api "Generator" . bd:serviceParam wikibase:endpoint "commons.wikimedia.org" . bd:serviceParam mwapi:gcmtitle "Category:Media of the day" . bd:serviceParam mwapi:generator "categorymembers" . bd:serviceParam mwapi:gcmtype "file" . bd:serviceParam mwapi:gcmlimit "max" . ?title wikibase:apiOutput mwapi:title . ?pageid wikibase:apiOutput "@pageid" . } BIND (URI(CONCAT('https://commons.wikimedia.org/entity/M', ?pageid)) AS ?media) } } as %m WHERE { include %m ?media a schema:VideoObject . ?media schema:duration ?duration_ . bind ("00:00:00"^^xsd:time + ?duration_ as ?duration) } order by desc(?duration)
- TIL. Wasn't aware that Commons was already doing all the things it should obviously do. c:Commons:Structured data/Properties table. --Tagishsimon (talk) 11:15, 12 February 2024 (UTC)
- Wow great! Must say I don't quite understand how these bd: and schema: things are related but glad it works, thank you! Carlinmack (talk) 18:47, 12 February 2024 (UTC)
Wiki loves monument UK - Image Submission
Hello, Wikidata Community!
I'm working on a project to visualise data from the Wiki Loves Monuments UK contest, aiming to display the contributions of participants from the years 2013 to 2023. My approach involves querying Wikidata for image submissions under specific heritage designations, as recommended by the eligible buildings list for the contest. I'm seeking advice on refining my query and ensuring I capture the full scope of relevant data.
Process:
- Querying Wikidata: I utilize the Wikidata Query Service (SPARQL) to identify items associated with specific heritage designations.
- Heritage Designations: Based on the guidelines, I target the following categories and their corresponding Wikidata Q numbers:
- Scheduled Monument -
Q219538
- Grade I Listed Building -
Q15700818
- Grade II* Listed Building -
Q15700831
- Grade II Listed Building -
Q15700834
- Category A Listed Building -
Q10729054
- Category B Listed Building -
Q10729125
- Category C Listed Building -
Q10729142
- Grade A Listed Building -
Q71055272
- Grade B Listed Building -
Q71056106
- Grade C Listed Building -
Q71056072
- Scheduled Monument -
- Gathering Additional Information: For each retrieved image URL, I query Wikimedia Commons to obtain the artist (image author) and the original submission date (
dateTimeOriginal
).
Query:
Here's an example of the SPARQL query I've been using:
SELECT DISTINCT ?itemLabel ?image ?instanceOfTypeLabel ?location ?inception ?adminEntityLabel ?historicCountyLabel WHERE {
?item p:P1435 ?statement.
?statement ps:P1435/(wdt:P279*) wd:{heritage_id}.
?item wdt:P18 ?image.
?item wdt:P31 ?instanceOfType.
?instanceOfType rdfs:label ?instanceOfTypeLabel FILTER(LANG(?instanceOfTypeLabel) = "en").
?item wdt:P625 ?location.
OPTIONAL { ?item wdt:P571 ?inception. }
OPTIONAL {
?item wdt:P131 ?adminEntity.
?adminEntity rdfs:label ?adminEntityLabel FILTER(LANG(?adminEntityLabel) = "en").
}
OPTIONAL {
?item wdt:P131 ?historicCounty.
?historicCounty rdfs:label ?historicCountyLabel FILTER(LANG(?historicCountyLabel) = "en").
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
Issue:
This approach has yielded 27,595 items, which I've narrowed down by filtering submissions by date and removing duplicates, resulting in 775 refined items. However, comparing this outcome with the official Wiki Loves Monuments statistics, which recorded 12,851 contributions for 2023 alone, suggests that my method may be missing significant data.
I would really appreciate any feedback or suggestions to improve the accuracy and completeness of my data collection process. Are there adjustments to the query or additional steps that I should consider to better align my dataset with the contest's submissions?
Thank you! UnChampignon (talk) 09:52, 13 February 2024 (UTC)
- @UnChampignon: Well, a few observations; 1) possibly no point in fetching labels manually and using the label service at the same time 2) P131 does not fetch historic county 3) you can deal with duplication in the query by grouping 4) there are 124623 items in your target set which have images. Beyond that, there's not much wrong with your query; unclear why you get the figures you report.
- Try it!
#title=Count items with image SELECT (count(DISTINCT ?item) as ?count) WHERE { VALUES ?heritage_designation { wd:Q219538 # Scheduled Monument wd:Q15700818 # Grade I Listed Building wd:Q15700831 # Grade II* Listed Building wd:Q15700834 # Grade II Listed Building wd:Q10729054 # Category A Listed Building wd:Q10729125 # Category B Listed Building wd:Q10729142 # Category C Listed Building wd:Q71055272 # Grade A Listed Building wd:Q71056106 # Grade B Listed Building wd:Q71056072 # Grade C Listed Building } ?item wdt:P1435 ?heritage_designation . ?item wdt:P18 ?image. }
- --Tagishsimon (talk) 14:10, 13 February 2024 (UTC)Try it!
SELECT DISTINCT ?itemLabel (sample(?image_) as ?image) (group_concat(distinct ?instanceOfTypeLabel_;separator="; " ) as ?instanceOfTypeLabel) ?location ?inception (group_concat(distinct ?adminEntityLabel_;separator="; " ) as ?adminEntityLabel) ?historicCountyLabel WHERE { ?item wdt:P1435 wd:Q10729142. # <<<<<< Obvs, change this for each heritage type ?item wdt:P18 ?image_. ?item wdt:P31 ?instanceOfType. ?item wdt:P625 ?location. OPTIONAL { ?item wdt:P571 ?inception . } OPTIONAL { ?item wdt:P131 ?adminEntity . } OPTIONAL { ?item wdt:P7959 ?historicCounty . } SERVICE wikibase:label { bd:serviceParam wikibase:language "en". ?item rdfs:label ?itemLabel . ?instanceOfType rdfs:label ?instanceOfTypeLabel_ . ?adminEntity rdfs:label ?adminEntityLabel_ . ?historicCounty rdfs:label ?historicCountyLabel .} } group by ?itemLabel ?location ?inception ?historicCountyLabel
Help populating a new Category in Simple EN
ORIGINALLY POSTED 31 January 2024; archived with no response
Is it possible to search W:People from the Gaza Strip and all its subcategories, to obtain which articles exist in the Simple Wikipedia? The purpose is to (manually) populate the new Simple:Category:People from the Gaza Strip? Thank you - I'm not advanced enough to write my own SPARQL but appreciate the results! -- Deborahjay (talk) 17:38, 16 February 2024 (UTC)
- Typically one ends up doing such a thing in two steps. Here for the first step we can obtain a list of pages belonging to a category with a tool such as pywikibot or petscan. Since this only yields a few results we can supply this as exogenous bindings to the SPARQL query engine. From there it only takes a few more lines to get a result. Click "expand".
- Since you said you intended to populate the pages on simplewiki I had it list pages there which were missing. To have it list pages that exist instead just replace "filter not exists" with "filter exists".
- Infrastruktur (talk) 18:25, 16 February 2024 (UTC)Try it!
select * where { values ?title_en { "Abdel Aziz al-Rantisi"@en "Al-Shafi'i"@en "Israel ben Moses Najara"@en "Ghazi al-Jabali"@en "Izz El-Deen Sheikh Khalil"@en "Mohammed Dahlan"@en "Iman Darweesh Al Hams"@en "Killing of Muhammad al-Durrah"@en "Ibrahim al-Makadmeh"@en "Dorotheus of Gaza"@en "Saint Porphyrius"@en "Salah Shehade"@en "Nidal Farahat"@en "Wa'el Nassar"@en "Mousa Abu Marzook"@en "Fathi Shaqaqi"@en "Abdel Bari Atwan"@en "Kamal Nasser"@en "Aeneas of Gaza"@en "Umm Nidal"@en "Jamal Abu Samhadana"@en "Abd Al Aziz Awda"@en "Nathan of Gaza"@en "Raji Sourani"@en "Ghassan Elashi"@en "Jamil Majdalawi"@en "Mohammed Shabir"@en "Moses Najara II"@en "Vitalis of Gaza"@en "Ziad Abu Amr"@en "Hani Talab al-Qawasmi"@en "Riyad Hassan El-Khoudary"@en "Ahmad Kurd"@en "Ahmed Jabari"@en "Faris Odeh"@en "Reem Riyashi"@en "Haidar Abdel-Shafi"@en "Rashid Masharawi"@en "Zakaria al-Agha"@en "Intissar al-Wazir"@en "Muin Bseiso"@en "Shaker Asad"@en "Nader al-Masri"@en "Agapius of Palestine"@en "Ramzi Saleh"@en "Mohammed Omer (journalist)"@en "Hilles clan"@en "Ihab al-Ghussein"@en "Nizar Rayan"@en "Abu Zakaria al-Jamal"@en "Sa'id al-Shawwa"@en "Rashad al-Shawwa"@en "Shurrab family"@en "Saeb Jendeya"@en "Ali H. S. Hajjaj"@en "Kamal Naji"@en "Said Al-Sobakhi"@en "Ahmed Keshkesh"@en "Fathi Hamad"@en "Khaled Juma"@en "Fahmi al-Husseini"@en "Ayman Taha"@en "Abdel Latif Moussa"@en "Killing of Khalil al-Mughrabi"@en "Aqil Agha"@en "Nahid al-Rayyis"@en "Amin al-Hindi"@en "Laila Shawa"@en "Rushdi al-Shawwa"@en "Izzeldin Abuelaish"@en "Timotheus of Gaza"@en "Ramadan Shalah"@en "Ahmad Pasha ibn Ridwan"@en "Dirar Abu Seesi"@en "Eliezer ben-Arhah"@en "Asma al-Ghul"@en "Ismail Al-Amour"@en "Houssam Wadi"@en "Abdelhamid Abuhabib"@en "Iyad Abu Gharqoud"@en "Mohammed Shbair"@en "Raed al Atar"@en "Salim al-Za'nun"@en "Wafa al Bass"@en "Tawfiq Ali"@en "Husayn Pasha"@en "Musa Pasha ibn Hasan"@en "Raed Fares"@en "Yasser Elshantaf"@en "Alaa Atiya"@en "Sheikh Ibrahim Mudayris"@en "Ismail Abu Shanab"@en "Mohammed Assaf"@en "Sanna Abubkheet"@en "Ismail Haniyeh"@en "Raghib al-Alami"@en "Khalil al-Hayya"@en "Farah Baker"@en "Hesham Salhe"@en "Ahmad Bahar (Palestinian politician)"@en "Musa Agha al-Hasi"@en "Husayn Pasha ibn Makki"@en "Ayman al-Aloul"@en "Amna Suleiman"@en "Yahya Sinwar"@en "Yaser Murtaja"@en "Fadi Mohammad al-Batsh"@en "Killing of Rouzan al-Najjar"@en "Mohammed Saleh (footballer, born 1993)"@en "Mahmoud Wadi"@en "Ziyad al-Nakhalah"@en "Death of Mohammed Sobhi al-Judeili"@en "Ammar Campa-Najjar"@en "May Sayegh"@en "Baha Abu al-Ata"@en "Husam Zomlot"@en "Saeed Abu Ali"@en "Omaya Joha"@en "Fatma Omar An-Najar"@en "Rawya Shawa"@en "Intisar Abu Amara"@en "John of Gaza"@en "Bader Mousa"@en "Wael Al-Dahdouh"@en "Mohammed Hamada"@en "MC Abdul"@en "Abdul al-Majali"@en "Abu Julia"@en "Ziad Al-Zaza"@en "Heba Al-Farra"@en "Abu Obaida (Hamas)"@en "Tayseer al-Jabari"@en "Fatima Al-Zahra'a Shbair"@en "Nasser Abu Hamid"@en "Mohammed Balah"@en "Killing of Rami Ayyad"@en "Ehab Bessaiso"@en "Omar Ferwana"@en "Ayman Nofal"@en "Malak Mattar"@en "Hiba Abu Nada"@en "Amal Hamad"@en "Ibrahim al-Astal"@en "Mandy and Lara Sirdah"@en "Omar Abu Shawish"@en "Wael Al Zard"@en "Hind Khoudary"@en "Motaz Azaiza"@en "Yousef Bashir"@en "Silvanus of Gaza (hieromartyr)"@en "Mohamed al-Dalou"@en "Sufian Tayeh"@en "Refaat Alareer"@en "Belal Jadallah"@en "Yousef al-Mansi"@en "Yusra Al Barbari"@en "Awni El-Dous"@en "Yahya Al-Sarraj"@en "Mohammed Sinwar"@en "Moumen al-Natour"@en "Sulayman al-Ghazzi"@en "Kegham Djeghalian"@en } ?article_en schema:name ?title_en . ?article_en schema:about ?item . filter not exists { ?article_simple schema:about ?item . ?article_simple schema:isPartOf <https://simple.wikipedia.org/> . } } order by ?title_en
- Just noticed you can obtain this list using Petscan alone too. Edit: page exists https://petscan.wmflabs.org/?psid=27009566 page doesn't exist https://petscan.wmflabs.org/?psid=27009571 Infrastruktur (talk) 18:34, 16 February 2024 (UTC)
Properties by creation year
Hi! Given this query:
SELECT ?property ?propertyLabel
WHERE {
?property wdt:P31/wdt:P279* wd:Q96776953.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
is it possible to add a column with the year of creation of each property? I made some unsuccessfull attempts through mwapi. Thanks in advance! --Epìdosis 19:19, 16 February 2024 (UTC)
- @Epìdosis: Yes, more or less. The PID milestone (Q73616850) item provides a chronology of PIDs at https://www.wikidata.org/wiki/Q73616850#P1114 and so it's possible to get SPARQL to identify the milestone date immediately greater (or immediately less) than each of the PIDs in your query. I've not looked, but I guess a pair of contiguous milestones falling in two successive calendar years runs the risk of suggesting a property was created in the later year rather than the earlier year (using the method below, which takes the greater than date). Still; might be good enough for rock'n'roll?
- I'm afraid I don't know MWAPI well enough to know whether it can be used to provide more accurate dates.
- --Tagishsimon (talk) 00:40, 17 February 2024 (UTC)Try it!
SELECT ?property ?propertyLabel ?number ?date (YEAR(?date) as ?year) WITH { SELECT ?property ?number (MIN(?milestone_) as ?milestone) WHERE { ?property wdt:P31/wdt:P279* wd:Q96776953. BIND(xsd:integer(strafter(str(?property),"P")) as ?number) wd:Q73616850 wdt:P1114 ?milestone_ . FILTER(?milestone_ > ?number) } group by ?property ?number } as %i WHERE { INCLUDE %i wd:Q73616850 p:P1114 ?stat . ?stat ps:P1114 ?milestone . ?stat pq:P585 ?date SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } }
- @Tagishsimon: Thanks, it is a good approximation and I didn't know PID milestone (Q73616850)! Epìdosis 18:30, 17 February 2024 (UTC)
List all properties and their aliases used by a class or any of its subclasses
I am looking to list all properties and their aliases used by a class or any of its subclasses. For example, I'd like to get all properties that make claims about a human (Q5), or all properties that make claims about a geographic area (city, state, country, region, etc.), or all properties that makes claims about an organization, etc. Ideally the properties would be sorted by how often they are used by the class and subclasses, but this isn't a requirement if all properties are returned.
I've been using the two queries below, but they don't seem to capture all possible properties. Thanks for any help!
Query A:
SELECT ?class ?classLabel ?property ?propertyLabel (GROUP_CONCAT(?alias; separator="; ") AS ?aliases)
WHERE {
?tree0 (wdt:P279)* ?class .
BIND (wd:Q5 AS ?tree0)
?class wdt:P1963 ?property .
?property a wikibase:Property ;
rdf:type wikibase:Property ;
wikibase:propertyType ?type ;
skos:altLabel ?alias .
FILTER(?type != wikibase:PropertyType && ?type != wikibase:ExternalId)
FILTER(LANG(?alias) = "en")
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
GROUP BY ?class ?classLabel ?property ?propertyLabel
ORDER BY ?class ?propertyId
Query B:
SELECT ?property ?propertyLabel (GROUP_CONCAT(?alias; separator="; ") AS ?aliases) {
VALUES (?class) {(wd:Q5)}
?property a wikibase:Property .
?property p:P2302 [ ps:P2302 wd:Q21503250 ;
pq:P2309 wd:Q21503252 ;
pq:P2308 ?class ] .
?property a wikibase:Property ;
rdf:type wikibase:Property ;
wikibase:propertyType ?type ;
skos:altLabel ?alias .
FILTER(?type != wikibase:PropertyType && ?type != wikibase:ExternalId)
FILTER(LANG(?alias) = "en")
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}
GROUP BY ?property ?propertyLabel
ORDER BY ASC(xsd:integer(strafter(str(?property), concat(str(wd:), "P"))))
EasyAsCake (talk) 17:00, 18 February 2024 (UTC)
- @EasyAsCake: I have such a report, which makes use of the
?property wikibase:directClaim ?predicate .
doodab suggested at https://www.mediawiki.org/wiki/Wikibase/Indexing/RDF_Dump_Format#Properties ... so, here, the report is looking at truthy statements of the sort which a wdt: predicate would return. Should you want to amend the report to look at all statements, then substitute in wikibase:claim for wikibase:directClaim. The example, below, is looking at properties for UK lakes. Somewhat gratuitously, it contains a report-within-a-report allowing the user to look at items having the property. Obvs, there are limits to what WDQS is capable of; running this for all ~11 million human items is probably not going to work. I'll leave you to add the aliases in these, since you seem to have a good grasp of aggregation, and I'm quite lazy.
- --Tagishsimon (talk) 17:10, 18 February 2024 (UTC)Try it!
SELECT ?property ?propertyLabel ?count ?report WITH { SELECT ?property (count(distinct ?item) as ?count) WHERE { {?item wdt:P31/wdt:P279* wd:Q23397 .} # The class & subclasses hint:Prior hint:runFirst true . ?item wdt:P17 wd:Q145 . # in UK ?item ?predicate ?value . ?property wikibase:directClaim ?predicate . } group by ?property } as %i WHERE { INCLUDE %i SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . } BIND(IRI(CONCAT("https://query.wikidata.org/#SELECT%20%3Fitem%20%3FitemLabel%20%3Fvalue%20%3FvalueLabel%20WHERE%20%7B%0A%20%20%20%20%3Fitem%20wdt%3AP31%20wd%3AQ23397%20.%20%0A%20%20%20%20%3Fitem%20wdt%3AP17%20wd%3AQ145%20.%20%0A%20%20%20%20%3Fitem%20wdt%3A",STRAFTER(STR(?property),"http://www.wikidata.org/entity/"),"%20%3Fvalue.%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22en%22%20.%20%7D%0A%7D%0A")) as ?report) } order by desc(?count)
- You should put an optional on where you get the alias as the property might not have an alias. Infrastruktur (talk) 18:09, 18 February 2024 (UTC)
Filter by period of time (years) and labels instead of QIDs
Hi everyone, I have this query to get a list of the people in the period specified (1700-1800) who can speak Basque in the territories cited, and get in the list a column of not only the QIDs, but the person name/surname and the language, not its QID. No property (P) needed, as it is showing now. So far not good. How could I get these results? Regards
SELECT ?item ("P1412" AS ?propietatea) ("Q8752" AS ?hizkuntza) WHERE {
?item wdt:P31 wd:Q5.
?date_node wikibase:timePrecision "11"^^xsd:integer .
?date_node wikibase:timeValue ?date .
?item (wdt:P31/(wdt:P279*)) ?type;
wdt:P569 ?dateof_birth.
FILTER((?dateof_birth >= "1700-01-01"^^xsd:dateTime) && (?dateof_birth <= "1799-12-31"^^xsd:dateTime))
?item p:P569/psv:P569 ?date_node .
{?item wdt:P19/wdt:P131* wd:Q95010 .}
UNION {?item wdt:P19/wdt:P131* wd:Q81801 .}
UNION {?item wdt:P19/wdt:P131* wd:Q93366 .}
UNION {?item wdt:P19/wdt:P131* wd:Q671023 .}
UNION {?item wdt:P19/wdt:P131* wd:Q4018 .}
UNION {?item wdt:P19/wdt:P131* wd:Q638503 .}
UNION {?item wdt:P19/wdt:P131* wd:Q673040 .}
}
Iñaki LL (talk) 10:09, 19 February 2024 (UTC)
- @Iñaki LL: Probably this. You need to specify ?item wdt:P1412 wd:Q8752 as a triple; perhaps use the label service to retrieve the item's label, and use DISTINCT in the select since the report will produce duplicate rows where the P19/P131* triples overlap. I have also added the ?family_name in case this is of use; not all items will have a value for this.
- If you *really* want the language string as a column, then you could add ("Basque" AS ?hizkuntza). Clearly they all speak Basque because that's what the report selects for.
- --Tagishsimon (talk) 10:26, 19 February 2024 (UTC)Try it!
SELECT DISTINCT ?item ?itemLabel ?family_nameLabel WHERE { ?item wdt:P31 wd:Q5. ?date_node wikibase:timePrecision "11"^^xsd:integer . ?date_node wikibase:timeValue ?date . ?item (wdt:P31/(wdt:P279*)) ?type; wdt:P569 ?dateof_birth. FILTER((?dateof_birth >= "1700-01-01"^^xsd:dateTime) && (?dateof_birth <= "1799-12-31"^^xsd:dateTime)) ?item p:P569/psv:P569 ?date_node . {?item wdt:P19/wdt:P131* wd:Q95010 .} UNION {?item wdt:P19/wdt:P131* wd:Q81801 .} UNION {?item wdt:P19/wdt:P131* wd:Q93366 .} UNION {?item wdt:P19/wdt:P131* wd:Q671023 .} UNION {?item wdt:P19/wdt:P131* wd:Q4018 .} UNION {?item wdt:P19/wdt:P131* wd:Q638503 .} UNION {?item wdt:P19/wdt:P131* wd:Q673040 .} ?item wdt:P1412 wd:Q8752 . OPTIONAL {?item wdt:P734 ?family_name . } SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],eu,es,en". } }
- Thanks @Tagishsimon, that is helpful. I modulated your query to incorporate "Basque AS hizkuntza", still I got the QID of the language. However, as you note, no point in repeating the language name, since all results are about people who spoke or could speak Basque. I also removed family name option. But would it be possible to show the languages they ALSO speak in another column?
SELECT DISTINCT ?item ?itemLabel ("Q8752" AS ?hizkuntza) WHERE {
?item wdt:P31 wd:Q5.
?date_node wikibase:timePrecision "11"^^xsd:integer .
?date_node wikibase:timeValue ?date .
?item (wdt:P31/(wdt:P279*)) ?type;
wdt:P569 ?dateof_birth.
FILTER((?dateof_birth >= "1700-01-01"^^xsd:dateTime) && (?dateof_birth <= "1799-12-31"^^xsd:dateTime))
?item p:P569/psv:P569 ?date_node .
{?item wdt:P19/wdt:P131* wd:Q95010 .}
UNION {?item wdt:P19/wdt:P131* wd:Q81801 .}
UNION {?item wdt:P19/wdt:P131* wd:Q93366 .}
UNION {?item wdt:P19/wdt:P131* wd:Q671023 .}
UNION {?item wdt:P19/wdt:P131* wd:Q4018 .}
UNION {?item wdt:P19/wdt:P131* wd:Q638503 .}
UNION {?item wdt:P19/wdt:P131* wd:Q673040 .}
?item wdt:P1412 wd:Q8752 .
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],eu,es,en". }
}
Iñaki LL (talk) 11:37, 19 February 2024 (UTC)
- @Iñaki LL: Yes, along these lines; right now this gives multiple rows for someone who speaks multiple languages; it's possible to aggregate the values into a single row, but I'm uncertain if you want the QIDs or the labels, so I have not taken that step; happy to (later) if you wish.
- --Tagishsimon (talk) 12:13, 19 February 2024 (UTC)Try it!
SELECT DISTINCT ?item ?itemLabel ("Q8752" AS ?hizkuntza) ?language WHERE { ?item wdt:P31 wd:Q5. ?date_node wikibase:timePrecision "11"^^xsd:integer . ?date_node wikibase:timeValue ?date . ?item (wdt:P31/(wdt:P279*)) ?type; wdt:P569 ?dateof_birth. FILTER((?dateof_birth >= "1700-01-01"^^xsd:dateTime) && (?dateof_birth <= "1799-12-31"^^xsd:dateTime)) ?item p:P569/psv:P569 ?date_node . {?item wdt:P19/wdt:P131* wd:Q95010 .} UNION {?item wdt:P19/wdt:P131* wd:Q81801 .} UNION {?item wdt:P19/wdt:P131* wd:Q93366 .} UNION {?item wdt:P19/wdt:P131* wd:Q671023 .} UNION {?item wdt:P19/wdt:P131* wd:Q4018 .} UNION {?item wdt:P19/wdt:P131* wd:Q638503 .} UNION {?item wdt:P19/wdt:P131* wd:Q673040 .} ?item wdt:P1412 wd:Q8752 . OPTIONAL {?item wdt:P1412 ?language . filter (?language not in (wd:Q8752)) } SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],eu,es,en". } }
- Great. Labels would be best. I see the new column of the languages they also talked, but one only language added. However, sometimes they could speak more than two languages, e.g. Q3164591. Possibly another column for each language? Iñaki LL (talk) 12:33, 19 February 2024 (UTC)
- @Iñaki LL: Multiple columns for languages is more complex. Try this?
- --Tagishsimon (talk) 12:49, 19 February 2024 (UTC)Try it!
SELECT DISTINCT ?item ?itemLabel ("Q8752" AS ?hizkuntza) (GROUP_CONCAT(DISTINCT ?languageLabel_;separator="; ") as ?languageLabel) WHERE { ?item wdt:P31 wd:Q5. ?date_node wikibase:timePrecision "11"^^xsd:integer . ?date_node wikibase:timeValue ?date . ?item (wdt:P31/(wdt:P279*)) ?type; wdt:P569 ?dateof_birth. FILTER((?dateof_birth >= "1700-01-01"^^xsd:dateTime) && (?dateof_birth <= "1799-12-31"^^xsd:dateTime)) ?item p:P569/psv:P569 ?date_node . {?item wdt:P19/wdt:P131* wd:Q95010 .} UNION {?item wdt:P19/wdt:P131* wd:Q81801 .} UNION {?item wdt:P19/wdt:P131* wd:Q93366 .} UNION {?item wdt:P19/wdt:P131* wd:Q671023 .} UNION {?item wdt:P19/wdt:P131* wd:Q4018 .} UNION {?item wdt:P19/wdt:P131* wd:Q638503 .} UNION {?item wdt:P19/wdt:P131* wd:Q673040 .} ?item wdt:P1412 wd:Q8752 . OPTIONAL {?item wdt:P1412 ?language . filter (?language not in (wd:Q8752)) } SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],eu,es,en". ?item rdfs:label ?itemLabel . ?language rdfs:label ?languageLabel_ .} } group by ?item ?itemLabel ?hizkuntza
- That solution looks good enough. Thank you! Iñaki LL (talk) 13:30, 19 February 2024 (UTC)
- Great. Labels would be best. I see the new column of the languages they also talked, but one only language added. However, sometimes they could speak more than two languages, e.g. Q3164591. Possibly another column for each language? Iñaki LL (talk) 12:33, 19 February 2024 (UTC)
Single image of each person in query results
I’m trying to modify some image grids for Wikidata:WikiProject The Women’s Library LSESuffrageInterviewsProject – examples below, so that only one image of each person appears in the results.
https://w.wiki/9DoB or https://w.wiki/9DoL
I’ve been working with (SAMPLE(?image) as ?image) and various longer adjustments, but keep getting malformed query errors.
Any expertise would be appreciated! Many thanks HelsKRW (talk) 11:50, 20 February 2024 (UTC)
- @HelsKRW: For the second of these, the issue is that the two variable names in (SAMPLE(?image_) AS ?image) must be different; (SAMPLE(?image) AS ?image) does not work:
#defaultView:ImageGrid
SELECT DISTINCT ?person ?personLabel ?personDescription ?familynameLabel ?givennameLabel (SAMPLE(?image_) AS ?image) WHERE {
?person wdt:P5008 wd:Q117322976;
wdt:P31 wd:Q5;
wdt:P9600 wd:Q2371017.
OPTIONAL { ?person wdt:P734 ?familyname. }
OPTIONAL { ?person wdt:P735 ?givenname. }
OPTIONAL { ?person wdt:P18 ?image_. }
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
} GROUP BY ?person ?personLabel ?personDescription ?familynameLabel ?givennameLabel
ORDER BY (?familynameLabel)
--Tagishsimon (talk) 12:01, 20 February 2024 (UTC)
- @Tagishsimon, HelsKRW: Good catch on the
(SAMPLE(?image) AS ?image)
But the query is still coming up with multiple rows in the solution (and therefore multiple images) whenever there is more than one forename, or more than one surname (eg married name / maiden name; also some double-barreled surnames, depending how they've been recorded. - Approaches to get around this might be (i) to drop the forenames and surnames completely, if it's the image grid that is of prime interest; (ii) try to fish out the 'right' forename and surname (eg first forename; a particular one of the surnames -- but this would require qualifiers like series ordinal (P1545) and object of statement has role (P3831) to consistently be present, to get the right one); (iii) use
GROUP_CONCAT
to smash any multiple forenames or multiple surnames together -- but bearing in mind that there is no way to makeGROUP_CONCAT
respect any particular ordering. Jheald (talk) 12:22, 20 February 2024 (UTC)- Thank you both. It's the image grid that's of prime interest, so I've taken names out and that's working - thank you. But trying to apply the same logic to my other query isn't working so I'm wondering what I still haven't grasped about how to use it!
- HelsKRW (talk) 12:30, 20 February 2024 (UTC)Try it!
#defaultView:ImageGrid SELECT DISTINCT ?item ?itemLabel ?person ?personLabel (SAMPLE(?image_) AS ?image) WHERE { ?item wdt:P5008 wd:Q117322976; # item on focus list of the LSESuffrageInterviewsProject wdt:P31 wd:Q178651 . # item is an interview ?item p:P710 ?statement . # item has a participant statement ?statement ps:P710 ?person . # participant is ?person ?statement pq:P3831 wd:Q55534929. # qualifier object has role is interviewee ?item wdt:P921 ?person . # main subject is same as participant OPTIONAL { ?person wdt:P18 ?image_.} SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } } GROUP BY ?item ?itemLabel ?person ?personLabel ORDER BY xsd:integer(STRAFTER(str(?person), 'Q'))
- (ec) Similarly, the first query is producing multiple rows (and so multiple repeats of the same image) if there are multiple interviews for the same person -- one could perhaps force it to only produce an image for the interview with the lowest Q-number. Alternatively, in this case maybe multiple images are the right answer, if linking to the interviews is the key thing. Or one could perhaps return an image like c:File:Arrow slim right.svg for susbequent hits. Jheald (talk) 12:34, 20 February 2024 (UTC)
- Thank you. For this one multiple images are probably ok as they will link to the separate interviews. Thank you so much for your help! HelsKRW (talk) 12:54, 20 February 2024 (UTC)
- That's good. Here's a version anyway, with repeated images replaced by arrows, because it became too much of a challenge to see if I could get it to work or not :-)
- -- Jheald (talk) 13:51, 20 February 2024 (UTC)Try it!
#defaultView:ImageGrid SELECT DISTINCT ?item ?itemLabel ?seq ?person ?personLabel (SAMPLE(?image_) AS ?image) WITH { SELECT ?item ?person WHERE { ?item wdt:P5008 wd:Q117322976; # item on focus list of the LSESuffrageInterviewsProject wdt:P31 wd:Q178651 . # item is an interview ?item p:P710 ?statement . # item has a participant statement ?statement ps:P710 ?person . # participant is ?person ?statement pq:P3831 wd:Q55534929. # qualifier object has role is interviewee ?item wdt:P921 ?person . # main subject is same as participant } } AS %items WITH { SELECT (COUNT(DISTINCT(?item1)) AS ?seq) ?item ?person WHERE { INCLUDE %items . { SELECT (?item AS ?item1) ?person WHERE{ INCLUDE %items . } } FILTER (str(?item1) <= str(?item)) } GROUP BY ?item ?person } AS %items_with_seq WHERE { INCLUDE %items_with_seq OPTIONAL { INCLUDE %items_with_seq FILTER (?seq > 1) . ?person wdt:P18 ?img . BIND( IRI('http://commons.wikimedia.org/wiki/Special:FilePath/Arrow slim right.svg') AS ?image_ ) . } OPTIONAL { ?person wdt:P18 ?image_} . SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } } GROUP BY ?item ?itemLabel ?person ?personLabel ?seq ORDER BY xsd:integer(STRAFTER(str(?person), 'Q')) ?item
- Wow - thank you! That is definitely SPARQL at a level way beyond my current skills! I'll study it to learn more! Thank you. HelsKRW (talk) 14:03, 20 February 2024 (UTC)
- Thank you. For this one multiple images are probably ok as they will link to the separate interviews. Thank you so much for your help! HelsKRW (talk) 12:54, 20 February 2024 (UTC)
- (ec) Similarly, the first query is producing multiple rows (and so multiple repeats of the same image) if there are multiple interviews for the same person -- one could perhaps force it to only produce an image for the interview with the lowest Q-number. Alternatively, in this case maybe multiple images are the right answer, if linking to the interviews is the key thing. Or one could perhaps return an image like c:File:Arrow slim right.svg for susbequent hits. Jheald (talk) 12:34, 20 February 2024 (UTC)
- @Tagishsimon, HelsKRW: Good catch on the
- To explain JH's mad query; it has three sections: a first query which selects interviews and interviewees as normal. The results of that are passed to the second query, which duplicates the ?item QID as ?item1, generates a Cartesian product set of rows of ?item x ?item1, and then assigns a sequence number to each ?item by counting for each person, (via the filter and then the grouping), how many distinct ?item1 have a lower or equal QID number than the ?item; and passes the results of that query to the third query, which assigns an arrow image to ?item with a sequence number greater than 1, and chooses an optional normal image for ?item with sequence=1, and then groups the whole thing, sampling the image, to knock out any duplicate image rows. The pattern seen in the middle query - how to add a numeric index to a set of rows - is the secret sauce for the whole thing; the ?seq number can be seen if you flip the results into the table view. It is a standard design pattern - which is to say there is not a better way of doing what it does - but it is undoubtedly advanced level SPARQL of the sort which causes brains to explode when trying to figure out what's going on. --Tagishsimon (talk) 15:08, 20 February 2024 (UTC)
Property labels for all properties connected to an item
I am trying to get all information which is required to display a wikidata page for a given item, and to link back to all relevant bits of the actual Wikidata page. So given say Q1, I want to get:
- All connections Q1 makes/all the triples in which Q1 is the subject
- Links to each triple in which Q1 is the subject
- All the property labels for each property involved in the display. So for P31 for example I want to get "instance of" as the 'label'.
- All the item labels involved in the display. If say Q2 appeared as an object in Q1 I would want to get "Earth" as its label.
I have created this query:
#defaultView:ImageGrid
SELECT ?property ?propertyLabel ?statementLink ?simplevalue ?simplevalueLabel ?simplevalueLink ?propdirect ?propdirectLabel
WHERE
{
wd:Q1 ?property ?statementLink .
?statementLink ?simplevalueLink ?simplevalue .
wd:Q1 ?propdirect ?simplevalue.
FILTER(STRSTARTS(STR(?propdirect), "http://www.wikidata.org/prop/direct/"))
FILTER(STRSTARTS(STR(?property), "http://www.wikidata.org/prop/P"))
FILTER(STRSTARTS(STR(?simplevalueLink), "http://www.wikidata.org/prop/statement/"))
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } # Helps get the label in your language, if not, then en language
}
This gets all triples, and it also works as I want for item labels (columns simplevalue for the ID which can easily be converted to the link I need, and simplevalueLabel for the label associated), but while property gives me the property id, propertyLabel gives me a URL towards the property rather than the label I want. Propdirect has the same issue. How can I connect this directly to the right property so that I can get the property label? M.alten.tue (talk) 10:52, 21 February 2024 (UTC)
- @M.alten.tue: Picking up only on the property label part of you question, the query below illustrates how to map from a predicate to a property item, such that the label service can be used to retrieve the property label. Mappings for various predicates are set out at https://www.mediawiki.org/wiki/Wikibase/Indexing/RDF_Dump_Format#Properties
- Your question talks about e.g. Q1 being used as an object, but your example shows Q1 as a subject, which is why I've shied away from doing anything else ... please come back here again if you need more.
- --Tagishsimon (talk) 12:35, 21 February 2024 (UTC)Try it!
SELECT DISTINCT ?wdt_property ?wdt_propertyLabel ?p_property ?p_propertyLabel WHERE { wd:Q1 ?p_predicate ?statementLink . ?statementLink ?simplevalueLink ?simplevalue . wd:Q1 ?wdt_predicate ?simplevalue. ?p_property wikibase:claim ?p_predicate . ?wdt_property wikibase:directClaim ?wdt_predicate . SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } }
- Thank you so much, this is exactly what I needed. I'm sorry for the confusion about object vs. subject, I meant for it to be the subject like in the query. I used this to make the complete query:
- M.alten.tue (talk) 13:10, 21 February 2024 (UTC)Try it!
#defaultView:ImageGrid ::SELECT DISTINCT ?p_property ?p_propertyLabel ?statementLink ?simplevalue ?simplevalueLabel ::WHERE ::{ :: :: wd:Q1 ?property ?statementLink . :: ?statementLink ?simplevalueLink ?simplevalue . :: wd:Q1 ?propdirect ?simplevalue. :: :: #find property label (thanks to tagishsimon) :: ?p_property wikibase:claim ?property . :: :: FILTER(STRSTARTS(STR(?propdirect), "http://www.wikidata.org/prop/direct/")) :: FILTER(STRSTARTS(STR(?property), "http://www.wikidata.org/prop/P")) :: #BIND(STR(?simplevalueLink) as ?xxx) :: FILTER(STRSTARTS(STR(?simplevalueLink), "http://www.wikidata.org/prop/statement/")) :: SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } # Helps get the label in your language, if not, then en language ::} ::
Help Troubleshooting SPARQL Query for Wiki Loves Monuments Contest Data
I'm working on a project to gather data on heritage items eligible for the Wiki Loves Monuments contest. I'm able to get the scheduled monument and Grade IIs Listed Buildings but now I am having issue getting items for Grade II Listed Buildings.
I'm using a SPARQL query from previous conversation to retrieve details such as the item label, image, instance type, location, inception date, administrative entity, and historic county label for each item. However, I keep encountering a 500 error code when executing my query, indicating a server-side issue. I think the complexity of my query or a specific part of it might be causing this problem. Below is the query:
SELECT DISTINCT ?itemLabel
(sample(?image_) as ?image)
(group_concat(distinct ?instanceOfTypeLabel_;separator="; " ) as ?instanceOfTypeLabel)
?location
?inception
(group_concat(distinct ?adminEntityLabel_;separator="; " ) as ?adminEntityLabel)
?historicCountyLabel
WHERE {
?item p:P1435 ?statement.
?statement ps:P1435/(wdt:P279*) wd:Q15700834.
?item wdt:P18 ?image_.
?item wdt:P31 ?instanceOfType.
?item wdt:P625 ?location.
OPTIONAL { ?item wdt:P571 ?inception . }
OPTIONAL { ?item wdt:P131 ?adminEntity . }
OPTIONAL { ?item wdt:P7959 ?historicCounty . }
SERVICE wikibase:label { bd:serviceParam wikibase:language "en".
?item rdfs:label ?itemLabel .
?instanceOfType rdfs:label ?instanceOfTypeLabel_ .
?adminEntity rdfs:label ?adminEntityLabel_ .
?historicCounty rdfs:label ?historicCountyLabel .}
}
GROUP BY ?itemLabel ?location ?inception ?historicCountyLabel
LIMIT 1000 OFFSET 0
Does anyone have any insights or suggestions on how to refine this query to avoid the 500 error? Any advice on optimising the query for better performance or troubleshooting tips would be greatly appreciated! UnChampignon (talk) 11:58, 21 February 2024 (UTC)
- @UnChampignon: There are ~370,000 grade II listed building items, more than WDQS can cope with. Using LIMIT 1000 OFFSET 0 still requires the query engine to amass all rows before it can dispense the first 1000; and it is not, besides, a repeatable way of slicing through the data. Also, there are no subclasses of Q15700834 so using the p: ps: route is not worthwhile and is probably slowing the query. The SLICE service is a better way of segmenting data. Try this:
- --Tagishsimon (talk) 12:18, 21 February 2024 (UTC)Try it!
SELECT DISTINCT ?itemLabel (sample(?image_) as ?image) (group_concat(distinct ?instanceOfTypeLabel_;separator="; " ) as ?instanceOfTypeLabel) ?location ?inception (group_concat(distinct ?adminEntityLabel_;separator="; " ) as ?adminEntityLabel) ?historicCountyLabel WHERE { SERVICE bd:slice { ?item wdt:P1435 wd:Q15700834. bd:serviceParam bd:slice.offset 0 . # Start at item number (not to be confused with QID) bd:serviceParam bd:slice.limit 50000 . # List this many items } ?item wdt:P18 ?image_. ?item wdt:P31 ?instanceOfType. ?item wdt:P625 ?location. OPTIONAL { ?item wdt:P571 ?inception . } OPTIONAL { ?item wdt:P131 ?adminEntity . } OPTIONAL { ?item wdt:P7959 ?historicCounty . } SERVICE wikibase:label { bd:serviceParam wikibase:language "en". ?item rdfs:label ?itemLabel . ?instanceOfType rdfs:label ?instanceOfTypeLabel_ . ?adminEntity rdfs:label ?adminEntityLabel_ . ?historicCounty rdfs:label ?historicCountyLabel .} } GROUP BY ?itemLabel ?location ?inception ?historicCountyLabel
- Ah, thank you so much! This makes so much sense :) UnChampignon (talk) 13:25, 21 February 2024 (UTC)
retrieve all values of property P106 occupation
SELECT ?op ?opLabel WHERE {
?item wdt:P31 wd:Q5 . ?item p:P106 [ps:P106 ?op]. SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
} LIMIT 10 Dann.wu (talk) 15:10, 22 February 2024 (UTC)
- @Dann.wu: There doesn't seem a great virtue in taking the p: ps: route to P106 statement values rather than the wdt:, so, perhaps this:
- --Tagishsimon (talk) 15:20, 22 February 2024 (UTC)Try it!
SELECT ?occ ?occLabel WITH { SELECT DISTINCT ?occ WHERE { ?item wdt:P106 ?occ . } } as %i WHERE { INCLUDE %i SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } }
Missing german description linked german article
Hello together, I'm looking for a quary of all items in Wikidata, where the description is missing but have an article in german Wikipedia. Perhaps quary can show if there is a label too or not? King regards --Crazy1880 (talk) 16:41, 22 February 2024 (UTC)
- @Crazy1880: Probably something like this. You'd need to run the report repeatedly with different OFFSETs to slice through all of the qualifying articles; WDQS cannot handle the volume of data in a single query.
- --Tagishsimon (talk) 19:34, 22 February 2024 (UTC)Try it!
SELECT ?item ?itemLabel WHERE { SERVICE bd:slice { ?article schema:isPartOf <https://de.wikipedia.org/> . bd:serviceParam bd:slice.offset 0 . # Start at item number (not to be confused with QID) bd:serviceParam bd:slice.limit 70000 . # List this many items } ?article schema:about ?item . optional {?item rdfs:label ?itemLabel . filter(lang(?itemLabel)="de") } FILTER NOT EXISTS {?item schema:description ?itemDescription . filter(lang(?itemDescription)="de") } FILTER NOT EXISTS { ?item wdt:P31/wdt:P279* wd:Q4167836 . hint:Prior hint:gearing "forward". } FILTER NOT EXISTS { ?item wdt:P31 wd:Q11266439 .} FILTER NOT EXISTS { ?item wdt:P31 wd:Q15184295 .} FILTER NOT EXISTS { ?item wdt:P31 wd:Q11753321 .} FILTER NOT EXISTS { ?item wdt:P31 wd:Q17633526 .} FILTER NOT EXISTS { ?item wdt:P31 wd:Q19887878 .} }
- Thanks Tagishsimon, looks very nice, but what do you mean with to slice? Can you tell me, what i have to change then? Thanks --Crazy1880 (talk) 19:52, 22 February 2024 (UTC)
- @Crazy1880: The value at bd:serviceParam bd:slice.offset. It is set to 0 right now, and is looking at the first 70,000 de.wikipedia articles. To get the next set, change the value to 70000; and then next to 140000, and so on. If is times out, set it to a lower increment. The slice service serves up sets of triples based on a fixed index, so if we imagine there are 1,000,000 de wiki articles, it allows you to select a subset of these based on the OFFSET (where to start in the index) and the LIMIT (how many to look at, starting from the OFFSET value. --Tagishsimon (talk) 20:57, 22 February 2024 (UTC)
- Or you could run the whole thing in one go on Qlever - https://qlever.cs.uni-freiburg.de/wikidata/v6kNws - and get 369,123 rows --Tagishsimon (talk) 20:57, 22 February 2024 (UTC)