Shortcuts: WD:RAQ, w.wiki/LX

Wikidata:Request a query: Difference between revisions

From Wikidata
Jump to navigation Jump to search
Content deleted Content added
Quesotiotyo (talk | contribs)
Line 517: Line 517:
For this new year, I want to find if my wife's feeling about double family name is wright or wrong (she said that the mother family name always come first). For that, I need to find the humans with two family name and associate these with the father and the mother in the order given in the label. As an example, {{Q|109447298}} -> Taillefer -> {{P|25}} and -> Simard -> {{P|22}}.<br>For this, I think I need to use a REGEX in the label like <code>rdfs:label ?label . filter (regex(str(?label), ".*? (.*?)\-(.*?)"))</code>, but I don't know who to reuse/link the first <code>(.*?)</code> to {{P|25}} and the second to {{P|22}}. [[User:Simon Villeneuve|Simon Villeneuve]] ([[User talk:Simon Villeneuve|<span class="signature-talk">{{int:Talkpagelinktext}}</span>]]) 14:01, 1 January 2022 (UTC)
For this new year, I want to find if my wife's feeling about double family name is wright or wrong (she said that the mother family name always come first). For that, I need to find the humans with two family name and associate these with the father and the mother in the order given in the label. As an example, {{Q|109447298}} -> Taillefer -> {{P|25}} and -> Simard -> {{P|22}}.<br>For this, I think I need to use a REGEX in the label like <code>rdfs:label ?label . filter (regex(str(?label), ".*? (.*?)\-(.*?)"))</code>, but I don't know who to reuse/link the first <code>(.*?)</code> to {{P|25}} and the second to {{P|22}}. [[User:Simon Villeneuve|Simon Villeneuve]] ([[User talk:Simon Villeneuve|<span class="signature-talk">{{int:Talkpagelinktext}}</span>]]) 14:01, 1 January 2022 (UTC)
: I wouldn't try to analyze labels. You cannot really know what they contain, and text processing and regexs are slow in SPARQL. So I would instead look for {{P|P734}} and the qualifier {{P|P1545}}. --[[User:Dipsacus fullonum|Dipsacus fullonum]] ([[User talk:Dipsacus fullonum|<span class="signature-talk">{{int:Talkpagelinktext}}</span>]]) 18:14, 1 January 2022 (UTC)
: I wouldn't try to analyze labels. You cannot really know what they contain, and text processing and regexs are slow in SPARQL. So I would instead look for {{P|P734}} and the qualifier {{P|P1545}}. --[[User:Dipsacus fullonum|Dipsacus fullonum]] ([[User talk:Dipsacus fullonum|<span class="signature-talk">{{int:Talkpagelinktext}}</span>]]) 18:14, 1 January 2022 (UTC)
: This query narrowly avoids timing out and currently returns 132 people with a label containing [mother's family name][hyphen][father's family name]. Inverting the order and re-running the query gives 113 people with the father's name first.
: {{SPARQL|query=SELECT ?person ?personLabel ?mother_family_nameLabel ?father_family_nameLabel

WITH {
SELECT ?person ?mother_family_name ?father_family_name WHERE {
?person wdt:P25 [wdt:P734 ?mother_family_name] ;
wdt:P22 [wdt:P734 ?father_family_name] .
FILTER(?mother_family_name != ?father_family_name)
}
} AS %results1

WITH {
SELECT ?person ?personLabel ?mother_family_nameLabel ?father_family_name WHERE {
INCLUDE %results1.
SERVICE wikibase:label {
bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" .
?person rdfs:label ?personLabel .
?mother_family_name rdfs:label ?mother_family_nameLabel .
}
FILTER(CONTAINS(?personLabel, CONCAT(?mother_family_nameLabel, "-")))
}
} AS %results2

WHERE {
INCLUDE %results2.
SERVICE wikibase:label {
bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" .
?father_family_name rdfs:label ?father_family_nameLabel .
}
FILTER(CONTAINS(?personLabel, CONCAT("-", ?father_family_nameLabel)))
} ORDER BY ASC(?mother_family_nameLabel) ASC(?father_family_nameLabel) ASC(?personLabel)}}
:Some double family names are written without a hyphen, but these are difficult to differentiate from the common practice (in English names at least) of a mother's maiden name being given as a middle name. It was easiest to focus only on hyphenated names to avoid false positives. Several may still appear though, on the rare occasion where a woman marries a man with the same family name as her mother's maiden name, making it impossible to discern from the label alone whether she had a hyphenated name from birth or as a result of marriage.
:I hope that this helps answer your question.
:--[[User:Quesotiotyo|Quesotiotyo]] ([[User talk:Quesotiotyo|<span class="signature-talk">{{int:Talkpagelinktext}}</span>]]) 00:57, 2 January 2022 (UTC)


== References containing only P813 ==
== References containing only P813 ==

Revision as of 00:57, 2 January 2022

Number of artists / cultural organisations from Québec

Hi! I am looking to find out how many artists (Q483501) or cultural institutions (Q3152824) / art organizations (Q7062022) from the province of Quebec (Q176) are listed actually on Wikidata (all disciplines combined). I have no clue as to where to start in creating a SPARQL query, but here are some properties I found that could be useful (I think), if someone (anyone) is willing to give me a hand. Thanks in advance for your help.

  • Wikidata property related to art (Q27918607)
  • Québec (Q176)
  • Artists (Q483501)
  • Cultural institution (Q3152824)
  • not-for-profit arts organization (Q7062022)
  • nonprofit organization (Q163740)
  • field of this occupation : Art (Q735)
  • sub-class of : Art (Q2018526)
  • part of / field of work : Culture (Q11042)
  • has quality of : art genre (Q1792379) , art style (Q1792644)
  • Catégorie:Arts (Q4104783)
  • Catégorie:Association ou organisme culturel au Québec (Q49656487)
  • Catégorie:Association ou organisme culturel (Q8809115)

Query about museum's collection

Hello everyone! I'm looking for a query that can identify all the elements of a wikimedia commons category or a museum collection (that can also capture data such as upload date, license, etc)!

Query returning pages with non-empty talk pages

Hi,

I am looking for an efficient way to list items with non-empty talk pages (generic).

I am mainly interested in taxa, so my request would more or less look like:

#title: Which are the taxa present in Wikidata which have been discussed? (example limited to 1000 results)

SELECT DISTINCT?taxon ?taxon_name WHERE {

  ?taxon wdt:P225 ?taxon_name.                   # get the taxon scientific name

  # IMPLEMENT STEP TO FILTER TAXA WITH DISCUSSION

}

LIMIT 1000
Which are the taxa present in Wikidata which have been discussed? (example limited to 1000 results)

AdrianoRutz (talk) 14:36, 15 December 2021 (UTC)[reply]

There is no information about talk pages in WDQS. You could make a query get a list of all item talk pages by using the MWAPI service and then check the corresponding item pages, but the MWAPI service has a hard limit of max 10,000 results so the list will be incomplete. There may be other tools which are better this task. --Dipsacus fullonum (talk) 15:13, 15 December 2021 (UTC)[reply]
@Dipsacus fullonum Thank you, I don't care if the list is incomplete, would be great already. How would you do it through MWAPI? AdrianoRutz (talk) 15:29, 15 December 2021 (UTC)[reply]
@AdrianoRutz: Here you are. The MWAPI call fetches 10,000 out of 35.000+ talk pages so most results are missing:
SELECT ?taxon ?taxon_name
WHERE
{
  OPTIONAL # Optional is used as means to have this block executed first
  {
    SERVICE wikibase:mwapi
    {
      bd:serviceParam wikibase:api "Generator" .
      bd:serviceParam wikibase:endpoint "www.wikidata.org" .
      bd:serviceParam mwapi:generator "allpages" .
      bd:serviceParam mwapi:gapnamespace "1" .
      bd:serviceParam mwapi:gaplimit "max" .
      ?talk wikibase:apiOutput mwapi:title  .
    }
    BIND(URI(CONCAT("http://www.wikidata.org/entity/", SUBSTR(?talk, 6))) AS ?taxon)
  }
  ?taxon wdt:P225 ?taxon_name .  
}
Try it!
--Dipsacus fullonum (talk) 16:23, 15 December 2021 (UTC)[reply]
Exactly what I was looking for! Thank you very much! AdrianoRutz (talk) 16:33, 15 December 2021 (UTC)[reply]
@AdrianoRutz, Dipsacus fullonum: this is usually a job for Petscan. The issue here is that "any item with P225" (3.5M items) is a bit excessive, so it does not work in this particular case.
However, one can query as in petscan:20892129 (Talk pages of items that have position held (P39): Federal Chancellor of Germany (Q4970706)). Existing talk pages have a "Page ID" in the results set, non-existing ones do not. —MisterSynergy (talk) 17:07, 15 December 2021 (UTC)[reply]
@MisterSynergy Oh, very nice indeed! Added some lines to limit to some taxa only and works like a charm! Thank you AdrianoRutz (talk) 12:10, 17 December 2021 (UTC)[reply]

Complementary spouses

Made a statement to find spouses that do not have a complementary spouse statement. It works for the most part, but I'm puzzled why it lists spouses that does have a complementary spouse statement, although with qualifiers and jazz. What's going on here?

PS. I already added the missing spouse statements, meaning this query is supposed to return no items now.

SELECT DISTINCT ?item ?itemLabel ?spouse ?spouseLabel
WHERE {
  ?item wdt:P31 wd:Q5;
        wdt:P27 wd:Q20;
        wdt:P26 ?spouse.
  MINUS { ?spouse wdt:P26 ?item }
  FILTER(!wikibase:isSomeValue(?spouse))
  SERVICE wikibase:label { bd:serviceParam wikibase:language "nb,en". }
}
ORDER BY (?item)
Try it!

--Infrastruktur (T | C) 12:23, 19 December 2021 (UTC)[reply]

wdt:P26 just gets you "best" rank (preferred rank if there is, otherwise normal rank).
try p:P26/ps:P26 instead --- Jura 12:36, 19 December 2021 (UTC)[reply]

All places in the Great Catalan Encyclopedia that are not part of the Catalan Countries

Hello! I want to search for all the items in the Great Catalan Encyclopedia that are not part or their cultural area. The query is done, but it seems heavy it can't be run. Is there a way to make it more economical?

SELECT (COUNT(?item) as ?count) ?herrialdeaLabel WHERE {
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
  ?item wdt:P17 ?herrialdea.
  ?item wdt:P1296 ?cat
  MINUS{?item wdt:P131*/wdt:P706* wd:Q5765.} 
  MINUS{?item wdt:P131*/wdt:P706* wd:Q5705.} 
  MINUS {?item wdt:P131*/wdt:P706* wd:Q5720.} 
  MINUS {?item wdt:P131*/wdt:P706* wd:Q228.} 
  MINUS {?item wdt:P131*/wdt:P706* wd:Q166282.} 
  MINUS {?item wdt:P131*/wdt:P706* wd:Q15580.}
}
GROUP by ?herrialdeaLabel
ORDER by DESC(?count)
Try it!

Thanks! -Theklan (talk) 21:02, 20 December 2021 (UTC)[reply]

@Theklan: I only suspect that wdt:P131/wdt:P706* is probably what you need, rather than wdt:P131*/wdt:P706* ... I could be wrong. This takes runtime to about 38 seconds.
SELECT (COUNT(?item) as ?count) ?herrialdeaLabel WHERE {
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
  ?item wdt:P1296 ?cat . hint:Prior hint:runFirst true.
  ?item wdt:P17 ?herrialdea .
  MINUS{?item wdt:P131/wdt:P706* wd:Q5765. hint:Prior hint:gearing "forward" . } 
  MINUS{?item wdt:P131/wdt:P706* wd:Q5705. hint:Prior hint:gearing "forward" . } 
  MINUS {?item wdt:P131/wdt:P706* wd:Q5720. hint:Prior hint:gearing "forward" . } 
  MINUS {?item wdt:P131/wdt:P706* wd:Q228. hint:Prior hint:gearing "forward" . } 
  MINUS {?item wdt:P131/wdt:P706* wd:Q166282. hint:Prior hint:gearing "forward" . } 
  MINUS {?item wdt:P131/wdt:P706* wd:Q15580. hint:Prior hint:gearing "forward" . }
}
GROUP by ?herrialdeaLabel
ORDER by DESC(?count)
Try it!
--Tagishsimon (talk) 21:53, 20 December 2021 (UTC)[reply]
It got out of time, too. Actually, I think that only with P131* would work also. I'll give a try to that. Theklan (talk) 22:02, 20 December 2021 (UTC)[reply]
Oh! It worked (59 secs) in the secont try! Thanks! Theklan (talk) 22:02, 20 December 2021 (UTC)[reply]

Help:Ranking had an empty section on this. I filled it with a few sample queries. Please expand/correct as it may seem useful to you. --- Jura 00:19, 21 December 2021 (UTC)[reply]

Nice. I'm curious; efficiency-wise how does your 'not deprecated' query fare against something like this given a big working set?
SELECT * WHERE {
  VALUES ?truthy {
    wikibase:PreferredRank
    wikibase:NormalRank
  }
  wd:Q692 p:P569 ?st.
  ?st ps:P569 ?value.
  ?st wikibase:rank ?truthy. # <- more or less efficient?
  #MINUS { ?st wikibase:rank wikibase:DeprecatedRank. }
}
Try it!

--Infrastruktur (talk) 12:51, 21 December 2021 (UTC)[reply]

Also, there's no explanation about what "a" is. I've been using "?st rdf:type wikibase:BestRank." to test for best rank. --Infrastruktur (talk) 12:59, 21 December 2021 (UTC)[reply]
Thanks for your feedback.
Using values is probably more efficient. I had been wondering if I should add that too.
I would be interested in a good explanation about "a" too. --- Jura 13:12, 21 December 2021 (UTC)[reply]
You can see in the SPARQL 1.1 Query Language definition at https://www.w3.org/TR/sparql11-query/ section 4.2.4 that a is a case-sensitive keyword that can be used as a predicate instead of the IRI http://www.w3.org/1999/02/22-rdf-syntax-ns#type (which is normally shortened using the rdf: prefix til rdf:type). So in other words "a" is an alternative spelling for "rdf:type". It makes no difference which is used. --Dipsacus fullonum (talk) 22:25, 21 December 2021 (UTC)[reply]
  • Infrastruktur had added it to Help:Ranking in the meantime. Thanks to both of you. Here a query to find its values (Without the main usecase: no value statements).
SELECT *
WITH
{
  SELECT DISTINCT ?rdftype (COUNT(*) as ?count)
  WHERE
  {
    _:b2 rdf:type ?rdftype
  }
  GROUP BY ?rdftype
} as %types
WHERE
{
  INCLUDE %types
  MINUS { _:b7 wikibase:novalue ?rdftype }
}
ORDER BY DESC(?count)
Try it!

Wonder if the 80 million schema:Article triples have any use. --- Jura 13:03, 22 December 2021 (UTC)[reply]

Added that to Wikidata:Report_a_technical_problem/WDQS_and_Search#delete_triples_from_WDQS:_?a_rdf:type_schema:Article. --- Jura 20:08, 22 December 2021 (UTC)[reply]

This forum is a neverending source of inspiration and knowledge, a warm thanks to all its contributors. <3 --Infrastruktur (talk) 13:30, 23 December 2021 (UTC)[reply]

Searching only one-way railway stations

Hello, given that first query, https://w.wiki/4avz , how to add a filter that would count adjacent station (P197) and filter out those that have more than one P197 ? Counting only one P197 is not sufficient but would help filter. Eg Sheringham railway station (Q248831) would be a false positive (trains can go up and forth whilst I'm looking only stations where you can go from A then B then C, but not C then B then A). Bouzinac💬✒️💛 11:45, 22 December 2021 (UTC)[reply]

Filtering out those that have more than one P197:
SELECT ?item ?label ?_image WHERE {
  ?item wdt:P1103 +1. .
  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "fr,en,de" . 
    ?item rdfs:label ?label
  }
  ?item wdt:P197 ?adjacent_station .
  MINUS { ?item (wdt:P576|wdt:P582|wdt:P3999) ?dispar. }#on ne veut pas les stations disparues
  MINUS { ?item wdt:P5817|wdt:P5816 ?interdit.
    VALUES ?interdit {  wd:Q811683  wd:Q63065035 wd:Q12377751 wd:Q97317113 wd:Q55653430 wd:Q30108381 wd:Q55570340 wd:Q11639308 wd:Q104664889
    }
  }#on ne veut pas les cas particuliers, en construction etc
}
GROUP BY ?item ?label ?_image
HAVING (COUNT(?adjacent_station) = 1)
Try it!
Also filtering those where with a P197 from a adjacent station back to the first station:
SELECT ?item ?label ?_image WHERE {
  ?item wdt:P1103 +1. .
  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "fr,en,de" . 
    ?item rdfs:label ?label
  }
  ?item wdt:P197 ?adjacent_station .
  MINUS { ?adjacent_station wdt:P197 ?item. }
  MINUS { ?item (wdt:P576|wdt:P582|wdt:P3999) ?dispar. }#on ne veut pas les stations disparues
  MINUS { ?item wdt:P5817|wdt:P5816 ?interdit.
    VALUES ?interdit {  wd:Q811683  wd:Q63065035 wd:Q12377751 wd:Q97317113 wd:Q55653430 wd:Q30108381 wd:Q55570340 wd:Q11639308 wd:Q104664889
    }
  }#on ne veut pas les cas particuliers, en construction etc
}
GROUP BY ?item ?label ?_image
HAVING (COUNT(?adjacent_station) = 1)
Try it!
--Dipsacus fullonum (talk) 15:28, 22 December 2021 (UTC)[reply]

Metadata about every human in English Wikipedia

Hi everyone. I am new here so please if I am doing something wrong.

As far as I can know, there are around 1.800.000 biographies of people in the English Wikipedia (355.000 women, 1.500.000 men and 1.500 with other gender or no-gender).

What I want is some metadata of every biography/Person(P31:Q5) (item, itemLabel, dateOfBirth, dateOfDeath, gender, occupation) in the English Wikipedia. I have been writing some SPARQL query but it has not been possible for me.

Also, I do not know (because I failed in the first step) if the timeout could be a problem if I want to extract data from 1.800.000 entries.

Thanks in advance.

Hi. I don't think that it is possible using WDQS to make a query returning a result for every biography in the English Wikipedia in under 60 seconds which is the limit for timeout. It may be possible to make several queries each giving a subset of the result, and then combine the subsets afterwards. --Dipsacus fullonum (talk) 14:55, 23 December 2021 (UTC)[reply]
Thank you so much for your fast answer.
As you mentioned, the limit for timeout it is 60 seconds. Because of this I am wondering: Is there any REST API (or something similar) that can solve this timeout 'limitation'? I have seen two tools (WikidataQueryServiceR and WikidataR, both in R) but they use the WDQS, so I imagine that they both have similar timeout limitation.
Again, thank you in advance and also thank you for your previous answer. 85.56.131.251 15:45, 23 December 2021 (UTC)[reply]

Basics:

Dates

Places

Activities

Other

Languages:

Names:

Images:

For deceased persons:

It may be possible to do similar ones for English Wikipedia. Maybe a bot could generate them one by one for several wikis. --- Jura 16:52, 23 December 2021 (UTC)[reply]

list of all games on wikidata

Hello, I am trying to create a dataset with all the videos games on wikidata but I am stuck. My query returns only 3 results, Can someone help? I am new to SPARQL and confused about what I am doing wrong here.

SELECT DISTINCT ?GameLabel ?GenreLabel ?CountryLabel ?PublicationDateLabel ?DeveloperLabel ?SoftwareEngineLabel ?GameModeLabel ?BusinessModeLabel ?AgeRatingLabel ?UsesLabel
where{
   ?Game wdt:P279 wd:Q7889.
   ?Game wdt:P136 ?Genre.
  ?Game wdt:P495 ?Country. 
  optional {
  ?Game wdt:P577 ?PublicationDate. 
  ?Game wdt:P178 ?Developer. 
  ?Game wdt:P408 ?SoftwareEngine. 
  ?Game wdt:P404 ?GameMode. 
  ?Game wdt:P7936 ?BusinessMode. 
  ?Game wdt:P9897 ?AgeRating. 
  ?Game wdt:P2283 ?Uses.
    }
  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "en" .
  }
}
Try it!

Thank you,  – The preceding unsigned comment was added by ? (talk • contribs).

Hi. I see at least 2 problems with the query:
  1. ?Game wdt:P279 wd:Q7889. Here you asks for subclasses of videogames. I think you mean to ask for instances of (P31) video game or a subclass of video game. You do that with ?Game wdt:P31 / wdt:P279* wd:Q7889. or (same meaning, but faster) wd:Q7889 ^wdt:P279* / ^wdt:P31 ?Game . (The * after wdt:P279 in both variants means that wdt:P279 is invoked zero or more times with no upper limit.)
  2. The optional block contains 7 triples. All of these must be present for the block to be included in the results. So if an item has values for only 1 to 6 of the variables ?PublicationDate, ?Developer, ?SoftwareEngine, ?GameMode, ?BusinessMode, ?AgeRating and ?Uses, none of them will be in the results. If you want any results even if all not is present, you have to split it up into 7 independent optional blocks. --Dipsacus fullonum (talk) 14:51, 23 December 2021 (UTC)[reply]
--Dipsacus fullonum (talk) 14:51, 23 December 2021 (UTC)[reply]

Items whose article in Wikipedia contain a given string

Suppose I have a list of QIDs and I want to know which of these items have an article in English Wikipedia whose body contain the string red banana. The following is a minimal working example I've written for you to test your possible solution

SELECT ?item {
  VALUES ?item {
    # Group 1: The articles in en.wikipedia.org of these items
    # contain the string "red banana"
    wd:Q164934
    wd:Q490941
    wd:Q4927783
    # Group 2: The articles in en.wikipedia.org of these items
    # don't contain the string "red banana"
    wd:Q12876
    wd:Q249091
    wd:Q1233773
  }

  # TODO: Filter out items whose article in en.wikipedia.org doesn't
  # contain "red banana". That is, the query should show the items
  # from Group 1
}
Try it!

In order to avoid the XY problem, here's what I'm trying to do: I want to list all peruvian female humans whose article in Spanish Wikipedia contain the string "esposa de" because I want to know how many females are defined by mentioning their spouse. This because someone in Wikimedia Peru shared a guide on gender bias on Wikipedia and we started wondering how we could get a list of peruvian female humans that are potentially defined by mentioning their husband. We have already written a query for getting the list of peruvian female humans, but we don't know how to narrow the results to those that contain the string esposa de. Any help is appreciated

Rdrg109 (talk) 17:27, 25 December 2021 (UTC) (please ping on reply)[reply]

@Rdrg109: You can search Wikipedia articles for certain words by using the search function in the Wikipedia. The search https://es.wikipedia.org/w/index.php?search=%22esposa+de%22&ns0=1 will thus list all articles with the string "esposa de" in the Spanish Wikipedia. But it is hard to combine that search with a Wikidata query because there is more than 10.000 results (16.707 results to be exact). It is not so difficult to embed a Wikipedia search in a query using the MWAPI service (see https://www.mediawiki.org/wiki/Wikidata_Query_Service/User_Manual/MWAPI), but the problem here is that MWAPI has a hard limit of returning max 10,000 results. But if you change the search to also only include articles in the category es:Categoría:Mujeres (https://es.wikipedia.org/w/index.php?search=%22esposa+de%22+incategory%3AMujeres&ns0=1) you can limit the number of search results to under 3,000 which is manageable. The category Mujeres is normally set by es:Plantilla:NF which uses Wikidata. So here is a query to give articles in Spanish Wikipedia about Peruvian females with the string "esposa de" and placed in category "Mujeres". That is as close as I can get it.
SELECT ?item ?title (URI(CONCAT("https://es.wikipedia.org/wiki/", ?title)) AS ?article)
WITH
{
  SELECT DISTINCT ?item
  WHERE
  {
    { ?item wdt:P27 wd:Q419 . }
    UNION
    { wd:Q419 ^wdt:P131* / ^wdt:P19 ?item . }
    UNION
    { ?item wdt:P1532 wd:Q419 . }

    ?item wdt:P31 wd:Q5 .
    ?item wdt:P21 wd:Q6581072 .
  }
} AS %Peruvian_females
WHERE
{
  OPTIONAL # OPTIONAL is used to force this block to be evaluated first 
  {
    SERVICE wikibase:mwapi
    {
      bd:serviceParam wikibase:endpoint "es.wikipedia.org" .
      bd:serviceParam wikibase:api "Generator" .
      bd:serviceParam mwapi:generator "search" .
      bd:serviceParam mwapi:gsrsearch '"esposa de" incategory:Mujeres' .
      bd:serviceParam mwapi:gsrlimit "max" .
      ?title wikibase:apiOutput mwapi:title .
      ?item wikibase:apiOutputItem mwapi:item .
    }
    FILTER BOUND(?item)
  }
  INCLUDE %Peruvian_females
}
Try it!
--Dipsacus fullonum (talk) 10:17, 31 December 2021 (UTC)[reply]
@Rdrg109:. I have edited the query above because the "*" after wdt:P131 was missing in first version giving too few results. The change meant that I also had to restructure the query to avoid timeout. --Dipsacus fullonum (talk) 12:47, 31 December 2021 (UTC)[reply]

pubmed from a given list of QIDs

Happy Boxing Day for those who celebrate such things. I have a list of QIds. Can I get a query that returns a list that includes the QId and the PubMed publication ID (P698)? Here is a start of my list of QIDs:

  • Q27938560
  • Q27939622
  • Q28508930
  • Q28566968
  • Q28567875
  • Q28584294

Thanks very much (in advance). Trilotat (talk) 16:18, 26 December 2021 (UTC)[reply]

@Trilotat:
SELECT ?item ?itemLabel ?PubMed_ID
WHERE 
{
  VALUES ?item {
    wd:Q27938560
    wd:Q27939622
    wd:Q28508930
    wd:Q28566968
    wd:Q28567875
    wd:Q28584294
    }
  OPTIONAL {?item wdt:P698 ?PubMed_ID . } 
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } 
}
Try it!
--Tagishsimon (talk) 17:21, 26 December 2021 (UTC)[reply]
Thank you so much!! Trilotat (talk) 18:49, 26 December 2021 (UTC)[reply]

Homonyms inside same subway

Hello, I came across Western (Q3567470), Western (Q3567480), Western (Q3567469), etc and I wondered how to find subways stations that share exactly same name inside same subway network? Bouzinac💬✒️💛 17:24, 30 December 2021 (UTC)[reply]

@Bouzinac: Is this what you were looking for?
SELECT ?transitsystemLabel ?stationLabel ?count ?stations WHERE {
{
  SELECT DISTINCT ?transitsystem ?transitsystemLabel ?stationLabel (COUNT(DISTINCT ?station) AS ?count) (GROUP_CONCAT(DISTINCT ?station) AS ?stations) WHERE {
    wd:Q5503 ^wdt:P279*/^wdt:P31 ?transitsystem .
    ?transitsystem ^wdt:P361 ?station .
    ?station wdt:P31/wdt:P279* wd:Q12819564 .
    SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
  } GROUP BY ?transitsystem ?transitsystemLabel ?stationLabel
}
  FILTER(?count > 1)
} ORDER BY DESC (?count) ASC (?transitsystemLabel) ASC (?stationLabel)
Try it!
--Quesotiotyo (talk) 05:08, 31 December 2021 (UTC)[reply]
Yes, something like that and appears that 86th Street would beat Western in terms of homonyms (but Western looks more strange to me since there is two Western on the same subway line). I've slightly modified your query this way https://w.wiki/4csT
That query functions provided en label is correct among items... Bouzinac💬✒️💛 09:01, 31 December 2021 (UTC)[reply]

Days with no photos

Please can we have a query for instances of calendar day of a given year (Q47150325), later than, say, 1900, with no image?

@Pigsonthewing: This sort of thing.
SELECT ?item ?itemLabel ?date
WHERE 
{
  ?item wdt:P31 wd:Q47150325. 
  ?item wdt:P585 ?date . hint:Prior hint:rangeSafe true.
  filter(xsd:integer(year(?date)) >  1900 && xsd:integer(year(?date)) <= 2021 )
  filter not exists {?item wdt:P18 [] . }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } 
} order by desc(?date)
Try it!
--Tagishsimon (talk) 19:25, 30 December 2021 (UTC)[reply]
@Tagishsimon: Many thanks: 43491 (!) results, so maybe we need to make if for a given year, or decade? Also need to exclude future dates, please (it's bearable now, but would be a pain on the 1st January). Andy Mabbett (Pigsonthewing); Talk to Andy; Andy's edits 19:40, 30 December 2021 (UTC)[reply]
@Pigsonthewing: minimal fiddling with the filter will get you that. now() is also your friend.
SELECT ?item ?itemLabel ?date
WHERE 
{
  ?item wdt:P31 wd:Q47150325. 
  ?item wdt:P585 ?date . hint:Prior hint:rangeSafe true.
  filter(xsd:integer(year(?date)) =  2021 && ?date <= now() )
  filter not exists {?item wdt:P18 [] . }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } 
} order by desc(?date)
Try it!
--Tagishsimon (talk) 20:00, 30 December 2021 (UTC)[reply]
@Tagishsimon: Just the job. Splendid work, thank you. Andy Mabbett (Pigsonthewing); Talk to Andy; Andy's edits 22:15, 30 December 2021 (UTC)[reply]
and only 745 with ... --Infovarius (talk) 22:33, 30 December 2021 (UTC)[reply]
Comparing date values directly is much more effective than extracting years and then compare integers:
SELECT ?item ?itemLabel ?date
WHERE 
{
  ?item wdt:P31 wd:Q47150325. 
  ?item wdt:P585 ?date . hint:Prior hint:rangeSafe true.
  filter("2021-01-01"^^xsd:dateTime <= ?date && ?date <= now() )
  filter not exists {?item wdt:P18 [] . }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } 
} order by desc(?date)
Try it!
--Dipsacus fullonum (talk) 23:22, 30 December 2021 (UTC)[reply]
PS. This version is much faster because of the rangeSafe hint and the indexing of the ?date values. The engine can directly select the dates that satisfies the date range filter without testing all values as is necessary in the version using the YEAR function. --Dipsacus fullonum (talk) 23:32, 30 December 2021 (UTC)[reply]

Politicians by country (USA; UK; CAN; IN) in English and Hindi Wikipedias

Hello, I am looking to compare the number of articles on politicians from four countries (USA; UK; Canada; India) on two different Wikipedias (English and Hindi). I would be forever grateful for queries that would help me get these numbers!

  • American (Q30) politicians (Q82955) on en.wikipedia.org
  • British (Q145) politicians on en.wikipedia.org
  • Canadian (Q16) politicians on en.wikipedia.org
  • Indian (Q668) politicians on en.wikipedia.org
  • American politicians on hi.wikipedia.org
  • Canadian politicians on hi.wikipedia.org
  • Indian politicians on en.wikipedia.org

Thanks! Seeris (talk)

@Seeris: By the looks of it you'll have to run this query four times, swapping in the relevant country on the indicated line. I can't get a single query to work without timeout.
SELECT ?CoC (count(?article) as ?hi_count) (count(?article2) as ?en_count)  WHERE {
  ?item wdt:P106 wd:Q82955.
  ?item wdt:P27 ?CoC .
  VALUES ?CoC {wd:Q30}   # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 
  OPTIONAL { ?article schema:about ?item ;
                      schema:isPartOf <https://hi.wikipedia.org/> . }
  OPTIONAL { ?article2 schema:about ?item ;              
                       schema:isPartOf <https://en.wikipedia.org/> . }
} group by ?CoC
Try it!
--Tagishsimon (talk) 01:52, 31 December 2021 (UTC)[reply]
Marvelous! Thanks a lot @Tagishsimon:. Here's my blurb about the results:
If we count the number of articles that exist about politicians by nationality, we find that in the English Wikipedia there are 61,800 articles on Americans, 15,000 on Canadians, 12,000 on Indians, and 10,200 on Britons; this represents a disproportionate number of Americans, but also slightly more Canadians than Indians (while India's population is significantly higher than Canada's), and surprisingly, a third more Canadians than Britons (while en.wikipedia is also said to be biased in favor of the British). This ratio is reversed in the Hindi Wikipedia: it has 4,257 articles on Indians, 141 on Americans, 105 on British, and 21 on Canadians; that is, a disproportionate number of Indians, and an almost two hundred-fold gap between Indians and Canadians (whereas it is only four times as large between Americans and Canadians in the English Wikipedia). Conclusion: ethnocentrism seems as prevalent on the Hindi Wikipedia as on the English one! Seeris (talk)

Indigenous (Canadian) humans in a given Wikipedia

Hi again - how to count how many biographies (articles about humans) there are in a given Wikipedia (ex. English) about indigenous (aboriginal "Indians") who are also Canadian citizens? I am not sure if there is a general "Indigenous" element that we could use? Additionaly, I'd like to count the total number of articles about humans in a given Wikipedia. Thanks in advance! Seeris (talk)

@Seeris: I have no idea how to know who is indigenous (or even how that is defined). You can count the number of articles about humans in a Wikipedia with this query:
SELECT (COUNT(?article) AS ?biographies)
WHERE
{
  ?item wdt:P31 wd:Q5 .
  ?article schema:about ?item .
  ?article schema:isPartOf <https://hi.wikipedia.org/> .
}
Try it!
It may timeout for the biggest Wikipedias. --Dipsacus fullonum (talk) 18:34, 1 January 2022 (UTC)[reply]

Two family name

Happy new year everyone !

For this new year, I want to find if my wife's feeling about double family name is wright or wrong (she said that the mother family name always come first). For that, I need to find the humans with two family name and associate these with the father and the mother in the order given in the label. As an example, Rosalie Taillefer-Simard (Q109447298) -> Taillefer -> mother (P25) and -> Simard -> father (P22).
For this, I think I need to use a REGEX in the label like rdfs:label ?label . filter (regex(str(?label), ".*? (.*?)\-(.*?)")), but I don't know who to reuse/link the first (.*?) to mother (P25) and the second to father (P22). Simon Villeneuve (talk) 14:01, 1 January 2022 (UTC)[reply]

I wouldn't try to analyze labels. You cannot really know what they contain, and text processing and regexs are slow in SPARQL. So I would instead look for family name (P734) and the qualifier series ordinal (P1545). --Dipsacus fullonum (talk) 18:14, 1 January 2022 (UTC)[reply]
This query narrowly avoids timing out and currently returns 132 people with a label containing [mother's family name][hyphen][father's family name]. Inverting the order and re-running the query gives 113 people with the father's name first.
SELECT ?person ?personLabel ?mother_family_nameLabel ?father_family_nameLabel

WITH {
  SELECT ?person ?mother_family_name ?father_family_name WHERE {
  ?person wdt:P25 [wdt:P734 ?mother_family_name] ;
          wdt:P22 [wdt:P734 ?father_family_name] .
  FILTER(?mother_family_name != ?father_family_name)
}
} AS %results1 

WITH {
  SELECT ?person ?personLabel ?mother_family_nameLabel ?father_family_name WHERE {
  INCLUDE %results1.
  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" . 
    ?person rdfs:label ?personLabel .
    ?mother_family_name rdfs:label ?mother_family_nameLabel .
  }
  FILTER(CONTAINS(?personLabel, CONCAT(?mother_family_nameLabel, "-")))
}
} AS %results2 

WHERE {
  INCLUDE %results2.
  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" . 
    ?father_family_name rdfs:label ?father_family_nameLabel .
  }
  FILTER(CONTAINS(?personLabel, CONCAT("-", ?father_family_nameLabel)))
} ORDER BY ASC(?mother_family_nameLabel) ASC(?father_family_nameLabel) ASC(?personLabel)
Try it!
Some double family names are written without a hyphen, but these are difficult to differentiate from the common practice (in English names at least) of a mother's maiden name being given as a middle name. It was easiest to focus only on hyphenated names to avoid false positives. Several may still appear though, on the rare occasion where a woman marries a man with the same family name as her mother's maiden name, making it impossible to discern from the label alone whether she had a hyphenated name from birth or as a result of marriage.
I hope that this helps answer your question.
--Quesotiotyo (talk) 00:57, 2 January 2022 (UTC)[reply]

References containing only P813

Hi! Given a group of items (let's say, e.g., items containing the identifier Angelicum ID (P5731)), which is the best way to obtain of all the items in this group which have at least one statement having a reference containing only retrieved (P813) and no other properties? Thanks, --Epìdosis 16:49, 1 January 2022 (UTC)[reply]

@Epìdosis: could you clarify your specification, pls. If "one statement having a reference containing only retrieved (P813) and no other properties" (which I take to be, no other statements) then we're looking for an item that has 1 or 2 statements total (i.e. a Angelicum ID (P5731) with a retrieved (P813) ref, or a P5731 and one other statements with a P813 ref) ... but for P5731 items, the smallest statement count is 4 and so we'd get zero results. Sorry to be pedantic and/or sorry that I've not understood. --Tagishsimon (talk) 17:27, 1 January 2022 (UTC)[reply]
@Epìdosis: This query if I understood request correctly:
SELECT ?item ?Angelicum_ID ?prop ?statement ?reference ?retrieved
WHERE
{
  ?item wdt:P5731 ?Angelicum_ID .
  ?item ?prop ?statement .
  ?statement prov:wasDerivedFrom ?reference .
  ?reference pr:P813 ?retrieved .
  ?reference ?refprop ?refvalue .
}
GROUP BY ?item ?Angelicum_ID ?prop ?statement ?reference ?retrieved
HAVING (COUNT(?refprop) = 2) # Having count = 2 because there will minimum also be a prv:P813 value
Try it!
--Dipsacus fullonum (talk) 17:36, 1 January 2022 (UTC)[reply]
@Dipsacus fullonum: Yes, you understood correctly! Thanks also to @Tagishsimon:, of course; I meant items where there are references which contain in themselves only P813, so wrong in most cases and to be corrected. --Epìdosis 17:54, 1 January 2022 (UTC)[reply]
(ec) maybe this:
SELECT ?item ?id ?property ?statement ?retrieved
WHERE
{
  # VALUES ?item { wd:Q107331778 }
  hint:Query hint:optimizer "None".
  ?item wdt:P5731 ?id .
  ?item ?property ?statement . 
  ?property  ^ wikibase:claim [] .
  ?statement prov:wasDerivedFrom ?b .
  ?b pr:P813 ?retrieved . 
  FILTER NOT EXISTS { ?b ?other [] . FILTER( ?other != pr:P813 && ?other != prv:P813 ) }
}
LIMIT 100

Try it!

It should find P735 on Donald W. Dayton (Q107331778). Happy New Year! --- Jura 17:38, 1 January 2022 (UTC)[reply]

@Jura1: Yes, it works perfectly! Happy 2022, --Epìdosis 17:54, 1 January 2022 (UTC)[reply]