Shortcut: WD:RAQ

Wikidata:Request a query

From Wikidata
Jump to navigation Jump to search

Request a query
Fishing in the Wikidata river requires both an idea where to look for fish and a suitable fishing method. If you have the former, this page can help you find the latter.

This is a page where SPARQL 1.1 Query Language (Q32146616) queries can be requested. Please provide feedback if a query is written for you.

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 2024/08.

Slide show with images

I am trying to make a slideshow with automatic looping over the a set of images returned from a WDQS query. I have made some progress on a page that loops via CCS and Javascript. I was wondering if there is a simpler way. For instance, with SAMPLE and HTML meta refresh. This will not work:

#defaultView:ImageGrid
SELECT (SAMPLE(DISTINCT ?image) AS ?image) WHERE {
  ?image ^wdt:P18 / wdt:P50 / (wdt:P108| wdt:P463 | wdt:P1416/wdt:P361*) wd:Q24283660 .
}
Try it!

all paintings that have a movement property OR of which the creator has a movement property

Hi everyone!

I'm creating a dataset of artworks with their genres/styles attached to them. I only get 7.4K hits of artworks that have an image and a style/genre associated with them, so I'm trying to expand the query. I want to include artworks with images that have a creator, which in turn has a movement property. That way this gives an indication of which style/genre the artwork has. I only don't know how to create an OR operator in SPARQL that will let me say 'give me all artworks with an image, and a movement property OR a creator with a movement property. Store that movement property in ?style.

Here's what I have so far:

SELECT ?item ?creator ?style ?image WHERE {
  ?item wdt:P31/wdt:P279* wd:Q838948 .
  ?item wdt:P170 ?creator .
  ?item wdt:P135 ?style .
  ?item wdt:P18 ?image .
  }
Try it!

CalvinBall (talk) 14:04, 27 July 2018 (UTC)[reply]

?item wdt:P170?/wdt:P135 ?style can be a possibility. Matěj Suchánek (talk) 16:57, 27 July 2018 (UTC)[reply]
@CalvinBall: I suspect you need a couple of queries (I've tried combining them, but get a timeout). The first returns styles for images that have a Movement. The second, using Matěj's approach, returns the styles of the Creators, where the image has no Movement. The second query (and indeed the first) will give duplicate rows for images where the creator has more than one Movement (or ditto the image). fwiw, UNION is and/or in SPARQL, so did you want to look for this or that, you'd combine them in the style {?item wdt:P1 wd:Q1.} UNION {?item wdt:P2 wd:Q2.}
SELECT distinct ?item ?creator ?style ?image WHERE {
  ?item wdt:P31/wdt:P279* wd:Q838948 .
  ?item wdt:P170 ?creator .
  ?item wdt:P135 ?style .
  ?item wdt:P18 ?image .
  }
Try it!
SELECT distinct ?item ?creator ?style where {
  ?item wdt:P31/wdt:P279* wd:Q838948 .
  ?item wdt:P170 ?creator .
  filter not exists {?item wdt:P135 ?style .}
  ?item wdt:P170?/wdt:P135 ?style.
  ?item wdt:P18 ?image .
  }
Try it!
--Tagishsimon (talk) 20:17, 27 July 2018 (UTC)[reply]
Thank you @Matěj Suchánek: and @Tagishsimon: for your help! For posterity, I ended up concatenating the style variable to eliminate duplicate rows.
SELECT distinct ?item ?creator (group_concat(?style;separator=", ") as ?style) ?image WHERE {
  ?item wdt:P31/wdt:P279* wd:Q838948 .
  ?item wdt:P170 ?creator .
  ?item wdt:P135 ?style .
  ?item wdt:P18 ?image .
  } GROUP BY ?item ?creator ?image
Try it!

Finding an organisation by its website

This simple query:

SELECT ?item ?itemLabel
{
  ?item wdt:P856 "https://twitter.com" .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
Try it!

does not find X (Q918), as expected. What am I doing wrong? Andy Mabbett (Pigsonthewing); Talk to Andy; Andy's edits 15:50, 28 July 2018 (UTC)[reply]

@Pigsonthewing:
SELECT ?item ?itemLabel
{
  ?item wdt:P856 <https://twitter.com> .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
Try it!
--Tagishsimon (talk) 15:54, 28 July 2018 (UTC)[reply]
@Tagishsimon: Thank you. Is it possible to make it check for various forms (http & https; with/without "www"; with/without trailing slash, etc? Andy Mabbett (Pigsonthewing); Talk to Andy; Andy's edits 16:26, 28 July 2018 (UTC)[reply]
@Pigsonthewing: timeouts may be a problem ... this is much less efficient. If you do get timeouts, you can at least prove the method by uncommenting the first line.
SELECT ?item ?itemLabel
{
#  ?item wdt:P856 <https://twitter.com> .
  ?item wdt:P856 ?url.
  filter(contains(str(?url),"twitter")) .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
Try it!
--Tagishsimon (talk) 16:35, 28 July 2018 (UTC)[reply]

Different colors, different properties

Hi! I've created this query:

#defaultView:Map
SELECT ?item ?itemLabel ?itemDescription ?coord ?gmp ?gkg ?ind ?com ?layer
WHERE {
  ?item (wdt:P131)* wd:Q16245 . 
  MINUS { { ?item wdt:P31 wd:Q1134686 . } UNION { ?item wdt:P31 wd:Q747074 . } }
  ?item wdt:P625 ?coord . 
  OPTIONAL {?item wdt:P3749 ?gmp}
  OPTIONAL {?item wdt:P2671 ?gkg}
  OPTIONAL {?item wdt:P969 ?ind}
  OPTIONAL {?item wdt:P373 ?com}
  BIND(IF(BOUND(?gmp),'gmp exists','no gmp exists') AS ?layer)
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],it,en". }
}
Try it!

The color of the points depends only on P3749. However, it would be great to link it with two or more properties at the same time, i.e. P3749 and P373 (so: color 1: item has both P3749 and P373; color 2: item has only P3749; color 3: item has only P373; color 4: item has neither P3749 and P373). Is it possible? Thank you, --Epìdosis 16:10, 29 July 2018 (UTC)[reply]

@Epìdosis: Something like this, I think. bind(if(bound(?gmp) && bound(?com),"gmp & com exist",if(bound(?com),"com exists",if(bound(?gmp),"gmp exists","none"))) as ?layer) --Tagishsimon (talk) 20:28, 29 July 2018 (UTC)[reply]

I would like to have a list with all the items that contain a link to MusicBrainz. For example Q255 contains a link to MusicBrainz - Property:P434. Can anyone help me? Thanks. -- OneMusicDream (talk) 17:06, 29 July 2018 (UTC)[reply]

@OneMusicDream: Have you tried using some of the links (at the top right, or towards the bottom) of the information box in Property talk:P434? At least one of them does what you would like to do. Mahir256 (talk) 17:19, 29 July 2018 (UTC)[reply]
@Mahir256: Thanks but I can't find such a link. Can you indicate me where it is please? -- OneMusicDream (talk) 20:45, 29 July 2018 (UTC)[reply]
@OneMusicDream: Try the link labeled "Current uses" at the top right. Mahir256 (talk) 20:53, 29 July 2018 (UTC)[reply]
@Mahir256: Thanks. I've set the limit to 100,000 and I get "Query timeout limit reached". Is there any way to generate a huge file containing hundreds of thousands of lines? -- OneMusicDream (talk) 21:23, 29 July 2018 (UTC)[reply]
@OneMusicDream: With SPARQL, you're basically stuck with the one-minute time limit for queries. I know of no easy way to get 100,000 items in a single query; most queries that I've tried to extract such a list also time out. Maybe others can help you in this regard. Mahir256 (talk) 21:28, 29 July 2018 (UTC)[reply]
@OneMusicDream: - 143476 results in 20632 ms
SELECT ?item ?itemLabel ?value 
{
  ?item wdt:P434 ?value .
  optional {?item rdfs:label ?itemLabel . filter(lang(?itemLabel)="en")}
}
Try it!
--Tagishsimon (talk) 21:46, 29 July 2018 (UTC)[reply]
You can also use BEACON for this: https://tools.wmflabs.org/wikidata-todo/beacon.php?prop=434&source=0&site= -- Andy Mabbett (Pigsonthewing); Talk to Andy; Andy's edits 11:50, 30 July 2018 (UTC)[reply]
@Tagishsimon, Pigsonthewing: Wonderful, thank you both! @Tagishsimon: can you please improve the script to show only artists (humans and bands) - musical group (Q215380), human (Q5) and not songs/labels/releases? OneMusicDream (talk) 07:29, 31 July 2018 (UTC)[reply]
@OneMusicDream:
SELECT ?item ?itemLabel ?value 
{
  ?item wdt:P434 ?value .
  {?item wdt:P31 wd:Q215380.}
  UNION
  {?item wdt:P31 wd:Q5.}
  optional {?item rdfs:label ?itemLabel . filter(lang(?itemLabel)="en")}
}
Try it!
--Tagishsimon (talk) 07:35, 31 July 2018 (UTC)[reply]
@Tagishsimon: Thank you very much. My bad, the MusicBrainz artist ID (P434) is already about "MusicBrainz artist ID", so it made no sense to ask for artists + bands. But the script is very good for having an example, thanks a lot! -- OneMusicDream (talk) 23:03, 31 July 2018 (UTC)[reply]

Academic papers on a given subject

I'm trying to find academic papers on a given subject, so that I can add main subject (P921). This query, for example,:

#papers by string in title
SELECT ?item ?itemLabel
{
  ?item wdt:P31 wd:Q13442814.
  ?item wdt:P1476 ?title.
  filter(contains(str(?title),"OpenStreetMap")) .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
Try it!

times out. Can anyone suggest a way to optimise it, or to sub-divide the task, please? Would it be more efficient to check labels instead of P1476? Andy Mabbett (Pigsonthewing); Talk to Andy; Andy's edits 11:47, 30 July 2018 (UTC)[reply]

Might be worth summoning @Daniel Mietchen: to ask him to explain the fu that makes him so prolific in this regard. --Tagishsimon (talk) 06:53, 31 July 2018 (UTC)[reply]

I'm usually using variants of the following query:

The following query uses these:

  • Properties: main subject (P921)  View with Reasonator View with SQID
    SELECT DISTINCT ?item ?itemLabel 
    WHERE {
      hint:Query hint:optimizer "None".
      SERVICE wikibase:mwapi {
        bd:serviceParam wikibase:api "Search";
                        wikibase:endpoint "www.wikidata.org";
                        mwapi:srsearch "zika haswbstatement:P31=Q13442814".
        ?title wikibase:apiOutput mwapi:title.
      }
      BIND(IRI(CONCAT(STR(wd:), ?title)) AS ?item)
      FILTER NOT EXISTS { ?item wdt:P921 wd:Q202864. }
      SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
    }
    # LIMIT 10000
    

--Daniel Mietchen (talk) 19:24, 31 July 2018 (UTC)[reply]

Thanks Daniel. Indistinguishable from magic. Periodic reminder for fellow SPARQL students that MWAPI is documented here. --Tagishsimon (talk) 00:31, 1 August 2018 (UTC)[reply]
Seconded. Thank you, Daniel. Andy Mabbett (Pigsonthewing); Talk to Andy; Andy's edits 11:41, 1 August 2018 (UTC)[reply]
I'm basically just the conveyor of the magic that Lucas Werkmeister had injected into this query a while back. --Daniel Mietchen (talk) 21:01, 4 August 2018 (UTC)[reply]

Airports (again :) )

Hello, would it be possible to get

  • a list of airports (except :
 rail infrastructure (Q1311670)
 Royal Air Force station (Q7373622)
 heliport (Q502074) and
 minus {?item2 wdt:P576 ?date}                     # exclude destructed airports

that have multiples natures of airports (exemple : airport + aerodrome instead of simply airport)

  • a list of airports (except :
 rail infrastructure (Q1311670)
 Royal Air Force station (Q7373622)
 heliport (Q502074) and
 minus {?item2 wdt:P576 ?date}                     # exclude destructed airports

that have multiples countries (exemple : former kingdom Poland + current Poland, etc)

Thanks for helping me clean data :)
[Besides, is it useful to say that "former country X" is "deprecated" and "current Poland" is "preferred" ? how to filter only current and valid data) ?] --Bouzinac (talk) 15:30, 30 July 2018 (UTC)[reply]

Hi @Bouzinac: first, let's cover a previous request - you asked earlier about items which 'have a native language wikipage but no English wikipage?'. I do not have that for you, but something similar. The report below shows items with a link to the Bahasa Melayu wikipedia (ms.wikipedia) - I understand this to be Malaysian wikipedia, though I think in fact there are several language versions covering that country. You can adapt this query (with some care) to check for sitelinks existing on wikipedia A but not on wikipedia B. Alter the URL strings after schema:isPartOf. The difficulty I have with the full request is that of mapping a country to a language wikipedia ... it can be done, probably, but is complex and fraught with multi-language issues. fwiw, it's a two part query ... the inner query finds candidate airports, but then groups them to eliminate duplicates; the outer query gets codes & countries & labels.
select distinct ?item ?itemLabel ?iata ?icao ?countryLabel ?ms_Sitelink with {select ?item ?ms_Sitelink WHERE {
  {?item wdt:P238 ?iata .}
  UNION
  {?item wdt:P239 ?icao .}
  ?ms_Sitelink schema:about ?item; schema:isPartOf <https://ms.wikipedia.org/> .
  filter not exists { ?enSitelink schema:about ?item; schema:isPartOf <https://en.wikipedia.org/> .}
  } group by ?item ?ms_Sitelink } as %i where {
  include %i
  optional {?item wdt:P17 ?country.}
  optional {?item wdt:P238 ?iata .}
  optional {?item wdt:P239 ?icao .}
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
} order by ?country ?ms_Sitelink
Try it!
--Tagishsimon (talk) 00:39, 31 July 2018 (UTC)[reply]
Here is airports with multiple countries, and with the exclusions you listed - are you sure about rail infrastructure (Q1311670)? The inner query finds the airports and by grouping them, counts the countries. The outer query filters where more than one country was counted, and finds codes, labels, and countrynames. There are few enough results that I have not bothered to look at issues of multiple / end-dated codes or countries.
select distinct ?item ?itemLabel ?iata ?icao ?countryLabel with {select ?item (count(distinct ?country) as ?cc)  WHERE {
  ?item wdt:P31/wdt:P279* wd:Q1248784.
  ?item wdt:P17 ?country.
  } group by ?item } as %i where {
  include %i
  filter (?cc >1)
  optional {?item wdt:P17 ?country.}
  optional {?item wdt:P238 ?iata .}
  optional {?item wdt:P239 ?icao .}
  minus {?item wdt:P31 wd:Q1311670.}
  minus {?item wdt:P31 wd:Q7373622.}
  minus {?item wdt:P31 wd:Q502074.}
  minus {?item wdt:P576 ?date.}   
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
} order by ?itemLabel
Try it!
--Tagishsimon (talk) 00:53, 31 July 2018 (UTC)[reply]
And this is airports with multiple P31 types. It's possible to do this report so that we get one row per airport ... couple of different ways of doing so (group-concatenate values, or specify a column for each potential type of P31 ... but there are 62 distinct instance types, so maybe that won't work) ... see how you get on.
select distinct ?item ?itemLabel ?iata ?icao ?instanceLabel with {select ?item (count(distinct ?instance) as ?ic)  WHERE {
  ?item wdt:P31/wdt:P279* wd:Q1248784.
  ?item wdt:P31 ?instance.
  } group by ?item } as %i where {
  include %i
  filter (?ic >1)
  optional {?item wdt:P31 ?instance.}
  optional {?item wdt:P238 ?iata .}
  optional {?item wdt:P239 ?icao .}
  minus {?item wdt:P31 wd:Q1311670.}
  minus {?item wdt:P31 wd:Q7373622.}
  minus {?item wdt:P31 wd:Q502074.}
  minus {?item wdt:P576 ?date.}   
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
} order by ?itemLabel
Try it!
--Tagishsimon (talk) 01:00, 31 July 2018 (UTC)[reply]
multiple countries ==> Deduplicating done, thanks ( cleaning false statements or putting main countries when really dual nationalities)--Bouzinac (talk) 18:47, 31 July 2018 (UTC)[reply]


multiple P31 types==>cleaned!

Dear Template:Tagishsimon, could you please refine the query about malaisian wikipedia : add a column specifiying if the article exists only in malaisian wikipage ? Thank you! --Bouzinac (talk) 21:35, 1 August 2018 (UTC)[reply]

@Bouzinac: Here you go; final column is a count of the number of sitelinks ... if the count is 1, then we know that the ms.wiki is the inly sitelink. --Tagishsimon (talk) 21:49, 1 August 2018 (UTC)[reply]
select distinct ?item ?itemLabel ?iata ?icao ?countryLabel ?ms_Sitelink ?linkcount with {select ?item ?ms_Sitelink WHERE {
  {?item wdt:P238 ?iata .}
  UNION
  {?item wdt:P239 ?icao .}
  ?ms_Sitelink schema:about ?item; schema:isPartOf <https://ms.wikipedia.org/> .
  filter not exists { ?enSitelink schema:about ?item; schema:isPartOf <https://en.wikipedia.org/> .}
  } group by ?item ?ms_Sitelink } as %i where {
  include %i
  optional {?item wdt:P17 ?country.}
  optional {?item wdt:P238 ?iata .}
  optional {?item wdt:P239 ?icao .}
  optional {?item wikibase:sitelinks ?linkcount. }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
} order by ?linkcount ?country ?ms_Sitelink
Try it!
--Tagishsimon (talk) 21:49, 1 August 2018 (UTC)[reply]
@Tagishsimon:, wow 1200 elements in malaisian... almost impossible for me to check one by one, some true airports (mainly airstrips), some false/doubles. I give up for those only having ICAO code, and try to clean those with both iata+icao. Thank you, in any case--Bouzinac (talk) 08:56, 2 August 2018 (UTC)[reply]

P597 = 321846

Hi! I am looking for an example of a SPARQL request. I want to list the enwiki and the dewiki sitelink title name of the item that has the WTA player ID (P597) with the value (as string type) "321846". For this example the title names has to be "Lena Rüffer", but I'm not able to build the SPARQL code. Thanks for any help, Doc Taxon (talk) 00:10, 1 August 2018 (UTC)[reply]

@Doc Taxon: this gives you the article names. ?sitelink_en and ?sitelink_de, if added to the select line, give you the full URL.
SELECT ?item ?itemLabel ?en_article ?de_article
WHERE 
{
  ?item wdt:P597 ?value.
  filter(?value="321846")
  ?en_article ^schema:name ?sitelink_en . ?sitelink_en schema:about ?item ; schema:isPartOf <https://en.wikipedia.org/> .
  ?de_article ^schema:name ?sitelink_de . ?sitelink_de schema:about ?item ; schema:isPartOf <https://de.wikipedia.org/> .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
Try it!
--Tagishsimon (talk) 00:21, 1 August 2018 (UTC)[reply]
@Tagishsimon: wow, so fast! Thanks a lot for the reply ... Doc Taxon (talk) 00:29, 1 August 2018 (UTC)[reply]
@Tagishsimon: Oh, I also need both the en_itemLabel and de_itemLabel Doc Taxon (talk) 00:42, 1 August 2018 (UTC)[reply]
@Doc Taxon: I thought about that, but was too lazy to give it to you in the first query. Here's a souped-up version.
SELECT ?item ?en_label ?de_label ?en_article ?de_article
WHERE 
{
  ?item wdt:P597 ?value.
  filter(?value="321846")
  optional {?en_article ^schema:name ?sitelink_en . ?sitelink_en schema:about ?item ; schema:isPartOf <https://en.wikipedia.org/> .}
  optional {?de_article ^schema:name ?sitelink_de . ?sitelink_de schema:about ?item ; schema:isPartOf <https://de.wikipedia.org/> .}
  optional {?item rdfs:label ?en_label. filter(lang(?en_label)="en")}
  optional {?item rdfs:label ?de_label. filter(lang(?de_label)="de")}
}
Try it!
--Tagishsimon (talk) 01:01, 1 August 2018 (UTC)[reply]
@Tagishsimon: thanks a lot again, Doc Taxon (talk) 02:32, 1 August 2018 (UTC)[reply]

Double Elo ratings

Is it possible to query if the property Elo rating (P1087) is used in an item with the same value of the qualifier point in time (P585) twice (it should not be discriminated between day and month precision, so 01-05-2018 and 05-2018 should be recognized as identical) ? Steak (talk) 07:32, 1 August 2018 (UTC)[reply]

Can be done, @Steak:, but we quickly run into timeouts which might render it impractical without multiple queries segmenting chess players by country, etc. The code below seems to be the thing; right now it restricts the query to UK chess players (and sometimes runs, sometimes timesout?). Commented out is a values statement restricting it to Judit Polgar - uncomment it, and comment the UK citizen line, to see a result for Judit. I don't know how inefficient my code is or where at the moment to go for improvements. The problem is chess players have so many ELO ratings, and the Cartesian product of these exhausts the hamsters.
SELECT ?item ?en_label ?pit1 ?value1 ?pit2 ?value2 
WHERE 
{
  ?item wdt:P106 wd:Q10873124.      # occupation of chess player
# values ?item {wd:Q183250}         # the JuditP values statement
  ?item wdt:P27 wd:Q145.            # UK citizen - comment out if you want to enstate the JuditP values statement

  ?item p:P1087 ?statement1.        # find first date block
  ?statement1 ps:P1087 ?value1.
  ?statement1 pqv:P585 ?node1.
  {?node1 wikibase:timePrecision "11"^^xsd:integer.}
  UNION
  {?node1 wikibase:timePrecision "10"^^xsd:integer.}
  ?node1 wikibase:timeValue ?pit1.
             
  ?item p:P1087 ?statement2.        # find second date block
  ?statement2 ps:P1087 ?value2.
  ?statement2 pqv:P585 ?node2.
  {?node2 wikibase:timePrecision "11"^^xsd:integer.}
  UNION
  {?node2 wikibase:timePrecision "10"^^xsd:integer.}
  ?node2 wikibase:timeValue ?pit2.
 
  filter(month(?pit1)=month(?pit2))  # logic
  filter(year(?pit1)=year(?pit2))
  filter (str(?statement1)>str(?statement2))
  
  optional {?item rdfs:label ?en_label. filter(lang(?en_label)="en")}
}
Try it!
--Tagishsimon (talk) 10:44, 1 August 2018 (UTC)[reply]
Thanks. Do you think this can be used as a complex constraint? Steak (talk) 11:00, 1 August 2018 (UTC)[reply]
No experience of. Maybe, since it's effectively doing the Polgar trick - dealing with a single item only, so might avoid timeout. Would probably need only the stuff from the # find first date block line downwards, and without the optional labels statement. --Tagishsimon (talk) 11:06, 1 August 2018 (UTC)[reply]

Find items who have an article in atj.wp and without French description

Hello,

I need help to write a query... I'm looking for items that have an article in Atikamekw (atj.wp) but that has no description in French language.

Thank you inadvance, Benoit Rochon (talk) 15:13, 1 August 2018 (UTC)[reply]

@Benoit Rochon: This, I think...and some of the items that lack fr descriptions also lack fr labels...
SELECT ?item ?fr_label ?atj_article ?sitelink_atj 
WHERE 
{
?atj_article ^schema:name ?sitelink_atj . ?sitelink_atj schema:about ?item ; schema:isPartOf <https://atj.wikipedia.org/> .
filter not exists {?item schema:description ?fr_desc. filter(lang(?fr_desc)="fr")}
optional {?item rdfs:label ?fr_label. filter(lang(?fr_label)="fr")}
} order by ?fr_label
Try it!
--Tagishsimon (talk) 16:15, 1 August 2018 (UTC)[reply]
Here, gratuitously, is the set that are missing their fr label, whether or not they have a fr description
SELECT ?item ?fr_desc ?atj_article ?sitelink_atj 
WHERE 
{
?atj_article ^schema:name ?sitelink_atj . ?sitelink_atj schema:about ?item ; schema:isPartOf <https://atj.wikipedia.org/> .
optional {?item schema:description ?fr_desc. filter(lang(?fr_desc)="fr")}
filter not exists {?item rdfs:label ?fr_label. filter(lang(?fr_label)="fr")}
} order by ?fr_desc
Try it!

Thank you Tagishsimon for your help, that is perfect. But I'm not sure the difference between both queries? Best, Benoit Rochon (talk) 13:20, 2 August 2018 (UTC)[reply]

@Benoit Rochon: The first shows items where there is no French description. The second shows items where there is no French label. There is maybe a wide overlap :) --Tagishsimon (talk) 13:23, 2 August 2018 (UTC)[reply]

Names & Birthdays of current US Representatives

What would a query look like for the label (or given name (P735) & family name (P734)) and date of birth (P569) of the current United States representative (Q13218630)? Thanks! U+1F360 (talk) 14:21, 2 August 2018 (UTC)[reply]

@U+1F360: It would look as below, but the data does not right now support it - I'm guessing few P39-Q13218630 statements are end-dated, so we get 10k hits - and even that number seems excessive to me :( .
SELECT distinct ?item ?itemLabel ?givennameLabel ?familynameLabel ?dob
WHERE 
{
  ?item p:P39 ?statement.                     #there is a P39 for the item
  ?statement ps:P39 wd:Q13218630.             #the P39 is United States representative
  filter not exists {?statement pq:P582 [].}  #the P39 does not have an end date
  optional {?item wdt:P735 ?givenname.}       #item might have a given name
  optional {?item wdt:P734 ?familyname.}      #item might have a family name
  optional {?item wdt:P569 ?dob.}             #item might have a date of birth
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
  }
Try it!
--Tagishsimon (talk) 18:17, 2 August 2018 (UTC)[reply]
@Tagishsimon: Interesting! Thanks! U+1F360 (talk) 18:21, 2 August 2018 (UTC)[reply]

Hello Taghsimon, I thought this query would end up work but keeps time-outrunned : any tip ? In fact, my final aim is to help build this dataset as requested by a wikipedian :
for airports that have an fr.wiki airport article: {'continent', 'country', 'IATA', 'ICAO', 'airport article title', 'destination city article title'} for airports that do not have an fr.wiki airport article (interwiki linking to en.wiki): {'continent', 'country', 'IATA', 'ICAO', , 'destination city article title', 'QID'} (see discussion there [1])

SELECT distinct ?item ?itemLabel ?countryLabel ?fr_article ?Wfr ?en_article ?Wen ?iata ?icao WHERE {
  ?item wdt:P31/wdt:P279* wd:Q1248784.                     # item must be an instance of an airport, or an instance that is a subclass of an airport
  optional {?item wdt:P17 ?country .                        # ?item may have a country 
           filter not exists {?country pq:P582 [].} }        # ?country that still exists              
  optional {?item p:P238 ?node .                           # item may have a P238 (IATA) node
           ?node ps:P238 ?iata.                            # and the node has a P238 (IATA) value
           filter not exists {?node pq:P582 [].}   }       # but the value is ignored if it has an end-date
  optional {?item wdt:P239 ?icao .                         # item may have a P239 (ICAO) value  
           filter not exists {?icao pq:P582 [].}   }      # P239 that still exists        
  optional {?Wen schema:about ?item;                       # item may have a sitelink
            schema:isPartOf <https://en.wikipedia.org/>;   # the sitelink points to en.wikipedia
            schema:name ?en_article.}                      # and has an article name
  optional {?Wfr schema:about ?item;                       # item may have a sitelink
            schema:isPartOf <https://fr.wikipedia.org/>;   # the sitelink points to fr.wikipedia 
            schema:name ?fr_article.}                      # and has an article name
  minus {?item wdt:P31/wdt:P279* wd:Q695850.}              # exclude items having instances that are military airports & subclasses thereof
  bind(coalesce(?en_article,?fr_article) as ?article)      # create ?article if there is an EN or FR wikipedia article
  minus {?item wdt:P31/wdt:P279* wd:Q695850.}      # exclude military airports
  minus {?item wdt:P31/wdt:P279* wd:Q1311670.}     # exclude railways stations
  minus {?item wdt:P31/wdt:P279* wd:Q7373622.}     # exclude Royal Air Force
  minus {?item wdt:P31/wdt:P279* wd:Q502074.}      # exclude héliports
  minus {?item wdt:P576 ?date}                     # exclude destructed airports
  minus {?item wdt:P31/wdt:P279* wd:Q44665966.}    # exclude  airports being build
  filter(bound(?article))                                  # select only if there is an ?article
  bind(coalesce(?iata,?icao) as ?code)                     # create ?code if there is an ICAO or IATA code 
  filter(bound(?code))                                     # select only if there is an ?code
  minus {?item wdt:P576 [].}                               # remove items with P576 (dissolved, abolished or demolished) as a main property
  minus {?item wdt:P582 [].}                               # remove items with P582 (end date) as a main property
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" } . #this bit gets labels for Qid values
} group by ?item ?itemLabel ?sitelink order by ?itemLabel
Try it!

--Bouzinac (talk) 19:26, 2 August 2018 (UTC)[reply]

@Bouzinac: this gets your query working, I think. Let me know if it works for you. I can look at 'destination city article title' later ... not sure how well the data support that. LHR should give us London. What should Stanstead airport give? --Tagishsimon (talk) 19:40, 2 August 2018 (UTC)[reply]
SELECT distinct ?item ?itemLabel ?countryLabel ?fr_article ?Wfr ?en_article ?Wen ?iata ?icao WHERE {
  ?item wdt:P31/wdt:P279* wd:Q1248784.                     # item must be an instance of an airport, or an instance that is a subclass of an airport
  optional {?item wdt:P17 ?country .                        # ?item may have a country 
           filter not exists {?country pq:P582 [].} }        # ?country that still exists              
  optional {?item p:P238 ?node .                           # item may have a P238 (IATA) node
           ?node ps:P238 ?iata.                            # and the node has a P238 (IATA) value
           filter not exists {?node pq:P582 [].}   }       # but the value is ignored if it has an end-date
  optional {?item p:P239 ?node2. 
            ?node2 ps:P239 ?icao .                         # item may have a P239 (ICAO) value  
           filter not exists {?node2 pq:P582 [].}   }      # P239 that still exists        
  optional {?Wen schema:about ?item;                       # item may have a sitelink
            schema:isPartOf <https://en.wikipedia.org/>;   # the sitelink points to en.wikipedia
            schema:name ?en_article.}                      # and has an article name
  optional {?Wfr schema:about ?item;                       # item may have a sitelink
            schema:isPartOf <https://fr.wikipedia.org/>;   # the sitelink points to fr.wikipedia 
            schema:name ?fr_article.}                      # and has an article name
  minus {?item wdt:P31/wdt:P279* wd:Q695850.}              # exclude items having instances that are military airports & subclasses thereof
  bind(coalesce(?en_article,?fr_article) as ?article)      # create ?article if there is an EN or FR wikipedia article
  minus {?item wdt:P31/wdt:P279* wd:Q695850.}      # exclude military airports
  minus {?item wdt:P31/wdt:P279* wd:Q1311670.}     # exclude railways stations
  minus {?item wdt:P31/wdt:P279* wd:Q7373622.}     # exclude Royal Air Force
  minus {?item wdt:P31/wdt:P279* wd:Q502074.}      # exclude héliports
  minus {?item wdt:P576 ?date}                     # exclude destructed airports
  minus {?item wdt:P31/wdt:P279* wd:Q44665966.}    # exclude  airports being build
  filter(bound(?article))                                  # select only if there is an ?article
  bind(coalesce(?iata,?icao) as ?code)                     # create ?code if there is an ICAO or IATA code 
  filter(bound(?code))                                     # select only if there is an ?code
  minus {?item wdt:P576 [].}                               # remove items with P576 (dissolved, abolished or demolished) as a main property
  minus {?item wdt:P582 [].}                               # remove items with P582 (end date) as a main property
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" } . #this bit gets labels for Qid values
} order by ?itemLabel
Try it!
thanks,but no icao are present in your dataset. For your question about London, it should be disambiguated somewhat like "London - Stansted" // "Paris - Orly" etc. --Bouzinac (talk) 20:02, 2 August 2018 (UTC)[reply]
@Bouzinac: icao fixed in the above query. My point about, for example, London Stansted Airport (Q8709) is, what statement exists in the record to guide us to the city served. (Okay, so in this example they include London in the name ... we cannot rely on that, though. Neither the P131 value and the P625 coordinate help us greatly. --Tagishsimon (talk) 20:13, 2 August 2018 (UTC)[reply]

Need for a simple count that is time-consuming

Generating "time out" error. I need this counting query to https://github.com/OSMBrasil/semantic-bridge

    SELECT  (COUNT(DISTINCT ?item) AS ?count)
    WHERE {
      ?item wdt:P402 _:b0.
      ?item  (wdt:P31*|wdt:P279*)/wdt:P17 wd:Q155 .
    }

--Krauss (talk) 21:14, 2 August 2018 (UTC)[reply]

PS: expected ≥ 5580. I try with "similar but not the same" operations,

SELECT  (COUNT(DISTINCT ?item) AS ?count) 
WHERE {
      ?item wdt:P402 _:b0 .
      ?item wdt:P31*/wdt:P17 wd:Q155
}
# wdt:P17 = 5579
# wdt:P31*/wdt:P17 = 5580   #seems optimal
# wdt:P31*/wdt:P279*/wdt:P17 = 5580
# wdt:P279*/wdt:P31*/wdt:P17 = 5580
# ... seems all = 5580

SELECT  (COUNT(DISTINCT ?item) AS ?count) 
WHERE {
      ?item wdt:P279*/wdt:P31*/wdt:P17 wd:Q155
}
# wdt:P17 = 99224
# wdt:P31*/wdt:P17 = 100856
# wdt:P31*/wdt:P279*/wdt:P17 = 101035 #seems optimal
# wdt:P279*/wdt:P31*/wdt:P17 = 100981

I'm not sure what you are trying to count. In general, P17=Q155 should be present on all relevant items. So doing the below should be sufficient (currently 5581):

SELECT  (COUNT(DISTINCT ?item) AS ?count) 
WHERE
{
      ?item wdt:P402 _:b0.
      ?item wdt:P17 wd:Q155 .
}

Try it!
--- Jura 21:41, 2 August 2018 (UTC)[reply]

Oops, sorry, edited with DISTINCT as you remember!
@Jura1: Thanks (!). Sorry, your query is not what I need, the problem is described in https://stackoverflow.com/q/51660914/287948 So, there are a way to submit query where we can wait by a result?

END, problem solved by Stanislav Kralin (see (wdt:P31|wdt:P279)*) with QueryHints:

SELECT DISTINCT ?item ?itemLabel ?osm_relid ?name {
  ?itemi wdt:P17 wd:Q155 .
  hint:Prior hint:runFirst true .
  ?item (wdt:P31|wdt:P279)* ?itemi .
  ?item wdt:P625 [].  # or P402
  OPTIONAL { ?item wdt:P1448 ?name. }
  OPTIONAL { ?item wdt:P402 ?osm_relid .}
  SERVICE wikibase:label { 
      bd:serviceParam wikibase:language "en,[AUTO_LANGUAGE]". 
  }
}
Good that it is sorted. But fwiw, queryhints was not the cure; the query works as well without. The qualifying set is restricted by ?itemi wdt:P17 wd:Q155 ., much as Jura noted earlier. --Tagishsimon (talk) 16:29, 3 August 2018 (UTC)[reply]

Two simple queries for sl.wikisource

Hi. I'm kindly requesting two simple queries for sl.wikisource:

  • Articles in the Category:Vsi avtorji, that has P569 (date of birth) with multiple values (multiple dates),
  • Articles in the Category:Vsi avtorji, that has P569 with just one value (date), but with missing day, month or year (so with no complete date). Grateful --Janezdrilc (talk) 17:53, 3 August 2018 (UTC)[reply]
@Janezdrilc: I cannot do that, as I do not know how to integrate an article's membership of a wikidata category, with data derived from its wikidata item. However, the sl.wikisource articles are authors, and Petscan establishes that all of them have wikidata items with suitable P31 and P21 statements. There are <1000 links from wikidata people items to sl.wikisource, and the query below shows them all. The date precision column tells you how precise the date is - 11=day, 10=month, 9=year, etc. The report also show the few records that have no DoB.
SELECT ?item ?itemLabel ?SL ?sl_article ?birth ?precision where
{
  ?item wdt:P31 wd:Q5.
  ?SL schema:about ?item;                       # item has a sitelink
  schema:isPartOf <https://sl.wikisource.org/>;   # the sitelink points to sl.wikisource 
  schema:name ?sl_article.                     # and has an article name
  optional {?item p:P569/psv:P569 [wikibase:timePrecision ?precision; wikibase:timeValue ?birth].} #optionally has a DoB with a stated precision
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" } . #this bit gets labels for Qid values
} order by asc(?precision)
Try it!
--Tagishsimon (talk) 18:11, 3 August 2018 (UTC)[reply]
Here are the 7 items with more than one DoB.
SELECT ?item ?itemLabel ?SL ?sl_article ?birth ?precision with {select ?item ?itemLabel ?SL ?sl_article ?precision (count(?birth) as ?count) where
{
  ?item wdt:P31 wd:Q5.
  ?SL schema:about ?item;                       # item has a sitelink
  schema:isPartOf <https://sl.wikisource.org/>;   # the sitelink points to sl.wikisource 
  schema:name ?sl_article.                     # and has an article name
  ?item p:P569/psv:P569 [wikibase:timePrecision ?precision; wikibase:timeValue ?birth]. #has a DoB with a stated precision
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" } . #this bit gets labels for Qid values
} group by ?item ?itemLabel ?SL ?sl_article ?precision } as %i
where
{include %i
         filter(?count>1)
         }
Try it!
--Tagishsimon (talk) 18:16, 3 August 2018 (UTC)[reply]

Thanks for help. --Janezdrilc (talk) 21:45, 6 August 2018 (UTC)[reply]

Newspapers with an infobox in their enwiki page

Hi all. Is a query like this possible? What's the best way to approach it? I have similar trouble thinking about how to query for article assessments (other than FA and GA badges).--99of9 (talk) 11:28, 4 August 2018 (UTC)[reply]

@99of9: Possibly two ways to approach; one using MWAPI (still above my competence) and two via petscan, where I've made an attempt. --Tagishsimon (talk) 11:50, 4 August 2018 (UTC)[reply]

Here is a first attempt at using MWAPI:

The following query uses these:
SELECT * WHERE {
  SERVICE wikibase:mwapi {
      bd:serviceParam wikibase:api "Search" .
      bd:serviceParam wikibase:endpoint "en.wikipedia.org" .
      bd:serviceParam mwapi:srsearch "insource:\"infobox newspaper\"" .
      ?title wikibase:apiOutput mwapi:title .
  }
} LIMIT 200
--Daniel Mietchen (talk) 21:20, 4 August 2018 (UTC)[reply]
Super. I tried replacing "title" with "item", but it broke. How do I get the Q id's so I can combine this with other conditions (e.g. publication location)?
Next try — basically taken straight from the documentation linked above:
The following query uses these:
  • Properties: instance of (P31)  View with Reasonator View with SQID, subclass of (P279)  View with Reasonator View with SQID
    SELECT ?item ?itemLabel ?type ?typeLabel WHERE {
     {
       SELECT ?item WHERE {
        SERVICE wikibase:mwapi {
          bd:serviceParam wikibase:endpoint "en.wikipedia.org" .
          bd:serviceParam wikibase:api "Generator" .
          bd:serviceParam mwapi:generator "search" .
          bd:serviceParam mwapi:gsrsearch "insource:\"infobox newspaper\"" .
          bd:serviceParam mwapi:gsrlimit "max" .
          ?item wikibase:apiOutputItem mwapi:item .
        }
      } LIMIT 100
     }
     hint:Prior hint:runFirst "true".
     ?item wdt:P31|wdt:P279 ?type .
     SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
    } LIMIT 100
    
--Daniel Mietchen (talk) 00:30, 8 August 2018 (UTC)[reply]
@Daniel Mietchen: Awesome. I promise I tried to digest the documentation linked above, but this is my first time working with mwapi. I think you've cut through my blockage, so I should be able to do whatever is needed now. --99of9 (talk) 02:51, 8 August 2018 (UTC)[reply]

List of viking age Icelanders

Hi. I am looking for a rather specific list for en:List of viking age Icelanders. I don't mind having to run a number of queries and doing some sorting to get it. I am looking for:

  • People
  • Who lived in Iceland
  • And were alive at any point between 793–1066 AD

Thanks. Frayae (talk) 12:27, 4 August 2018 (UTC)[reply]

@Frayae: This gets you somewhere near. We do not have a concept of 'having lived in', so I use citizenship as a proxy. My dob/dod date ranges establish they were either born or died within your range ... clearly someone who died in 1067 might also have lived in Iceland.
SELECT ?item ?itemLabel 
WHERE 
{
  ?item wdt:P31 wd:Q5
  {?item wdt:P27 wd:Q20.}  #citizen of Norway
  UNION                    #or  
  {?item wdt:P27 wd:Q189.} #citizen of Iceland

  {?item wdt:P569 ?dob. filter(year(?dob)>793) filter(year(?dob)<1066)}
  union
  {?item wdt:P570 ?dod. filter(year(?dod)>793) filter(year(?dod)<1066)}
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
  }
Try it!
--Tagishsimon (talk) 13:21, 4 August 2018 (UTC)[reply]
@Frayae: Version of User:Tagishsimon's query, adapted to show years and places of birth and death where we have them, and to try to put the results into some sort of order. I was uneasy about the use of country of citizenship (P27) = Norway (Q20) or Iceland (Q189), as there's a bit of controversy regarding the use of modern-day states as values for P27 for times before those states existed in their current form. (There's eg a curren t thread on Project Chat), but given the apparent lack of information on WD for places of birth and death, there may be no alternative.
SELECT DISTINCT ?item ?itemLabel ?s1Label ?s2Label (year(?dob) AS ?yob) (year(?dod) AS ?yod) ?pobLabel ?podLabel
WHERE 
{
  ?item wdt:P31 wd:Q5
  {?item wdt:P27 wd:Q20}  #citizen of Norway
  UNION                    #or  
  {?item wdt:P27 wd:Q189} #citizen of Iceland

  {?item wdt:P569 ?dob . FILTER ((year(?dob) > 793) && ( year(?dob) < 1066 )) } 
  UNION
  {?item wdt:P570 ?dod . FILTER ((year(?dod) > 793) && (year(?dod) < 1066 ))  }

  OPTIONAL {?item wdt:P569 ?dob } .
  OPTIONAL {?item wdt:P570 ?dod } .

  BIND( IF (bound(?dob) && !isBlank(?dob) , year(?dob) , year(?dod) - 40) AS ?order_date) . 
  OPTIONAL {?item wdt:P19 ?pob} .
  OPTIONAL {?item wdt:P20 ?pod} .
  OPTIONAL {?item wdt:P27 ?s1 . FILTER(?s1 = wd:Q189)} .
  OPTIONAL {?item wdt:P27 ?s2 . FILTER(?s2 = wd:Q20)} .
          
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
} ORDER BY ?order_date ?yod ?yob ?itemLabel
Try it!
Jheald (talk) 19:10, 4 August 2018 (UTC)[reply]
There's also Icelandic Commonwealth (Q62389) but it does seem to yield any more Vikings. Another approach, hinted at in Jheald's query, is to look for place of birth (P19) and place of death (P20) values which are within Iceland (Q189). Agree citizenship applied to times past is often fraught with problems. --Tagishsimon (talk) 19:34, 4 August 2018 (UTC)[reply]
Thanks. I understand the records are not perfect, but it's a good enough starting point for the article. Frayae (talk) 19:50, 4 August 2018 (UTC)[reply]
@Frayae: I hope so. The thing I was trying to underline is there may be vikings who are in Wikidata, but not showing up in the above query because they don't have P27 set. How many? No idea, because I can't find them. Jheald (talk) 20:04, 4 August 2018 (UTC)[reply]
@Jheald: what if you query for all people who lived in that period. How many would there be? Frayae (talk) 20:10, 4 August 2018 (UTC)[reply]
@Frayae: Difficult to know how to get the query to run within the time limit (60 seconds). The number of people with P27 set to the two values is quite small. But without that limitation, the whole of Q5 is huge. Too many items to run even one of the date filters on, and the query times out: tinyurl.com/y9wtgp9j. User:Tagishsimon, any ideas? Jheald (talk) 20:25, 4 August 2018 (UTC)[reply]
None down this avenue. There might be something that could be done with MWAPI and searching language wikipedias, but not that I can rustle up in a meaningful timescale. Or Petscan with an embedded SPARQL query. I'll think on. --Tagishsimon (talk) 20:48, 4 August 2018 (UTC)[reply]
Hmm. My petscan attempt crashes and burns for the same reason that a query not restricted to a subset, e.g. by P27, fails. I was hoping Petscan would run the query on the set of items returned w.r.t. to category chosen; sadly it doesn't seem to do this, but I guess instead returns the overlap between articles returned by the category and items returned by the query. https://petscan.wmflabs.org/?psid=5353589 fwiw (Maybe open in a new tab - takes ages to run before you get a webpage, and ultimately all you'll see is a useless list & an error message :). --Tagishsimon (talk) 22:45, 4 August 2018 (UTC)[reply]

Any airports (not military) : do they have at least one icao/iata code

Hello Taghsimon, I think I must work too on whether each element has an iata/icao code ?

Could you please list any airport (not military, not railway station) element that is deprived from an iata/icao code, + its nature ? Thanks a lot ! --Bouzinac (talk) 20:12, 4 August 2018 (UTC)[reply]

@Bouzinac: If I understand correctly, you want airports (with the normal exclusions) that a) have no IATA code b) have no ICAO code c) have neither an IATA nor an ICAO code? a) have no IATA code but MIGHT have an ICAO code b) have no IATA code but MUST have an ICAO code c) have no ICAO code but MIGHT have an IATA code d) have no ICAO code but MUST have an IATA code e) have neither an IATA nor an ICAO code?
# No IATA code & might have an ICAO code
  SELECT distinct ?item ?itemLabel (group_concat(distinct ?typeLabel; separator = ", ") as ?airport_type) (group_concat(distinct ?countryLabel; separator = ", ") as ?countryL) ?iata ?icao WHERE {
  ?item wdt:P31/wdt:P279* wd:Q1248784.                     # ?item must be an instance of an airport, or an instance that is a subclass of an airport
  ?item wdt:P31 ?type.                                     #get the type
  ?type rdfs:label ?typeLabel. filter(lang(?typeLabel)="en")  #get its label in EN
  optional {?item wdt:P17 ?country .                       # ?item may have a country 
           filter not exists {?country pq:P582 [].}        # ?country that still exists
           ?country rdfs:label ?countryLabel. filter(lang(?countryLabel)="en")}       #get the country label in EN             
  filter not exists {?item wdt:P238 ?iata .}               # there is no IATA value
  optional {?item p:P239 ?node2.                           # ?item may have a P239 property
            ?node2 ps:P239 ?icao .                         # which has an ICAO value  
           filter not exists {?node2 pq:P582 [].}   }      # and the value has no P582 end time  
  minus {?item wdt:P31/wdt:P279* wd:Q695850.}              # exclude items having instances that are military airports & subclasses thereof
  minus {?item wdt:P31/wdt:P279* wd:Q695850.}              # exclude military airports
  minus {?item wdt:P31/wdt:P279* wd:Q1311670.}             # exclude railways stations
  minus {?item wdt:P31/wdt:P279* wd:Q7373622.}             # exclude Royal Air Force
  minus {?item wdt:P31/wdt:P279* wd:Q502074.}              # exclude héliports
  minus {?item wdt:P576 ?date}                             # exclude destructed airports
  minus {?item wdt:P31/wdt:P279* wd:Q44665966.}            # exclude  airports being build
  minus {?item wdt:P576 [].}                               # remove items with P576 (dissolved, abolished or demolished) as a main property
  minus {?item wdt:P582 [].}                               # remove items with P582 (end date) as a main property
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" } . #this bit gets labels for Qid values
} group by ?item ?itemLabel ?iata ?icao order by ?itemLabel
Try it!
# No IATA code & must have an ICAO code
  SELECT distinct ?item ?itemLabel (group_concat(distinct ?typeLabel; separator = ", ") as ?airport_type) (group_concat(distinct ?countryLabel; separator = ", ") as ?countryL) ?iata ?icao WHERE {
  ?item wdt:P31/wdt:P279* wd:Q1248784.                     # ?item must be an instance of an airport, or an instance that is a subclass of an airport
  ?item wdt:P31 ?type.                                     #get the type
  ?type rdfs:label ?typeLabel. filter(lang(?typeLabel)="en")  #get its label in EN
  optional {?item wdt:P17 ?country .                       # ?item may have a country 
           filter not exists {?country pq:P582 [].}        # ?country that still exists
           ?country rdfs:label ?countryLabel. filter(lang(?countryLabel)="en")}       #get the country label in EN             
  filter not exists {?item wdt:P238 ?iata .}               # there is no IATA value
  ?item p:P239 ?node2.                           # ?item must have a P239 property
  ?node2 ps:P239 ?icao .                         # which has an ICAO value  
  filter not exists {?node2 pq:P582 [].}         # and the value has no P582 end time  
  minus {?item wdt:P31/wdt:P279* wd:Q695850.}              # exclude items having instances that are military airports & subclasses thereof
  minus {?item wdt:P31/wdt:P279* wd:Q695850.}              # exclude military airports
  minus {?item wdt:P31/wdt:P279* wd:Q1311670.}             # exclude railways stations
  minus {?item wdt:P31/wdt:P279* wd:Q7373622.}             # exclude Royal Air Force
  minus {?item wdt:P31/wdt:P279* wd:Q502074.}              # exclude héliports
  minus {?item wdt:P576 ?date}                             # exclude destructed airports
  minus {?item wdt:P31/wdt:P279* wd:Q44665966.}            # exclude  airports being build
  minus {?item wdt:P576 [].}                               # remove items with P576 (dissolved, abolished or demolished) as a main property
  minus {?item wdt:P582 [].}                               # remove items with P582 (end date) as a main property
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" } . #this bit gets labels for Qid values
} group by ?item ?itemLabel ?iata ?icao order by ?itemLabel
Try it!
# No ICAO code and might have an IATA code
  SELECT distinct ?item ?itemLabel (group_concat(distinct ?typeLabel; separator = ", ") as ?airport_type) (group_concat(distinct ?countryLabel; separator = ", ") as ?countryL) ?iata ?icao WHERE {
  ?item wdt:P31/wdt:P279* wd:Q1248784.                     # ?item must be an instance of an airport, or an instance that is a subclass of an airport
  ?item wdt:P31 ?type.                                     #get the type
  ?type rdfs:label ?typeLabel. filter(lang(?typeLabel)="en")  #get its label in EN
  optional {?item wdt:P17 ?country .                       # ?item may have a country 
           filter not exists {?country pq:P582 [].}        # ?country that still exists
           ?country rdfs:label ?countryLabel. filter(lang(?countryLabel)="en")}       #get the country label in EN             
  filter not exists {?item wdt:P239 ?icao .}               # there is no ICAO value
  optional {?item p:P238 ?node2.                           # ?item may have a P238 property
            ?node2 ps:P238 ?iata .                         # which has an IATA value  
           filter not exists {?node2 pq:P582 [].}   }      # and the value has no P582 end time  
  minus {?item wdt:P31/wdt:P279* wd:Q695850.}              # exclude items having instances that are military airports & subclasses thereof
  minus {?item wdt:P31/wdt:P279* wd:Q695850.}              # exclude military airports
  minus {?item wdt:P31/wdt:P279* wd:Q1311670.}             # exclude railways stations
  minus {?item wdt:P31/wdt:P279* wd:Q7373622.}             # exclude Royal Air Force
  minus {?item wdt:P31/wdt:P279* wd:Q502074.}              # exclude héliports
  minus {?item wdt:P576 ?date}                             # exclude destructed airports
  minus {?item wdt:P31/wdt:P279* wd:Q44665966.}            # exclude  airports being build
  minus {?item wdt:P576 [].}                               # remove items with P576 (dissolved, abolished or demolished) as a main property
  minus {?item wdt:P582 [].}                               # remove items with P582 (end date) as a main property
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" } . #this bit gets labels for Qid values
} group by ?item ?itemLabel ?iata ?icao order by ?itemLabel
Try it!
# No ICAO code and must have an IATA code
  SELECT distinct ?item ?itemLabel (group_concat(distinct ?typeLabel; separator = ", ") as ?airport_type) (group_concat(distinct ?countryLabel; separator = ", ") as ?countryL) ?iata ?icao WHERE {
  ?item wdt:P31/wdt:P279* wd:Q1248784.                     # ?item must be an instance of an airport, or an instance that is a subclass of an airport
  ?item wdt:P31 ?type.                                     #get the type
  ?type rdfs:label ?typeLabel. filter(lang(?typeLabel)="en")  #get its label in EN
  optional {?item wdt:P17 ?country .                       # ?item may have a country 
           filter not exists {?country pq:P582 [].}        # ?country that still exists
           ?country rdfs:label ?countryLabel. filter(lang(?countryLabel)="en")}       #get the country label in EN             
  filter not exists {?item wdt:P239 ?icao .}               # there is no ICAO value
  ?item p:P238 ?node2.                                     # ?item must have a P238 property
  ?node2 ps:P238 ?iata .                                   # which has an IATA value  
  filter not exists {?node2 pq:P582 [].}                   # and the value has no P582 end time  
  minus {?item wdt:P31/wdt:P279* wd:Q695850.}              # exclude items having instances that are military airports & subclasses thereof
  minus {?item wdt:P31/wdt:P279* wd:Q695850.}              # exclude military airports
  minus {?item wdt:P31/wdt:P279* wd:Q1311670.}             # exclude railways stations
  minus {?item wdt:P31/wdt:P279* wd:Q7373622.}             # exclude Royal Air Force
  minus {?item wdt:P31/wdt:P279* wd:Q502074.}              # exclude héliports
  minus {?item wdt:P576 ?date}                             # exclude destructed airports
  minus {?item wdt:P31/wdt:P279* wd:Q44665966.}            # exclude  airports being build
  minus {?item wdt:P576 [].}                               # remove items with P576 (dissolved, abolished or demolished) as a main property
  minus {?item wdt:P582 [].}                               # remove items with P582 (end date) as a main property
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" } . #this bit gets labels for Qid values
} group by ?item ?itemLabel ?iata ?icao order by ?itemLabel
Try it!
# No ICAO and no IATA code
  SELECT distinct ?item ?itemLabel (group_concat(distinct ?typeLabel; separator = ", ") as ?airport_type) (group_concat(distinct ?countryLabel; separator = ", ") as ?countryL) ?iata ?icao WHERE {
  ?item wdt:P31/wdt:P279* wd:Q1248784.                     # ?item must be an instance of an airport, or an instance that is a subclass of an airport
  ?item wdt:P31 ?type.                                     #get the type
  ?type rdfs:label ?typeLabel. filter(lang(?typeLabel)="en")  #get its label in EN
  optional {?item wdt:P17 ?country .                       # ?item may have a country 
           filter not exists {?country pq:P582 [].}        # ?country that still exists
           ?country rdfs:label ?countryLabel. filter(lang(?countryLabel)="en")}       #get the country label in EN             
  filter not exists {?item wdt:P239 ?icao .}               # there is no ICAO value
  filter not exists {?item wdt:P238 ?iata .}               # there is no IATA value
  minus {?item wdt:P31/wdt:P279* wd:Q695850.}              # exclude items having instances that are military airports & subclasses thereof
  minus {?item wdt:P31/wdt:P279* wd:Q695850.}              # exclude military airports
  minus {?item wdt:P31/wdt:P279* wd:Q1311670.}             # exclude railways stations
  minus {?item wdt:P31/wdt:P279* wd:Q7373622.}             # exclude Royal Air Force
  minus {?item wdt:P31/wdt:P279* wd:Q502074.}              # exclude héliports
  minus {?item wdt:P576 ?date}                             # exclude destructed airports
  minus {?item wdt:P31/wdt:P279* wd:Q44665966.}            # exclude  airports being build
  minus {?item wdt:P576 [].}                               # remove items with P576 (dissolved, abolished or demolished) as a main property
  minus {?item wdt:P582 [].}                               # remove items with P582 (end date) as a main property
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" } . #this bit gets labels for Qid values
} group by ?item ?itemLabel ?iata ?icao order by ?itemLabel
Try it!
--Tagishsimon (talk) 22:34, 4 August 2018 (UTC)[reply]
Hello, yes, it's the # No ICAO and no IATA code that I was looking after. May I ask you to refine with "and also no FAA LID code (P240)" (I tried and it seems to timeout). Thanks! --Bouzinac (talk) 17:12, 5 August 2018 (UTC)[reply]
@Bouzinac: here you go ... we're bumping our heads on the 60 second mark ... this v.2 query seems to take half tht time, albeit we'll get multiple rows per airport having multiple types or countries.
# No ICAO and no IATA code and no FAA code
  SELECT distinct ?item ?itemLabel ?typeLabel ?countryLabel ?iata ?icao WHERE {
  ?item wdt:P31/wdt:P279* wd:Q1248784.                     # ?item must be an instance of an airport, or an instance that is a subclass of an airport
  ?item wdt:P31 ?type.                                     #get the type
  ?type rdfs:label ?typeLabel. filter(lang(?typeLabel)="en")  #get its label in EN
  optional {?item rdfs:label ?itemLabel. filter(lang(?itemLabel)="en")}
  optional {?item wdt:P17 ?country .                       # ?item may have a country 
           filter not exists {?country pq:P582 [].}        # ?country that still exists
           ?country rdfs:label ?countryLabel. filter(lang(?countryLabel)="en")}       #get the country label in EN             
  filter not exists {?item wdt:P239 ?icao .}               # there is no ICAO value
  filter not exists {?item wdt:P238 ?iata .}               # there is no IATA value
  filter not exists {?item wdt:P240 ?faa .}                # there is no FAA value
  minus {?item wdt:P31/wdt:P279* wd:Q695850.}              # exclude items having instances that are military airports & subclasses thereof
  minus {?item wdt:P31/wdt:P279* wd:Q695850.}              # exclude military airports
  minus {?item wdt:P31/wdt:P279* wd:Q1311670.}             # exclude railways stations
  minus {?item wdt:P31/wdt:P279* wd:Q7373622.}             # exclude Royal Air Force
  minus {?item wdt:P31/wdt:P279* wd:Q502074.}              # exclude héliports
  minus {?item wdt:P576 ?date}                             # exclude destructed airports
  minus {?item wdt:P31/wdt:P279* wd:Q44665966.}            # exclude  airports being build
  minus {?item wdt:P576 [].}                               # remove items with P576 (dissolved, abolished or demolished) as a main property
  minus {?item wdt:P582 [].}                               # remove items with P582 (end date) as a main property
#  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" } . #this bit gets labels for Qid values
}
Try it!
--Tagishsimon (talk) 17:58, 5 August 2018 (UTC)[reply]

Names & Types of furniture from around the world

This is a nice listing, https://en.wikipedia.org/wiki/List_of_furniture_types but I suspect there are many more international variations of "furniture in the home". For a definition to start: Name and Subclass of Things in a house or building that store items, provide rest, lighting, utility, but that are not a tool. Being broad is fine for now, since the goal is to narrow down the list later with human judgements.

SELECT ?item ?label ?_image WHERE { ?item wdt:P279 wd:Q14745. SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . ?item rdfs:label ?label } }

My suspicion is that this query misses a lot.

As a bonus, I'd like to see the smallest, simplest query possible that can provide this list. Thadguidry (talk) 16:12, 5 August 2018 (UTC)[reply]

@Thadguidry: The below for furnishings.
SELECT ?item ?label ?image WHERE { 
  ?item wdt:P279* wd:Q31807746. #subclass of furnishing
  optional {?item wdt:P18 ?image.}      #get image if there is one
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" } 
}
Try it!
and by way of example, the below for furnishings and home appliances
SELECT ?item ?label ?image WHERE { 
  {?item wdt:P279* wd:Q31807746.} #subclass of furnishing
  UNION
  {?item wdt:P279* wd:Q212920.} #subclass of home appliance
  optional {?item wdt:P18 ?image.}      #get image if there is one
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" }
}
Try it!
and then, if you want many root values, something like this:
SELECT ?item ?label ?image WHERE { 
  values ?thing {wd:Q31807746 wd:Q212920}                #add more things here
  ?item wdt:P279* ?thing.                                #each item in the VALUES array is matched in this statement
  optional {?item wdt:P18 ?image.}      #get image if there is one
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" } 
}
Try it!
--Tagishsimon (talk) 21:21, 5 August 2018 (UTC)[reply]

Hello, I need a script to select Top 50 popular first names in ukrainian wikipedia. Of course I can use script from example "Popular surnames among humans" and modify it, but.

  1. Current script never ends and gives TimeoutException.
  2. As I understand when I will add "ukrainian wikipedia" to the query it will take even more time. --Alex Blokha (talk) 23:52, 5 August 2018 (UTC)[reply]
@Alex Blokha: Solution below - albeit based on the given name (P735) property (which is to say, if they do not have a given name, they are not counted. It /might/ be possible to string-slice the Label & base counts on that, but I'm not inclined to go down that path.) Up to a point, the more you restrict a query (e.g. by specifying need for a wikilink to a specified language wikipedia) the faster the query runs, since the set of qualifying items is smaller. (I'm also presuming that uk.wikipedia.org is the Ukraine wikipedia...)
SELECT ?given ?givenLabel (count(?given) as ?count) 
WHERE 
{
 ?uk_article ^schema:name ?sitelink_uk . ?sitelink_uk schema:about ?item ; schema:isPartOf <https://uk.wikipedia.org/> .
 ?item wdt:P31 wd:Q5.
 ?item wdt:P735 ?given.
 optional {?given rdfs:label ?givenLabel. filter(lang(?givenLabel)="uk")}
  } group by ?given ?givenLabel order by desc(?count) limit 50
Try it!
--Tagishsimon (talk) 00:06, 6 August 2018 (UTC)[reply]
Thank you! :) --Alex Blokha (talk) 21:42, 6 August 2018 (UTC)[reply]

Elo ratings without source

I am trying to build a query that gives me all Elo ratings without a source ("imported from" shall not be considered as a source). I arrived at the following query, but this is not quite correct as Elo ratings without any source are not listed (I know some examples, so I know that they are missing):

SELECT ?item ?elo ?date WHERE { ?item p:P1087 ?stmt . ?stmt prov:wasDerivedFrom ?ref . MINUS { ?ref pr:P248 [] } ?stmt ps:P1087 ?elo . OPTIONAL { ?stmt pq:P585 ?date } }
Try it!

Can somebody fix? Steak (talk) 14:53, 6 August 2018 (UTC)[reply]

@Steak: I think this is as near to what you need as ou will get...
SELECT ?item ?value ?pit WHERE 
{ 
  ?item p:P1087 ?stmt . 
  optional {?stmt ps:P1087 ?value.}
  optional {?stmt pq:P585 ?pit.} 
  filter not exists { ?stmt prov:wasDerivedFrom [] . }
}
Try it!
and this is what you actually need, but it times out:
SELECT ?item ?value ?pit WHERE 
{ 
  ?item p:P1087 ?stmt . 
  optional {?stmt ps:P1087 ?value.}
  optional {?stmt pq:P585 ?pit.} 
  optional { ?stmt prov:wasDerivedFrom ?where . }
  filter not exists {?where pr:P248 [].}
  filter not exists {?where pr:P854 [].} 
}
Try it!
In passing (and probably more for my beneft than yours), here's a count of the references for ELO ratings by reference property, which allows us to check that we have no imported from Wikimedia project (P143):
SELECT ?pr (count(?pr) as ?count) WHERE 
{  
  ?item p:P1087 ?stmt . 
  ?stmt prov:wasDerivedFrom ?where . 
  ?where ?pr ?val.
} group by ?pr
Try it!
and if we zoom in on stated in (P248) we see that they all come from ratings.fide.com (the point of this is to ensure that we do not have any 'imported from wikipedia lurking within P248)
SELECT ?val (count(?val) as ?count) WHERE 
{ 
  ?item p:P1087 ?stmt . 
  ?stmt prov:wasDerivedFrom ?where . 
  ?where pr:P248 ?val.
} group by ?val
Try it!
--Tagishsimon (talk) 21:08, 6 August 2018 (UTC)[reply]
Thank you, your first query seems ok. But your second query is wrong it seems to me. I can limit the results to e.g. 100, then the query runs and quickly gives results, but all of them have a valid source. Steak (talk) 07:50, 7 August 2018 (UTC)[reply]
True. Not sure why. --Tagishsimon (talk) 10:49, 7 August 2018 (UTC)[reply]

Identifier with 11 digits

Hi! I want a query listing uses of INE municipality code (P772) with a 11-digit-length. How can I query that? Thanks in advance! strakhov (talk) 15:07, 6 August 2018 (UTC)[reply]

SELECT ?item ?id WHERE {
  ?item wdt:P772 ?id .
  FILTER(STRLEN(?id) = 11) .
}
Try it!

MisterSynergy (talk) 15:20, 6 August 2018 (UTC)[reply]

Thanks! Much appreciated. :) strakhov (talk) 20:55, 6 August 2018 (UTC)[reply]

Truthy statement fail

I've been puzzling over Steak's ELO issue, above. Along the way I've discovered the following - for me - puzzle. If I ask for the truthy values of Elo rating (P1087) for Judit Polgár (Q183250) I get 28 rows returned. If I ask for the ps:P1087 values for her, I get 123 rows. None of her ELO statements has anything other than normal rank. And I find that the 28 wdt: values returned are the same as the distinct set of ps:P1087 values.

I was expecting that if I get 123 ps:P1087 values all with normal rank, I would also get 123 wdt:P1087 values. What am I missing?

SELECT ?item ?value ?pit ?rank WHERE 
{ 
  values ?item {wd:Q183250}
  ?item p:P1087 ?stmt .
  ?stmt ps:P1087 ?value.
  ?stmt pq:P585 ?pit.
  ?stmt wikibase:rank ?rank.        
  }
Try it!
SELECT ?item ?value WHERE 
{ 
  values ?item {wd:Q183250}
  ?item wdt:P1087 ?value .
    }
Try it!

--Tagishsimon (talk) 20:20, 6 August 2018 (UTC)[reply]

Notice what happens when two ELO's are same - they differ in ?pit. WDQS returns all distinct rows for all variables (incl. those which are not SELECT'd). So when you include pq:P585 in the query, it must be reflected as well. Matěj Suchánek (talk) 12:12, 8 August 2018 (UTC)[reply]
I think the surprise is that it returns only distinct rows, rather than all possible rows. Ah well. --Tagishsimon (talk) 13:20, 8 August 2018 (UTC)[reply]
Oops, wrong vocabulary on my side. No, it does return all of them. The latter can be achieved using DISTINCT. Matěj Suchánek (talk) 13:38, 8 August 2018 (UTC)[reply]
But that's the thing. It evidently does not return all of them, but only the distinct set. --Tagishsimon (talk) 15:33, 8 August 2018 (UTC)[reply]
Now I see what you mean. I'm not aware of that but isn't it a feature? wdt: is supposed to provide simple matching, so... Matěj Suchánek (talk) 15:59, 8 August 2018 (UTC)[reply]
It may well be a feature, yes. Not one I've seen documented, but equally I doubt I've read enough documentation to rule out a mention. --Tagishsimon (talk) 16:05, 8 August 2018 (UTC)[reply]
@Smalyshev (WMF): - can you help? Is wdt: working as expected in the above example? --Tagishsimon (talk) 16:16, 8 August 2018 (UTC)[reply]

What are the elements that have airport as a class ?

I'd like to see what airport can have as instance of (P31) values : aerodrome (Q62447), airport (Q1248784) , air base (Q695850), Royal Air Force station (Q7373622), altiport (Q2840449), …. ? Thanks ! --Bouzinac (talk) 21:31, 7 August 2018 (UTC)[reply]

How would you recognize an airport when you cannot query after one of those classes? By Wikimedia categories? Matěj Suchánek (talk) 12:07, 8 August 2018 (UTC)[reply]
I meant the items linked as p31 (not airports, but what kind of sub-items are linked/apparented to "airports" ?  – The preceding unsigned comment was added by Bouzinac (talk • contribs) at 8. 8. 2018, 15:49‎ (UTC).
Something like this (ie. all subclasses of airport (Q1248784))?
SELECT ?class ?classLabel (SAMPLE(?item) AS ?sample) {
  ?class wdt:P279+ wd:Q1248784 .
  OPTIONAL { ?item wdt:P31 ?class } .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" } .
} GROUP BY ?class ?classLabel
Try it!
Matěj Suchánek (talk) 16:35, 8 August 2018 (UTC)[reply]

@Bouzinac, Matěj Suchánek: Edit conflict, but here's a similar query -

SELECT ?type ?typeLabel ?count where {
{ 
  SELECT ?type (count(?item) as ?count) where
  { ?type wdt:P279* wd:Q20977775 . # all things that are a subclass of air transport infrastructure
    ?item wdt:P31 ?type . } group by ?type } 
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
} order by desc(?count)
Try it!
This one gets everything that is an instance of anything which is a subclass of air transport infrastructure (Q20977775) and counts how many of each type there are (there may be some double-counting if things have two P31s).
Note that any Royal Air Force station (Q7373622) does not show up as it's not a subclass of air transport infrastructure (Q20977775) - possibly an omission, or maybe deliberate because not all RAF installations are actually airbases? There may well be others - as Matěj mentions, if it's not in that class hierarchy, it's hard for us to run a query that knows it's an airport.
There are also a couple of false positives - Royal Air Force (Q165862) probably shouldn't be on there, as it's the overall organisation and things can't really be an instance of it. Andrew Gray (talk) 16:48, 8 August 2018 (UTC)[reply]

To take a different tack, here are the P31s associated with aerodromes, which are not subclasses of aerodrome.

SELECT distinct ?item ?itemLabel ?type2 ?typeLabel WHERE {
  ?item wdt:P31 ?type.                     # ?item must be an instance of an airport, or an instance that is a subclass of an airport
  ?type wdt:P279* wd:Q62447.
  ?item wdt:P31 ?type2
  filter not exists {?type2 wdt:P279* wd:Q62447.}  
  ?type2 rdfs:label ?typeLabel. filter(lang(?typeLabel)="en")  #get type label in EN
  optional {?item rdfs:label ?itemLabel. filter(lang(?itemLabel)="en")}  #get item label in EN
  }
Try it!

--Tagishsimon (talk) 17:40, 8 August 2018 (UTC)[reply]

Thanks @Tagishsimon, Matěj Suchánek:, you replied to my question, and I appreciated the one with the count per the type. I suppose it is very recommended that any airport should be given only one P31s in the same "field" ? Bouzinac (talk) 20:55, 8 August 2018 (UTC)[reply]

Longest citation chain on a given topic

There are several questions in this context that I am interested in:

  1. For a given main subject (P921) (e.g. Zika virus (Q202864)), what is the longest chain (without loops) of papers that all have the same main subject (P921) and where one cites the other?
  2. How many elements does that chain have? Of course, once we have the answer to the above, it should be easy to determine, but maybe there is another route that could be taken to get at the number.
  3. How do the numbers of those chains go up if we reduce the chain length?

I had a first go at the first one:

The following query uses these:

  • Properties: main subject (P921)  View with Reasonator View with SQID, cites work (P2860)  View with Reasonator View with SQID
    SELECT * WHERE {
      ?paper1 wdt:P921 wd:Q202864 .
      ?paper1 wdt:P2860 ?paper2 .
      FILTER (?paper1 != ?paper2 )
      ?paper2 wdt:P921 wd:Q202864 .
      ?paper2 wdt:P2860 ?paper3 .
      FILTER (?paper1 != ?paper3 ) 
      FILTER (?paper2 != ?paper3 )
      ?paper3 wdt:P921 wd:Q202864 .
      ?paper3 wdt:P2860 ?paper4 .
      FILTER (?paper1 != ?paper4 ) 
      FILTER (?paper2 != ?paper4 )
      FILTER (?paper3 != ?paper4 )
      ?paper4 wdt:P921 wd:Q202864 .
      ?paper4 wdt:P2860 ?paper5 .
      FILTER (?paper1 != ?paper5 ) 
      FILTER (?paper2 != ?paper5 )
      FILTER (?paper3 != ?paper5 )
      FILTER (?paper4 != ?paper5 )
      ?paper5 wdt:P921 wd:Q202864 .
      ?paper5 wdt:P2860 ?paper6 .
      FILTER (?paper1 != ?paper6 ) 
      FILTER (?paper2 != ?paper6 )
      FILTER (?paper3 != ?paper6 )
      FILTER (?paper4 != ?paper6 )
      FILTER (?paper5 != ?paper6 )
      ?paper6 wdt:P921 wd:Q202864 .
      ?paper6 wdt:P2860 ?paper7 .
      FILTER (?paper1 != ?paper7 ) 
      FILTER (?paper2 != ?paper7 )
      FILTER (?paper3 != ?paper7 )
      FILTER (?paper4 != ?paper7 )
      FILTER (?paper5 != ?paper7 )
      FILTER (?paper6 != ?paper7 )
      ?paper7 wdt:P921 wd:Q202864 .
      ?paper7 wdt:P2860 ?paper8 .
      FILTER (?paper1 != ?paper8 ) 
      FILTER (?paper2 != ?paper8 )
      FILTER (?paper3 != ?paper8 )
      FILTER (?paper4 != ?paper8 )
      FILTER (?paper5 != ?paper8 )
      FILTER (?paper6 != ?paper8 )
      FILTER (?paper7 != ?paper8 )
      ?paper8 wdt:P921 wd:Q202864 .
      ?paper8 wdt:P2860 ?paper9 .
      FILTER (?paper1 != ?paper9 ) 
      FILTER (?paper2 != ?paper9 )
      FILTER (?paper3 != ?paper9 )
      FILTER (?paper4 != ?paper9 )
      FILTER (?paper5 != ?paper9 )
      FILTER (?paper6 != ?paper9 )
      FILTER (?paper7 != ?paper9 )
      FILTER (?paper8 != ?paper9 )
      ?paper9 wdt:P921 wd:Q202864 .
      ?paper9 wdt:P2860 ?paper10 .
      FILTER (?paper1 != ?paper10 ) 
      FILTER (?paper2 != ?paper10 )
      FILTER (?paper3 != ?paper10 )
      FILTER (?paper4 != ?paper10 )
      FILTER (?paper5 != ?paper10 )
      FILTER (?paper6 != ?paper10 )
      FILTER (?paper7 != ?paper10 )
      FILTER (?paper8 != ?paper10 )
      FILTER (?paper9 != ?paper10 )
      ?paper10 wdt:P921 wd:Q202864 .
      ?paper10 wdt:P2860 ?paper11 .
      FILTER (?paper1 != ?paper11 ) 
      FILTER (?paper2 != ?paper11 )
      FILTER (?paper3 != ?paper11 )
      FILTER (?paper4 != ?paper11 )
      FILTER (?paper5 != ?paper11 )
      FILTER (?paper6 != ?paper11 )
      FILTER (?paper7 != ?paper11 )
      FILTER (?paper8 != ?paper11 )
      FILTER (?paper9 != ?paper11 )
      FILTER (?paper10 != ?paper11 )
      ?paper11 wdt:P921 wd:Q202864 .
    }
    LIMIT 200
    

This feels horribly inefficient and still has many results for a chain length of 11, so I'm wondering what options there are to get at these questions in a more efficient manner. --Daniel Mietchen (talk) 23:39, 8 August 2018 (UTC)[reply]

I don't have a clue how this works, and it almost certainly contains loops, and it needs to be seeded with a paper, which is suboptimal; but I throw it in since it seems v.efficient and might just point us in a useful direction. Or maybe not. (See also this original use of this query to calculate descendants of Queen Elizabeth II).
select ?descendentLabel (count(?mid) as ?distance)  where { 
  ?descendent wdt:P921 wd:Q202864.
  ?descendent wdt:P2860* ?mid .
  ?mid wdt:P2860+ wd:Q22241243. 
  ?mid wdt:P921 wd:Q202864.
  filter (?descendent != ?mid)
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
group by ?descendentLabel 
order by desc(?distance) ?descendentLabel
Try it!
--Tagishsimon (talk) 01:34, 9 August 2018 (UTC)[reply]
Thanks — this seems a useful indeed. --Daniel Mietchen (talk) 04:31, 9 August 2018 (UTC)[reply]

Items with KLEKs ID but without coordinates

Hi,

I would like to have two lists: A list of cemeteries (Q39614) with P5556 (KLEKs ID), and a list of all items with P5556 (KLEKs ID) but without P625 (coordinate location).

Kind regards! --ChristianSW (talk) 10:58, 9 August 2018 (UTC)[reply]

@ChristianSW:.

SELECT DISTINCT ?item ?itemLabel ?klek
WHERE
{
?item wdt:P31/wdt:P279* wd:Q39614 . 
?item wdt:P5556 ?klek . 
  
  SERVICE wikibase:label { bd:serviceParam wikibase:language "de,en" . }
}
Try it!
SELECT DISTINCT ?item ?itemLabel ?klek
WHERE
{

?item wdt:P5556 ?klek . 
   MINUS { ?item wdt:P625 ?coord. }
  
  
  SERVICE wikibase:label { bd:serviceParam wikibase:language "de,en" . }
}
Try it!

. strakhov (talk) 11:53, 9 August 2018 (UTC)[reply]

@Strakhov: Thank you! --ChristianSW (talk) 11:56, 9 August 2018 (UTC)[reply]

Wikisource text available in most languages?

Do we have a query for that? Sample: The Decameron (Q16438) is available in 5 languages as Q16438#P747 links to five editions with a sitelink for a different wikisource version.
--- Jura 15:27, 9 August 2018 (UTC)[reply]

Now we hopefully do:
SELECT ?item (COUNT(DISTINCT ?lang) AS ?count) (GROUP_CONCAT(DISTINCT ?lang; separator=',') as ?languages) {
  ?item wdt:P747 ?edition .
  ?edition ^schema:about [ schema:isPartOf/wikibase:wikiGroup "wikisource"; schema:inLanguage ?lang ] .
} GROUP BY ?item ORDER BY DESC(?count) LIMIT 500
Try it!
Matěj Suchánek (talk) 17:04, 9 August 2018 (UTC)[reply]