Wikidata talk:SPARQL query service

Adding multiple identifiers to a URL in a SPARQL query edit

Following the description on Getting_urls_out_of_Wikidata_properties I added a link to a SPARQL query to the ASCL library catalogue based on the National_Thesaurus_for_Author_Names_ID (map view ; y622hrrw). The query works fine:

SELECT ?item ?itemLabel ?image ?placeofbirth ?placeofbirthLabel ?coord ?dob ?sitelink ?clickable_url WHERE {
  ?item wdt:P27 ?nationality.
  wd:Q27394 wdt:P527 ?nationality.
  ?item wdt:P21 wd:Q6581072.
  ?item wdt:P31 wd:Q5.
  ?item (wdt:P106/wdt:P279*) wd:Q36180.
  ?sitelink schema:about ?item; 
  schema:isPartOf <https://en.wikipedia.org/> .
  OPTIONAL {
    ?item wdt:P19 ?placeofbirth.
    ?placeofbirth wdt:P625 ?coord.
    ?item wdt:P1006 ?National_Thesaurus_for_Author_Names_ID.
    BIND(IRI(CONCAT("http://catalogue.leidenuniv.nl/primo_library/libweb/action/dlSearch.do?vid=UBL_V1&institution=UBL&tab=leiden&search_scope=lib_asc&query=any,contains,", ?National_Thesaurus_for_Author_Names_ID)) AS ?clickable_url)
  }
  OPTIONAL { ?item wdt:P18 ?image. }
  OPTIONAL { ?item wdt:P569 ?dob. }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
Try it!

I would like to modify the SPARQL query to make it search for two identifiers in the ASCL library catalogue (f.e. the National_Thesaurus_for_Author_Names_ID and the Library_of_Congress_authority_ID), but I don’t know how:  

The link with one identifier: http://catalogue.leidenuniv.nl/primo_library/libweb/action/dlSearch.do?vid=UBL_V1&institution=UBL&tab=leiden&search_scope=lib_asc&query=any,contains,‪071124152‬ (National_Thesaurus_for_Author_Names_ID: ‪071124152‬)

  The link with two identifiers: http://catalogue.leidenuniv.nl/primo_library/libweb/action/dlSearch.do?vid=UBL_V1&institution=UBL&tab=leiden&search_scope=lib_asc&query=any,contains,071124152%20OR%2080037754 (National_Thesaurus_for_Author_Names_ID: ‪071124152‬ ; Library_of_Congress_authority_ID: n80037754)

  Complicating factor: the query to the library catalogue should  only search the numbers of the Library of Congress identifier not the preceding letters (thus ‪80037754‬ and not n80037754))

Thanks in advance and kind regards, Walkuraxx (talk) 07:07, 7 April 2019 (UTC)Reply

@Walkuraxx: This, I think - presuming the initial character for the LoC is always "n".
SELECT ?item ?itemLabel ?image ?placeofbirth ?placeofbirthLabel ?coord ?dob ?sitelink ?clickable_url WHERE {
  ?item wdt:P27 ?nationality.
  wd:Q27394 wdt:P527 ?nationality.
  ?item wdt:P21 wd:Q6581072.
  ?item wdt:P31 wd:Q5.
  ?item (wdt:P106/wdt:P279*) wd:Q36180.
  ?sitelink schema:about ?item; 
  schema:isPartOf <https://en.wikipedia.org/> .
  OPTIONAL {
    ?item wdt:P19 ?placeofbirth.
    ?placeofbirth wdt:P625 ?coord.
    ?item wdt:P1006 ?National_Thesaurus_for_Author_Names_ID.
    ?item wdt:P244 ?Library_of_Congress_authority_ID.
    BIND(IRI(CONCAT("http://catalogue.leidenuniv.nl/primo_library/libweb/action/dlSearch.do?vid=UBL_V1&institution=UBL&tab=leiden&search_scope=lib_asc&query=any,contains,", ?National_Thesaurus_for_Author_Names_ID, "OR",strafter(?Library_of_Congress_authority_ID,"n"))) AS ?clickable_url)
  }
  OPTIONAL { ?item wdt:P18 ?image. }
  OPTIONAL { ?item wdt:P569 ?dob. }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
Try it!
--Tagishsimon (talk) 01:14, 8 April 2019 (UTC)Reply
@Tagishsimon: thank you very much. This is a big step forward. The majority of the LOC identifiers are now searchable in the ASCL catalogue together with the NTA. The LOC uses all in all four different prefixes for names: n,nb,nr,no. Any idea how to fix that? Kind regards, Walkuraxx (talk) 19:16, 8 April 2019 (UTC)Reply
@Walkuraxx: This should do it. (Also, short variable names next time, please :)
SELECT ?item ?itemLabel ?image ?placeofbirth ?placeofbirthLabel ?coord ?dob ?sitelink ?clickable_url WHERE {
  ?item wdt:P27 ?nationality.
  wd:Q27394 wdt:P527 ?nationality.
  ?item wdt:P21 wd:Q6581072.
  ?item wdt:P31 wd:Q5.
  ?item (wdt:P106/wdt:P279*) wd:Q36180.
  ?sitelink schema:about ?item; 
  schema:isPartOf <https://en.wikipedia.org/> .
  OPTIONAL {
    ?item wdt:P19 ?placeofbirth.
    ?placeofbirth wdt:P625 ?coord.
    ?item wdt:P1006 ?National_Thesaurus_for_Author_Names_ID.
    ?item wdt:P244 ?Library_of_Congress_authority_ID.
    bind(if(strstarts(?Library_of_Congress_authority_ID,"nb"),strafter(?Library_of_Congress_authority_ID,"nb"),if(strstarts(?Library_of_Congress_authority_ID,"nr"),strafter(?Library_of_Congress_authority_ID,"nr"),if(strstarts(?Library_of_Congress_authority_ID,"no"),strafter(?Library_of_Congress_authority_ID,"no"),strafter(?Library_of_Congress_authority_ID,"n")))) as ?loc)
    BIND(IRI(CONCAT("http://catalogue.leidenuniv.nl/primo_library/libweb/action/dlSearch.do?vid=UBL_V1&institution=UBL&tab=leiden&search_scope=lib_asc&query=any,contains,", ?National_Thesaurus_for_Author_Names_ID, "OR",?loc)) AS ?clickable_url)
  }
  OPTIONAL { ?item wdt:P18 ?image. }
  OPTIONAL { ?item wdt:P569 ?dob. }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
Try it!
--Tagishsimon (talk) 19:25, 8 April 2019 (UTC)Reply
@Tagishsimon: you made my day. Thank you very much! (I try to be more precise next time). Kind regards, Walkuraxx (talk) 17:55, 9 April 2019 (UTC)Reply

Adding multiple URLs to a SPARQL query edit

Following the description on Getting_urls_out_of_Wikidata_properties I added a link to a SPARQL query to the ASCL library catalogue based on the National_Thesaurus_for_Author_Names_ID (map view ; y622hrrw). The query works fine:

SELECT ?item ?itemLabel ?image ?placeofbirth ?placeofbirthLabel ?coord ?dob ?sitelink ?clickable_url WHERE {
  ?item wdt:P27 ?nationality.
  wd:Q27394 wdt:P527 ?nationality.
  ?item wdt:P21 wd:Q6581072.
  ?item wdt:P31 wd:Q5.
  ?item (wdt:P106/wdt:P279*) wd:Q36180.
  ?sitelink schema:about ?item; 
  schema:isPartOf <https://en.wikipedia.org/> .
  OPTIONAL {
    ?item wdt:P19 ?placeofbirth.
    ?placeofbirth wdt:P625 ?coord.
    ?item wdt:P1006 ?National_Thesaurus_for_Author_Names_ID.
    BIND(IRI(CONCAT("http://catalogue.leidenuniv.nl/primo_library/libweb/action/dlSearch.do?vid=UBL_V1&institution=UBL&tab=leiden&search_scope=lib_asc&query=any,contains,", ?National_Thesaurus_for_Author_Names_ID)) AS ?clickable_url)
  }
  OPTIONAL { ?item wdt:P18 ?image. }
  OPTIONAL { ?item wdt:P569 ?dob. }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
Try it!

I would now like to add a second url to my query, if possible. But I dont’ know how.

  Link 1: http://catalogue.leidenuniv.nl/primo_library/libweb/action/dlSearch.do?vid=UBL_V1&institution=UBL&tab=leiden&search_scope=lib_asc&query=any,contains, ‪071124152 ; ‬ National_Thesaurus_for_Author_Names_ID: ‪071124152‬

  Link 2 https://zoeken.beeldengeluid.nl/search/program?program.terms=nisv.person/person.name:103775 ; GTAA_ID: 103775  

[Link 3: http://d-nb.info/gnd/118718169 ; GND_ID: 118718169]

Thanks in advance and kind regards Walkuraxx (talk) 06:52, 7 April 2019 (UTC)Reply

link from data access page edit

Hey :)

Thanks for starting this. Can you please link/incorporate in Wikidata:Data access? Thanks a lot! --Lydia Pintscher (WMDE) (talk) 10:58, 16 September 2015 (UTC)Reply

@Lydia Pintscher (WMDE): I have added a link, diff Jheald (talk) 09:42, 17 September 2015 (UTC)Reply
Thanks! --Lydia Pintscher (WMDE) (talk) 22:11, 17 September 2015 (UTC)Reply

Logo? edit

How about a logo/icon for SPARQL query service? To make it easy to remember/distinguish from WDQ, Reasonator etc. The wikidata logo with a (white?) question mark?

--Atlasowa (talk) 14:43, 23 September 2015 (UTC)Reply

@Atlasowa: Thanks for the suggestion! This was turned into phab:T140577 and is being discussed there. --Deskana (WMF) (talk) 04:25, 20 July 2016 (UTC)Reply
@Deskana (WMF): Thank you! --Atlasowa (talk) 19:39, 20 July 2016 (UTC)Reply

Embed in map tutorial edit

Is there a tutorial that explains how to embed a query in the wikimedia map like this:

https://query.wikidata.org/embed.html#%23%20Points%20culminants%20par%20d%C3%A9partement%20fran%C3%A7ais%0A%23defaultView%3AMap%0ASELECT%20DISTINCT%20%3Fitem%20%3FitemLabel%20%3Fculminant%20%3FculminantLabel%20%3Faltitude%20%3FpartieDeLabel%20%3Fcoordinate%20%3Fimage%20%28concat%28str%28floor%28%3Faltitude%2F500%29%2a500%29%2C%20%27-%27%2Cstr%28ceil%28%3Faltitude%2F500%29%2a500%29%29%20%20as%20%3Flayer%29%0AWHERE%20%7B%0A%0A%20%20%3Fitem%20wdt%3AP31%2Fwdt%3AP279%2a%20wd%3AQ6465%20.%0A%20%20%3Fitem%20p%3AP31%20%3Fdep%0A%20%20FILTER%20NOT%20EXISTS%20%7B%20%3Fitem%20p%3AP576%20%3Fx%20%7D%20%0A%20%20FILTER%20NOT%20EXISTS%20%7B%20%3Fitem%20p%3AP582%20%3Fx%20%7D%20%0A%20%20FILTER%20NOT%20EXISTS%20%7B%20%3Fdep%20pq%3AP582%20%3Fx%20%7D%20%20%0A%20%20OPTIONAL%7B%0A%20%20%20%20%3Fitem%20wdt%3AP610%20%3Fculminant%20%0A%20%20%20%20OPTIONAL%7B%20%3Fculminant%20wdt%3AP2044%20%3Faltitude%20%7D%0A%20%20%20%20OPTIONAL%7B%20%3Fculminant%20wdt%3AP361%20%3FpartieDe%20%7D%0A%20%20%20%20OPTIONAL%7B%20%3Fculminant%20wdt%3AP625%20%3Fcoordinate%20%7D%0A%20%20%20%20optional%20%7B%20%3Fculminant%20wdt%3AP18%20%3Fimage%20%7D%0A%20%20%7D%0A%20%0A%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22fr%22.%20%7D%0A%20%20%0A%7D%20ORDER%20BY%20DESC%28%3Faltitude%29

Thanks! --Tobias1984 (talk) 10:01, 11 September 2016 (UTC)Reply

Actually I got it to work now. This is a nice example that shows how to put the labels into the layer-bar:
#Armenian Sister City
#defaultView:Map
SELECT ?item ?itemLabel ?sisterCity ?sisterCityLabel ?coordinate ?layer
WHERE {
  ?item wdt:P17 wd:Q399 .
  ?item wdt:P190 ?sisterCity .
  ?sisterCity wdt:P625 ?coordinate .
  ?sisterCity wdt:P17 ?sisterCountry .
  SERVICE wikibase:label { 
    bd:serviceParam wikibase:language "en" . 
    ?item rdfs:label ?itemLabel .
    ?sisterCity rdfs:label ?sisterCityLabel .
	?sisterCountry rdfs:label ?layer .
  }
}
Try it!

or the link to the map:

https://query.wikidata.org/embed.html#%23Armenian%20Sister%20City%0A%23defaultView%3AMap%0ASELECT%20%3Fitem%20%3FitemLabel%20%3FsisterCity%20%3FsisterCityLabel%20%3Fcoordinate%20%3Flayer%0AWHERE%20{%0A%20%20%3Fitem%20wdt%3AP17%20wd%3AQ399%20.%0A%20%20%3Fitem%20wdt%3AP190%20%3FsisterCity%20.%0A%20%20%3FsisterCity%20wdt%3AP625%20%3Fcoordinate%20.%0A%20%20%3FsisterCity%20wdt%3AP17%20%3FsisterCountry%20.%0A%20%20SERVICE%20wikibase%3Alabel%20{%20%0A%20%20%20%20bd%3AserviceParam%20wikibase%3Alanguage%20%22en%22%20.%20%0A%20%20%20%20%3Fitem%20rdfs%3Alabel%20%3FitemLabel%20.%0A%20%20%20%20%3FsisterCity%20rdfs%3Alabel%20%3FsisterCityLabel%20.%0A%09%3FsisterCountry%20rdfs%3Alabel%20%3Flayer%20.%0A%20%20}%0A}%0A

--Tobias1984 (talk) 19:42, 12 September 2016 (UTC)Reply

How to link to textual/ JSON data? edit

Let's say I want a script to retrieve the data from a query. So I don't want to click in a browser to select the download format for my query, but instead want to construct a URL to get the JSON/ CSV directly. How is this possible? Given the way the Javascript is minimised, I can't see what URL my browser is calling to get the data. Thanks in advance for any help, MartinPoulter (talk) 14:17, 13 September 2016 (UTC)Reply

the URL is: https://query.wikidata.org/bigdata/namespace/wdq/sparql?format=json&query=YOUR_QUERY --Pasleim (talk) 14:26, 13 September 2016 (UTC)Reply
A delayed thank-you to User:Pasleim. Cheers, MartinPoulter (talk) 16:25, 26 October 2016 (UTC)Reply

Wikidata Query Output edit

Can I choose the output format of the GUI tool? In addition to XML and JSON, can the GUI tool also output wiki formatted data (tables, lists, etc.) that I can simply paste into a wiki page? Thanks! SharkD (talk) 03:39, 24 March 2017 (UTC)Reply

missing links edit

Wikidata:SPARQL query service/Wikidata Query Help:

d1g (talk) 09:36, 29 April 2017 (UTC)Reply

How often is the data updated in wikidata query? edit

Hi, I tried to use the query, it states the query is updated 10 seconds ago. How long does the query reflect the edit I made in wikidata? --Napoleon.tan (talk) 12:58, 29 April 2017 (UTC)Reply

Usually about every three or four seconds, but it varies. 10 seconds is a slightly longer lag than usual, I think. Jheald (talk) 22:16, 29 April 2017 (UTC)Reply
In my experience, it may take minutes for edits be seen in queries.--Pere prlpz (talk) 21:06, 22 August 2020 (UTC)Reply

View from an external website edit

Moin Moin at all, I've there a query, which I have linked to an external website via iframe.

SELECT ?Kirche ?KircheLabel ?geographische_Koordinaten ?GeoNames_ID ?Bild WHERE {
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],de". }
  ?Kirche wdt:P31 wd:Q16970.
  ?Kirche wdt:P131 wd:Q23787499.
  OPTIONAL { ?Kirche wdt:P625 ?geographische_Koordinaten. }
  OPTIONAL { ?Kirche wdt:P1566 ?GeoNames_ID. }
  OPTIONAL { ?Kirche wdt:P18 ?Bild. }
}
LIMIT 100
Try it!

Now I don't want to have the view as a table, but directly the "Image Grid". Is there a parameter in iframe that I get directly "Image Grid"? Thanks in advance --Crazy1880 (talk) 20:44, 3 November 2017 (UTC)Reply

No items edit

I am trying to create this list and it brings a few names on the list generated on Wikidata Query.

However, when I copy the code to Wikipedia, it says there is "no items". Am I doing something wrong?—Teles «Talk to me˱M @ C S˲» 15:22, 2 December 2017 (UTC)Reply

Try ?item instead of ?singer
--- Jura 15:25, 2 December 2017 (UTC)Reply
@Jura1: This way?. Still the same.—Teles «Talk to me˱M @ C S˲» 15:32, 2 December 2017 (UTC)Reply
I saw your edit there and ListeriaBot just edited the page. Thanks!!—Teles «Talk to me˱M @ C S˲» 15:47, 2 December 2017 (UTC)Reply

SPARQL query service versus Wikidata Query edit

It seems like various places in Wikidata documentation call the tool at https://query.wikidata.org/ the "SPARQL query service", "Wikidata Query", and "Wikidata Query service". Do all of these names refer to the same tool? Blue Rasberry (talk) 20:20, 8 March 2018 (UTC)Reply

Proposed move edit

Propose move Wikidata:SPARQL query service -> Wikidata:Wikidata Query Service

The name for this tool is "Wikidata Query Service". Here are some use instances of that name:

I am not aware of any publication preferring "SPARQL query service" as the name for this interface. This is a proper noun so I favor capitalizing every letter as some of these sources do.

I do not know how moves work on Wikidata. I am posting here then asking for admin support. Blue Rasberry (talk) 17:46, 9 April 2018 (UTC)Reply

Ah - I see there's a lot of stuff sitting around under the top level page, including translations etc. We probably do want redirects though. Is there a tool that admins have access to for mass renaming like that? ArthurPSmith (talk) 08:30, 10 April 2018 (UTC)Reply
@MichaelSchoenitzer: Your proposal is best. Blue Rasberry (talk) 21:01, 1 August 2018 (UTC)Reply

Incorrect name propagating at Wikimedia Commons edit

At Wikimedia Commons I proposed a rename of a category containing images of output from this tool. Commons is calling this service's output the "Wikidata SPARQL data". I see this as incorrect and a result of the inconsistent use of the name here. I prefer "Wikidata Query Service" and to say that anything from it is "Wikidata Query Service output" or a "Wikidata Query Service data visualization". I appreciate anyone who can comment in Wikimedia Commons on what to call the illustrative image files which people are collecting to talk about this tool. Blue Rasberry (talk) 20:18, 8 August 2018 (UTC)Reply

@Bluerasberry: Over time there have been a number of Query Services against data from Wikidata, including Magnus's original Wikidata Query (WDQ), and various off-site services run against local replications of Wikidata. Hence the original emphasis that this was a (the?) SPARQL query service, as opposed to Magnus's service which used its own syntax, or other services offering other query APIs. Jheald (talk) 21:26, 9 August 2018 (UTC)Reply
@Jheald: The service at query.wikidata.org is the one that I identify as default and native.
I see two options -
  1. Keep this page named "SPARQL query service"
    1. This page remains named "SPARQL query service" and gives an overview of the many Wikidata tools which perform SPARQL queries
    2. We create a new page called "Wikidata Query Service" and make that the main documentation page for the particular tool at query.wikidata.org
  2. Rename this page to "Wikidata Query Service"
    1. Create a new page called "Wikidata tools for SPARQL queries"; put all content not related to the Wikidata Query Service there
    2. Reserve this page only for documenting the "Wikidata Query Service" at query.wikidata.org
Do you see other options? What do you prefer? Blue Rasberry (talk) 21:36, 9 August 2018 (UTC)Reply

Get rid of tag cloud edit

How do you get rid of the tag cloud from the default result display? This is just a waste of space and distracting. --Bamyers99 (talk) 22:23, 23 May 2018 (UTC)Reply

We are reverting it. The change will be deployed soon. Then it will be gone. Sorry for the trouble. It should not have gone out like this. --Lydia Pintscher (WMDE) (talk) 08:32, 24 May 2018 (UTC)Reply

Problems with embadding Commons Creator page on my Wikidata edit edit

Hi there,

if've created my Wikidata edit here: https://www.wikidata.org/wiki/Q54825941 but there is a misstake "This property is generally expected to contain only a single value." The help page doesn't help me. Can s.o. say me how to fix this? Thank you. --Taxiarchos228 (talk) 09:29, 6 June 2018 (UTC)Reply

Wikibooks edit

There's quite an extensive training package on SPAQRL being built by user:HenkvD over on English-Wikibooks: https://en.wikibooks.org/wiki/SPARQL

Can/should that be prominently featured on the page Wikidata:SPARQL query service/Wikidata Query Help? Wittylama (talk) 13:24, 7 February 2019 (UTC)Reply

@Lea Lacroix (WMDE): what do you think? Smalyshev (WMF) (talk) 19:00, 7 February 2019 (UTC)Reply

Name for the visualization suite? edit

I am trying to identify some documentation about the WDQS where I need to describe its features, and I want to separate the query function from the visualization function. The point is to describe these as separate interrelated features.

I think that in the wiki community we are all calling the query + visualization as the WDQS. Where is the specific documentation on just the visualization options? What do we call our package of visualizations? The Wikidata Visualization Suite? Thanks if anyone can either point to documentation or

speak up if you think visualization documentation does not exist!

Thanks Blue Rasberry (talk) 14:39, 21 July 2019 (UTC)Reply

  Resolved

It is Wikidata:SPARQL query service/Wikidata Query Help/Result Views, identified at special:diff/983392210. Wow, it is better developed than I expected! I will look around to see how easy it is to navigate to this page and think about why I missed it. Blue Rasberry (talk) 13:15, 22 July 2019 (UTC)Reply

Query timeout limit for important query reached edit

Hi! I need to perform this query in order to update our 32 Kinostarts pages. Here is for example Kinostarts 2019. Since August I've always been getting a timeout. Why is this the case? Any suggestions what to do? --Jobu0101 (talk) 21:55, 10 August 2019 (UTC)Reply

Hallo @Jobu0101:

das Problem ("java.util.concurrent.TimeoutException") ist der Stern-Operator ("/wdt:P279*") für eine beliebige (Null bis unendlich) Anzahl von Unterklassen von "Film":

[...] Die Lösung: ?objekt wdt:P31/wdt:P279* ?klasse. Dies heißt, dass es eine "Instanz von" und dann eine beliebige Anzahl von "Unterklassen von" Aussagen zwischen dem Datenobjekt und der Klasse.

?kunstwerk wdt:P31/wdt:P279* wd:Q838948. # Instanz von einer beliebigen Unterklasse von Kunstwerk

Siehe auch d:Wikidata:Contact_the_development_team#Query_that_timeouts,_I_really_don't_understand_why


Mein Anfangsverdacht war, dass die Query möglicherweise in eine Endlosschleife läuft, wenn A Unterklasse von B ist und B Unterklasse von A und in der Baumstruktur für Filme und deren Unterklassen irgendwo so eine Endlosschleife eingebaut wurde.

Diesen Verdacht konnte ich allerdings durch die Baumansicht der Klasse Film (vorerst) nicht bestätigen:


Lässt man in der Query "/wdt:P279*" weg, so erhält man alle Filme, aber nicht Unterklassen davon.

Beispielsweise fehlt

  • d:Q18517638 ... ist ein: "Animationsfilm"/"3D-Film" als Unterklasse von "Film".

Gibt man hingegen "/wdt:P279+" an, so erhält nur die Unterklassen von "Film", nicht aber die Instanzen von "Film" selbst.


Eine Lösungsmöglichkeit ist daher aus meiner Sicht beides mittels UNION zu kombinieren, also

  
  {
    ?film wdt:P31 wd:Q11424 .
  } 
    UNION 
  {
    ?film wdt:P31/wdt:P279+ wd:Q11424 .
  }

Die Query pro Jahr würde also wie folgt lauten:

SELECT DISTINCT ?film ?release ?filmLabel WHERE {
  { ?film wdt:P31 wd:Q11424. }
  UNION
  { ?film (wdt:P31/(wdt:P279+)) wd:Q11424. }
  ?film p:P577 ?release_statement.
  ?release_statement (psv:P577/wikibase:timePrecision) 11 ;
    ps:P577 ?release.
  FILTER((YEAR(?release)) = 2019 )
  ?release_statement pq:P291 wd:Q183.
  FILTER(NOT EXISTS { ?release_statement wikibase:rank wikibase:DeprecatedRank. })
  FILTER(NOT EXISTS { ?release_statement pq:P400 ?platform. })
  FILTER(NOT EXISTS { ?release_statement pq:P437 ?distribution. })
  FILTER(NOT EXISTS { ?film wdt:P31 wd:Q506240. })
  FILTER(NOT EXISTS { ?film wdt:P31 wd:Q21191270. })
  SERVICE wikibase:label { bd:serviceParam wikibase:language "de". }
}
ORDER BY (?release) (?filmLabel)
Try it!

https://w.wiki/779

Zusätzlich zur bisherigen Variante habe ich hier auch ein "DISTINCT" im SELECT ergänzt, um keine Mehrfacheinträge zu erhalten.


Für die aktuellen Kinostarts sieht das wie folgt aus:

SELECT DISTINCT ?film ?release ?filmLabel WHERE {
  { ?film wdt:P31 wd:Q11424. }
  UNION
  { ?film (wdt:P31/(wdt:P279+)) wd:Q11424. }
  ?film p:P577 ?release_statement.
  ?release_statement (psv:P577/wikibase:timePrecision) 11 ;
    ps:P577 ?release.
  FILTER((?release >= "2019-08-15"^^xsd:dateTime) && (?release < "2019-08-29"^^xsd:dateTime))
  ?release_statement pq:P291 wd:Q183.
  FILTER(NOT EXISTS { ?release_statement wikibase:rank wikibase:DeprecatedRank. })
  FILTER(NOT EXISTS { ?release_statement pq:P400 ?platform. })
  FILTER(NOT EXISTS { ?release_statement pq:P437 ?distribution. })
  FILTER(NOT EXISTS { ?film wdt:P31 wd:Q506240. })
  SERVICE wikibase:label { bd:serviceParam wikibase:language "de". }
}
ORDER BY (?release) (?filmLabel)
Try it!

https://w.wiki/778

--M2k~dewiki (talk) 00:02, 15 August 2019 (UTC)Reply


@M2k~dewiki: Vielen, vielen Dank! Ist das aber nicht merkwürdig, dass eine Vereinigung aus direkt film und +, welches mathematisch ja dasselbe wie * ist, so viel schneller bearbeitet wird? Gäbe es, wie von dir vermutet, eine Endlosschleife, so müsste diese doch auch bei + auftreten und zu einem Timeout führen, oder? --Jobu0101 (talk) 06:59, 15 August 2019 (UTC)Reply

Hallo Jobu0101, darum war die Endlosschleife in den Film-Unterklassen ja auch nur eine Theorie, die nicht verifiziert werden konnte. Das Problem tritt auch in vielen (allen?) anderen Fällen mit dem Stern-Operator auf (nicht nur bei Filmen), so funktionieren auch die Beispiele aus dem Wikidata:SPARQL_tutorial/de#Instanzen_und_Klassen nicht (mehr?). Oder auch Wikidata:Contact_the_development_team#Query_that_timeouts,_I_really_don't_understand_why: Dort ist als Antwort zu finden You used "*". Daher hatte ich es ohne * versucht. Unklar ist auch, warum es bis Ende Juli noch funktioniert hat, dann aber nicht mehr. Eine Theorie wäre eine Umstellung der Software oder in der Konfiguration. In den meta:Tech/News/2019/31 Tech-News konnte ich allerdings keinen *unmittelbaren* Hinweis auf eine diesbezügliche Änderung finden (wenn, dann in einem größeren Paket/Update "versteckt"). --M2k~dewiki (talk) 10:36, 15 August 2019 (UTC)Reply

wd:P27411 as result edit

Look at this query:

PREFIX p: <http://www.wikidata.org/prop/>
PREFIX v: <http://www.wikidata.org/prop/statement/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>

SELECT ?cineplex ?film ?filmLabel (GROUP_CONCAT(DISTINCT(?country)) AS ?countries) (MIN(?release) AS ?release) (MIN(?grelease) AS ?grelease) WHERE {
  ?film wdt:P3077 ?cineplex .
  ?film p:P577 ?release_statement . ?release_statement psv:P577/wikibase:timePrecision "11"^^xsd:integer . ?release_statement v:P577 ?release .
  ?film p:P577 ?grelease_statement . ?grelease_statement psv:P577/wikibase:timePrecision "11"^^xsd:integer . ?grelease_statement v:P577 ?grelease . ?grelease_statement pq:P291 wd:Q183 .
  OPTIONAL{?film wdt:P495 ?country} .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "de" }
}
GROUP BY ?cineplex ?film ?filmLabel
HAVING(year(?release)=1996 && year(?grelease)=1997 && day(?grelease)=30)
Try it!

Why do I not only get wd:Q27411 as result but also wd:P27411? I don't even know what wd:P27411 means. --Jobu0101 (talk) 07:00, 17 August 2019 (UTC)Reply

@M2k~dewiki: Have you seen such a thing before? --Jobu0101 (talk) 07:01, 17 August 2019 (UTC)Reply
Please also see Wikidata:Project_chat#References_to_non-existing_properties. --M2k~dewiki (talk) 14:40, 17 August 2019 (UTC)Reply
I think that this discussion is resolved and can be archived. If you disagree, don't hesitate to replace this template with your comment. 23:13, 26 August 2019 (UTC)

simple query timeout edit

Hello, this

SELECT ?item ?itemLabel WHERE {
  ?item wdt:P31 wd:Q8054 .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" . }
}
Try it!

hits 944k objects and presumably the label service is the reason for the timeout. Optimization via subsets however is not possible because any properties are too fine-grained (e.g. "found in taxon") or cannot be partitioned (external IDs). So how do I get a full list of protein objects and their labels? --SCIdude (talk) 14:36, 17 August 2019 (UTC)Reply

You may concider using this query
SELECT ?item ?itemLabel WHERE {
  ?item wdt:P31 wd:Q8054 .
  ?item rdfs:label ?itemLabel. FILTER( LANG(?itemLabel)="en" )
}
Try it!
This will only work if all items have an english label (or your language). HenkvD (talk) 16:00, 17 August 2019 (UTC)Reply
Runs without timeout, many thanks! I should put this solution on the optimization help page... --SCIdude (talk) 16:07, 17 August 2019 (UTC)Reply
I think that this discussion is resolved and can be archived. If you disagree, don't hesitate to replace this template with your comment. SCIdude (talk) 16:45, 17 August 2019 (UTC)Reply

How to remove day and month in timeline view edit

I created a timeline from a query where the ?date is only given with year-precision. The timeline view maps these years to January 1st. Is it possible to remove day and month in the display? -- JakobVoss (talk) 13:14, 27 August 2019 (UTC)Reply

Possible bug edit

I'm trying to select everything that has symptoms:

This should work for it:

SELECT ?item ?itemLabel ?linkTo {
  ?item wdt:P780/wdt:P31*/wdt:P279* wd:Q737460, wd:Q86, wd:Q21120251.
  SERVICE wikibase:label {bd:serviceParam wikibase:language "en" }
}
Try it!

But errors with

java.util.concurrent.ExecutionException: java.util.concurrent.ExecutionException: org.openrdf.query.QueryEvaluationException: com.bigdata.rdf.internal.NotMaterializedException: Vocab(2):XSDUnsignedInt(21167527)

Also tried

SELECT ?item ?itemLabel ?linkTo {
  hint:Query hint:optimizer "None" .
  ?item wdt:P780/wdt:P31*/wdt:P279* wd:Q737460, wd:Q86, wd:Q21120251.
  SERVICE wikibase:label {bd:serviceParam wikibase:language "en" }
}
Try it!

Gave error:

java.util.concurrent.ExecutionException: java.util.concurrent.ExecutionException: org.openrdf.query.QueryEvaluationException: com.bigdata.rdf.internal.NotMaterializedException: Vocab(2):XSDUnsignedInt(21167527)

Also tried:

SELECT ?item ?itemLabel ?linkTo { ?item wdt:P780/wdt:P31*/wdt:P279* wd:Q21120251.
  { SELECT ?item ?itemLabel ?linkTo { ?item wdt:P780/wdt:P31*/wdt:P279* wd:Q737460.
    { SELECT ?item ?itemLabel ?linkTo { ?item wdt:P780/wdt:P31*/wdt:P279* wd:Q86. { } } }
  } }
  SERVICE wikibase:label {bd:serviceParam wikibase:language "en" }
}
Try it!

Gave error:

java.util.concurrent.ExecutionException: java.util.concurrent.ExecutionException: org.openrdf.query.QueryEvaluationException: com.bigdata.rdf.internal.NotMaterializedException: TermId(567747892L)


Is it a bug or user error? Iwan.Aucamp (talk) 10:30, 21 September 2019 (UTC)Reply

@Iwan.Aucamp: User error. Quite some chutzpah to write dubious SPARQL, and when it fails to work, decide WDQS is buggy.
Also, you probably want to be on Wikidata:Request a query, fwiw.
Something like the following works; whether it's exactly what you want, I'm unsure. This is items which have all three of those symptoms.
SELECT ?item ?itemLabel ?linkTo {
  ?item wdt:P780 wd:Q737460;
        wdt:P780 wd:Q86;
        wdt:P780 wd:Q21120251.
  SERVICE wikibase:label {bd:serviceParam wikibase:language "en" }
}
Try it!
--Tagishsimon (talk) 10:46, 21 September 2019 (UTC)Reply
@Tagishsimon: I don't recall saying it is a bug, but my memory is not always great so apologies if I did (where though?). Can you clarify what I did wrong and what about my query was dubious? Your query would be simpler as just:
SELECT ?item ?itemLabel ?linkTo {
  ?item wdt:P780 wd:Q737460, wd:Q86, wd:Q21120251.
  SERVICE wikibase:label {bd:serviceParam wikibase:language "en" }
}
Try it!
And yes, it does work but it also does not accommodate for relations such as instance of (P31) subclass of (P279). I do want to take those relations into consideration though, I also want items where the item has a symptom that is a subclass or instance of wd:Q86 for example - which is why I have wdt:P780/wdt:P31*/wdt:P279*. Thanks for trying to help though. I did ask in Wikidata:Request a query also. Iwan.Aucamp (talk) 11:24, 21 September 2019 (UTC)Reply


Someone on IRC pointed out that this is similar to:
The following two queries (courtesy of bennofs) work fine:
SELECT ?item ?itemLabel ?linkTo {
  hint:Query hint:analytic "false".
  ?item wdt:P780/wdt:P31*/wdt:P279* wd:Q737460, wd:Q86, wd:Q21120251.
  SERVICE wikibase:label {bd:serviceParam wikibase:language "en" }
}
Try it!
SELECT ?item ?itemLabel {
  ?item wdt:P780/wdt:P31*/wdt:P279* wd:Q737460, wd:Q86, wd:Q21120251.
  OPTIONAL { ?item rdfs:label ?itemLabel FILTER(LANG(?itemLabel) = "en") }
}
Try it!
Iwan.Aucamp (talk) 11:42, 21 September 2019 (UTC)Reply

R code fix edit

R code fails it needs argument for agent wrong code is


SPARQL(endpoint,query)

correct code is

SPARQL(endpoint,query,curl_args=list(useragent=R.version.string))


see https://stackoverflow.com/questions/58560369/r-sparql-wikidata-opening-and-ending-tag-mismatch

who is in charge of the code tab and the suggestion?EncycloABC (talk) 15:23, 15 November 2019 (UTC)Reply

No idea who is in charge but the R tab should really be using WikidataQueryServiceR instead. – MPopov (WMF) (talk) 15:59, 17 November 2021 (UTC)Reply

Canonical endpoint edit

During cleanup of Scholia code we ran into "https://query.wikidata.org/bigdata/namespace/wdq/sparql" as oppose to "https://query.wikidata.org/sparql". I suppose we can safely remove all references to "https://query.wikidata.org/bigdata/namespace/wdq/sparql" and instead use "https://query.wikidata.org/sparql" as it looks more pretty. Or why is the "https://query.wikidata.org/bigdata/namespace/wdq/sparql" endpoiny mentioned prominently in this documentation? — Finn Årup Nielsen (fnielsen) (talk) 21:36, 7 February 2020 (UTC)Reply

Get query result directly into Google sheets edit

IS it possible to get the query result in a table format directly into a Google sheets.-❙❚❚❙❙ JinOy ❚❙❚❙❙ 08:45, 12 May 2020 (UTC)Reply

How would that be more direct than downloading CSV or TSV, and import into sheet? --SCIdude (talk) 09:02, 12 May 2020 (UTC)Reply
@SCIdude: The output data is one which is regularly been updated daily more than 10-20 times. So it will be difficult to update the sheet by downloading and importing to sheet. I have found a solution over Internet to import the data from Json to table format of live query result.-❙❚❚❙❙ JinOy ❚❙❚❙❙ 20:43, 12 May 2020 (UTC)Reply
Probably you can do that by coding in Google sheet. As far as I remember, Google sheets can include Javascript code and you could use the Javascript sample code provided in the query service.--Pere prlpz (talk) 22:02, 22 August 2020 (UTC)Reply

Information about properties edit

Why is there so little information returned when I query for information about properties? Here is an example for spouse (P26)

SELECT ?p ?o
WHERE {
  wdt:P26 ?p ?o .
}
Try it!

There should be more triples with properties in subject or object position. For example, this would be needed to retrieve the labels of properties. Wiljes (talk) 10:44, 20 June 2020 (UTC)Reply

@Wiljes: It is because you use the prefix wdt: you only get type information. Use instead the prefix wd: which is used to represent all entities (that is items, properties and lexemes). --Dipsacus fullonum (talk) 05:39, 23 June 2020 (UTC)Reply
@Dipsacus fullonum: Thank you. That solved it. Wiljes (talk) 20:26, 24 June 2020 (UTC)Reply

R code edit

The R code provided by the query has never worked for me. However, I found a workaround. I leave my code here just in case it may be useful for anybody else:

library(httr)
library(rjson)

treuvar <- function(var, bind) {
  sapply(bind, function(x) (x[[var]]$value))
}

desllista <- function(x) {
  x[sapply(x, is.null)] <- NA
  return(unlist(x))
}

getsparql <- function(url, coornum=TRUE) {
  cont <- fromJSON(rawToChar(content(GET(url))))
  nomsvars <- cont$head$vars
  llista <- lapply(nomsvars, treuvar, bind=cont$results$bindings)
  names(llista) <- nomsvars
  llista <- lapply(llista, desllista)
  df <- as.data.frame(llista, stringsAsFactors = FALSE)
  if (coornum) {
    if ("lat" %in% colnames(df)) {
      df$lat <- as.numeric(df$lat)
    }
    if ("lon" %in% colnames(df)) {
      df$lon <- as.numeric(df$lon)
    }
  }
  return(df)
}

getquery <- function(query) {
  url <- paste0("https://query.wikidata.org/sparql?query=", URLencode(query))
  return(getsparql(url))
}

#Example of usage
df <- getquery("SELECT ?item ?tipus ?mun
WHERE {
  ?item wdt:P17 wd:Q29.
    ?item wdt:P131* ?mun.
  ?mun wdt:P31 wd:Q33146843.
  ?item wdt:P31 ?tipus.
}
")

--Pere prlpz (talk) 21:53, 22 August 2020 (UTC)Reply

I recommend switching to WikidataQueryServiceR package:
:library(WikidataQueryServiceR) # install.packages("WikidataQueryServiceR")
:df <- query_wikidata("SELECT ?item ?tipus ?mun
:WHERE {
:  ?item wdt:P17 wd:Q29.
:  ?item wdt:P131* ?mun.
:  ?mun wdt:P31 wd:Q33146843.
:  ?item wdt:P31 ?tipus.
:}")
:
MPopov (WMF) (talk) 16:06, 17 November 2021 (UTC)Reply

Where do I find query helper? edit

The gentle introduction tells me I can edit an example query by using query helper, but I'm not seeing where I find query helper from the example screen? Sorry for being a stoopid noob. :) Valereee (talk) 15:29, 16 January 2021 (UTC)Reply

@Valereee: If we're talking https://query.wikidata.org/ it is the thing which takes up the left side of the page, togglable to be hidden or visible by an (i) symbol at the top of the list of icons on the far left. (I regret to say I have never found it useful, but your experience may be different.) I remind you that Wikidata:Request a query is an excellent place to ask any & all noob questions & generally hang out at to figure out SPARQL. Hope to see you there. --Tagishsimon (talk) 15:51, 16 January 2021 (UTC)Reply
Tagishsimon, I have put it on my watch, thanks! I've been meaning to figure out how to create a query for years. :) Valereee (talk) 16:03, 16 January 2021 (UTC)Reply

http -> https edit

Hi everyone, i saw that in the result set of the service the (wd-)items are in http, as for example here: [1] I think this should be changed to https. --Arnd (talk) 18:51, 1 February 2021 (UTC)Reply

You'd think, but WMF say too hard: https://www.wikidata.org/wiki/Wikidata:Contact_the_development_team/Query_Service_and_search#http_outputs_from_WDQS --Tagishsimon (talk) 19:23, 1 February 2021 (UTC)Reply

test.wikidata.org edit

How to query the Test Wikidata? Gikü (talk) 09:25, 23 August 2021 (UTC)Reply

@Gikü: I don't think that is currently possible. See phabricator T225245 or T250718. HenkvD (talk) 10:26, 25 August 2021 (UTC)Reply

title: edit

Hi y'all,

Am I looking wrong or is there no documentation about how to add a title to a query result with the comment #title:? (it is implicitely shown in some of the examples Wikidata:SPARQL_query_service/queries/examples but I guess it would desserve an explicit mention somewhere).

Cheers, VIGNERON en résidence (talk) 15:13, 6 December 2021 (UTC)Reply

I think I have read all the documentation on WDQS and I didn't know you could add a title to a query. I think it should have been mentioned in the User Manual. BTW the user manual is IMHO rather lacking. The parameters for the different kinds of result views are e.g. not really explained except for a few examples. --Dipsacus fullonum (talk) 16:41, 6 December 2021 (UTC)Reply
LucasW added #title: functionality on 1 June 2021 per https://phabricator.wikimedia.org/T225883 . I can't see that it's documented, at least, not on https://www.mediawiki.org/wiki/Wikidata_Query_Service/User_Manual . --Tagishsimon (talk) 16:51, 6 December 2021 (UTC)Reply
@Dipsacus fullonum, Tagishsimon: thanks for the confirmation. I'm guessing the best places would be to add it on both mw:Wikidata Query Service/User Manual and Wikidata:SPARQL_query_service/Wikidata_Query_Help/Result_Views, right? Following the "if you want something done, do it yourself" principle and since this a very simple code, I'll do it in the net days if there is no objection. Cheers, VIGNERON en résidence (talk) 09:45, 8 December 2021 (UTC)Reply
Good plan. --Tagishsimon (talk) 12:37, 8 December 2021 (UTC)Reply
Done here for MW: mw:Special:Diff/4960847, WDYT @Tagishsimon, Dipsacus fullonum:? Cheers, VIGNERON en résidence (talk) 09:44, 13 December 2021 (UTC)Reply

Name change to Wikidata Query Service edit

I propose changing the name of this page to Wikidata Query Service. Please comment at

Blue Rasberry (talk) 15:27, 26 January 2022 (UTC)Reply

I see support from the above discussion and made a request for move at the admin noticeboard and at Phabricator.
Bluerasberry (talk) 20:50, 27 February 2022 (UTC)Reply
@Bluerasberry: would it help if we moved the main page now and left all the examples for the sysadmins to do later? It doesn't seem that request is going to be done anytime soon — Martin (MSGJ · talk) 20:54, 27 May 2022 (UTC)Reply
@MSGJ: I am a bit stumped. It does seem like this move is entangled into 100s of related moves, which is unfortunate. If it is viable to only move the main page this yes, I support that and think it would be useful. Bluerasberry (talk) 21:15, 27 May 2022 (UTC)Reply

A question about the background map edit

I don't know if this is the right place for this question. If not, please guide me to the right one.
When I run a query with #defaultView:Map I get a background map with a lot of rendering errors in it. That name labels are partly missing or hidden I can live with. But when lakes like Mälaren (Q184492), Vänern (Q173596) and Vättern (Q188195) are rendered as land and not water it gets problematic. Especially when I work with lighthouses and need to see the shoreline. Remarkably Hjälmaren (Q211425) and smaller lakes show up just fine. All lakes are rendered just fine on openstreetmap.org, so there should not be any modeling error. I also note that Lake Huron (Q1383) and Lake Erie (Q5492) are also rendered correctly. Is there perhaps a limit on how many nodes a polygon can contain to render correctly? /ℇsquilo 11:10, 10 February 2022 (UTC)Reply

Strange. This query confirms it:
#defaultView:Map
SELECT ?lakes ?coordinates
WHERE
{
  VALUES ?lakes { wd:Q184492 wd:Q173596 wd:Q188195 wd:Q211425 }
  ?lakes wdt:P625 ?coordinates .
}
Try it!
You could make an error report at Wikimedia Phabricator or at Wikidata:Report a technical problem/WDQS and Search. --Dipsacus fullonum (talk) 12:47, 10 February 2022 (UTC)Reply

Looser results for pKa (P1117) edit

For the first time, I am trying to extract data from Wikidata. I'm looking for an alternative strong acid to do an alkylation and I thought I'd use wikidata to extract a list. When I use the query tool to find pKa's <-1 I only get 9 results. These include only one of the examples from superacids.

SELECT DISTINCT ?item ?itemLabel WHERE {
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]". }
  {
    SELECT DISTINCT ?item WHERE {
      ?item p:P1117 ?statement0.
      ?statement0 (psv:P1117/wikibase:quantityAmount) ?numericQuantity.
      FILTER(?numericQuantity < "-1"^^xsd:decimal)
    }
    LIMIT 100
  }
}

The issue seems to be inclusion of refences and error bars in the ChemBox entries, which Wikidata does not like. Is there any way of making the search looser, so that it ignores these? Alternatively, does the chembox need changing? --Project Osprey (talk) 09:59, 30 March 2022 (UTC)Reply

A Wikidata query returns results from Wikidata. If there are fewer results than you expected, it is because nobody entered the data into Wikidata. The design of the ChemBox template in the English Wikipedia has nothing to do with Wikidata, and doesn't affect a Wikidata query in any way. --Dipsacus fullonum (talk) 10:30, 30 March 2022 (UTC)Reply
Well I had assumed that wikidata automatically harvested data from ChemBox. That would imply that gaps indicate places where it encountered unexpected language and had just skipped over. Is that not the case? Is the data added manually? --Project Osprey (talk) 10:47, 30 March 2022 (UTC)Reply
There may be users who use bots or other tools to harvest data from the English Wikipedia or from Wikipedias in other languages. I don't know the status of this, and it isn't directly related to the SPARQL query service. I suggest that you ask at the Wikidata:WikiProject Chemistry page where people with knowledge of chemistry issues on Wikidata possibly can answer. --Dipsacus fullonum (talk) 11:01, 30 March 2022 (UTC)Reply

Delay in reflecting edits edit

Is there a noticable delay between an item edit and the moment is shows in a SPARQL query? IOW, how long should I wait before I see a straight change refelcted in query result? DePiep (talk) 11:12, 2 August 2022 (UTC)Reply

Nowadays, generally, about 100 seconds. However there are multiple report servers & their lag will bounce around a bit; and occasionally one or more will fall of its perch and develop hours of lag. See https://grafana.wikimedia.org/d/000000489/wikidata-query-service?orgId=1&refresh=1m&viewPanel=8 --Tagishsimon (talk) 15:16, 2 August 2022 (UTC)Reply
Thanks. I can be that patient. DePiep (talk) 14:28, 5 August 2022 (UTC)Reply

Numeric quantifiers in property paths? edit

I understand the *, +, and ? quantifiers in a path like wdt:P31/wdt:P279*. But is there any way to specify a numeric range, like {2,4}, that will match between two and four occurrences of the element? Swpb (talk) 16:27, 6 October 2022 (UTC)Reply

@Swpb: Seems not - https://www.w3.org/TR/sparql11-query/#propertypaths - but presumably one can use a combination of + and ? to achieve this - something like wdt:P31/wdt:P279/wdt:P279?/wdt:P279? ... not that I've put it to the test. --Tagishsimon (talk) 20:39, 6 October 2022 (UTC)Reply
Indeed. Some SPARQL engines do support the curly brackets notation for property paths, but not Blazegraph. There is however another way. To select fathers, grandfathers or great grandfathers you can use a path like wdt:P22/wdt:P22?/wdt:P22?, this is a functionally equivalent and portable way to specify a property path of finite length. Particularly useful for dealing with queries where deep hierarchies cause timeouts. Infrastruktur (talk) 02:48, 7 October 2022 (UTC)Reply
Thank you both, that's a good solution! Swpb (talk) 13:51, 11 October 2022 (UTC)Reply

How to get item #s without needing to find-and-replace entity URLs edit

Whenever I SELECT ?item, the ?item is a URL of form http://wikidata.org/entity/Q<num>. How would I get SPARQL to give me just Q<num>? RPI2026F1 (talk) 17:47, 5 January 2023 (UTC)Reply

As far as i know Sparql isnt designed to return only Q<num>. You would have to do this clientside e.g. per regex. R Focke (talk) 16:33, 20 April 2023 (UTC)Reply
Oh and you can directly use replace in the sparql query.
With eg: BIND(REPLACE(regex, "", ?item) as ?onlyqnumber)
Used here Wikidata:SPARQL query service/queries/examples#Humans who died on a specific date on the English Wikipedia, ordered by label R Focke (talk) 19:39, 22 April 2023 (UTC)Reply

Is the machine endpoint for sparql searches on wikidata (https://www.wikidata.org/sparql) correct? edit

I get only a page not found, when using that page.

I got sparql queries running, either with https://query.wikidata.org/sparql, when using pythons rdflib...SPARQLConnector.

Alternatively if i use a service function in a sparql search https://query.wikidata.org/ works fine too. R Focke (talk) 16:43, 20 April 2023 (UTC)Reply

The machine endpoint is https://query.wikidata.org/sparql. RPI2026F1 (talk) 16:45, 20 April 2023 (UTC)Reply

Math rendering error messages turning up in downloaded file edit

When running queries like this which return math output:

# All instances of the class "prime link" (Q77347966), recursively

SELECT DISTINCT ?item ?itemLabel ?itemDescription ?crossing_number ?gauss_notation ?knotatlas_id ?alexanderbriggs
WHERE {
  ?item wdt:P31 ?sub0 .  
  ?sub0 (wdt:P279)* wd:Q77347966  .
        OPTIONAL{?item wdt:P11035 ?crossing_number .}         # Crossing number
        OPTIONAL{?item wdt:P11034 ?gauss_notation .}          # Gauss notation
        OPTIONAL{?item wdt:P8426 ?knotatlas_id .}             # Knot Atlas ID
        OPTIONAL{?item wdt:P6432 ?alexanderbriggs .}          # Alexander-Briggs notation
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en,en"  }  
}

ORDER BY ASC(?item)
LIMIT 10000
Try it!

sometimes the TeX rendering fails, and there's an error message displayed in the page that looks something like this:

<math xmlns="http://www.w3.org/1998/Math/MathML"><merror><strong class="error texerror">Failed to parse (Conversion error. Server ("https://wikimedia.org/api/rest_") reported: "Cannot get mml. upstream connect error or disconnect/reset before headers. reset reason: connection termination"): {\displaystyle \{1,-9,2,-10\},\{9,-1,3,6,-5,-2,10,-3,-4,8,-7,5,-6,4,-8,7\}}</strong> </merror></math>

There is almost always at least one of these errors, usually several, in the displayed output data, perhaps unsuprisingly as hundreds of TeX renders are fired off in the process of rendering the page.

The problem is that this error message also appears in the downloaded JSON file instead of the expected error-free MathML; and worse, sometimes the data quoted in the error message does not match the actual data in the referenced item, presuably because of caching, so I can't even special-case the parser to extract the value from the error message.

Could this be fixed, please, and the correct value put in the JSON file even if the rendering request fails? It's a major barrier to my work on mathematical knots, as I have to do multiple passes to ensure everything is right, and manual checking on top to check that the multiple passes worked.

Cheers, The Anome (talk) 17:11, 18 October 2023 (UTC)Reply

I see now that there's a direct SPARQL API I can send queries to directly and get JSON out: I should have read the documentation here properly first: https://en.wikibooks.org/wiki/SPARQL/Wikidata_Query_Service I'll give that a go and see if it'm more robust. The Anome (talk) 00:06, 21 October 2023 (UTC)Reply
Return to the project page "SPARQL query service".