Wikidata:Request a query

Latest comment: 9 hours ago by Herzi Pinki in topic Filter by state not valid any more (P582)

P549 duplication

edit

Hi guys !

It's been a while since I last bugged you with my requests.

I've been associating items of astronomers with their scientific articles items for a while now, and I noticed that several Mathematics Genealogy Project ID (P549) items are duplicates of existing ones (example : 1 and 2). I've been trying to automatically identify these duplicates for some time, but, of course, my query attempts on the subject time out (see below), with or without limits, with or without more identifiers for the non-549 items, etc..

Do you have any ideas on what could be done?

SELECT DISTINCT ?item1 ?item2 ?l1 ?l2
WHERE
{
	?item1 wdt:P31 wd:Q5 ;
	      wdt:P549 [] ;
          wikibase:identifiers 1 ;
          rdfs:label ?l1 .
   ?item2 wdt:P31 wd:Q5 ;
          wdt:P496 [] ;
          rdfs:label ?l2 .
       FILTER(LANG(?l1) IN ("en")).
       FILTER(LANG(?l2) IN ("en")).
  MINUS{?item2 wdt:P549 [].} .
       FILTER(?l1 = ?l2).
}
Try it!

Simon Villeneuve (talk) 12:07, 5 December 2024 (UTC)Reply

@Simon Villeneuve: Quitte à procéder en deux temps c'est plus simple, on peut commencer par lister les items avec des homonymes, et trouver les homonymes pour chacun d'entre eux. On trouve environ 8000 noms, certains avec des dizaines d'homonymes :
SELECT distinct ?item1 ?l1 WHERE {
  ?item1 wdt:P31 wd:Q5;
         wdt:P549 _:b101;
         wikibase:identifiers 1 ;
         rdfs:label ?l1.

    ?itemHomonym wdt:P31 wd:Q5;
           wdt:P496 _:b102;
           rdfs:label ?l1.
    FILTER((STR(?itemHomonym)) > (STR(?item1)))
    MINUS { ?itemHomonym wdt:P549 _:b103. }

  FILTER(LANG(?l1) IN("en"))
}
Try it!
Il n'est pas nécessaire de filter sur "?l1=?l2" on peut juste réutiliser "?l1" dans les deux motifs de graphes .
Je prépare un notebook pour aider à la suite si nécessaire. author  TomT0m / talk page 15:26, 5 December 2024 (UTC)Reply
Coucou,
Merci !
C'est la première fois que je vois une notation comme _:b101. Pourquoi utilises-tu celle-ci plutôt que les crochets ([]) ?
Sinon, en ajoutant ?itemHomonym dans les variables de début, j'arrive à 55 000 résultats.
Penses-tu que ces possibles homonymes méritent d'être listés quelque part ? Si oui, où ? Simon Villeneuve (talk) 16:24, 5 December 2024 (UTC)Reply
Salut, j'ai fait un notebook : https://public-paws.wmcloud.org/User:TomT0m/Nobels/Duplicate_mathematicians.ipynb (tout n'est pas listé, il y en a trop et si on affiche tout ça ne fonctionne pas, il faut recalculer dans son espace perso et virer ce que j'ai mis pour n'afficher que le début et la fin). Voir le tableau à la fin du notebook, il faudrait peut-être
Les syntaxes pour les nœuds blancs c'est pour nommer les nœuds et c'est juste parce que j'ai cliqué sur le diamand pour formater la requête dans l'éditeur, ça n'a pas d'importance particulière. Ça permet éventuellement d'identifier un nœud blanc dans la requête pour le réutiliser je crois (?). author  TomT0m / talk page 19:22, 5 December 2024 (UTC)Reply
@TomT0m: Coucou,
Je suis embêté par la ligne FILTER((STR(?itemHomonym)) > (STR(?item1))) de la requête. En effet, les numéros des éléments doublons du math genealogy project sont presque toujours supérieurs aux itemHomonym, alors que cette ligne demande de garder les numéros des itemHomonym supérieurs à ceux du MGP. J'ai reformulé la requête pour afficher les STR et c'est bel et bien cela. Simon Villeneuve (talk) 13:07, 15 December 2024 (UTC)Reply
P.S. : J'imagine que c'est parce que la requête explore les chiffres du numéro de l'élément de gauche vers la droite à partir du Q et que dès qu'elle tombe sur un chiffre plus grand d'un côté, elle le classe automatiquement plus grand que l'autre malgré que le chiffre global est plus petit. Par exemple, Q102441089 est considéré plus petit que Q39183791 parce que le premier chiffre à gauche du Q est 1 pour le premier et 3 pour le second. Je fais cette déduction en changeant le sens du < dans la requête.
Si c'est ça, alors il me semble y avoir un fail fondamental dans la fonction. Simon Villeneuve (talk) 13:13, 15 December 2024 (UTC)Reply

items from mi.wiki without en labels / descriptions

edit

Items from mi.wiki were imported a long time ago. Some of them never received en labels on descriptions. I would like a query that finds items with a link to mi.wiki and a label in mi but missing either an en label or en description. Stuartyeates (talk) 03:18, 7 December 2024 (UTC)Reply

@Stuartyeates:
select ?item ?article ?title ?label ?description (bound(?description) as ?has) {
  ?article schema:about ?item ;
           schema:isPartOf <https://mi.wikipedia.org/> ;
           schema:name ?title .
  
  #minus {
    SERVICE wikibase:label { 
      bd:serviceParam wikibase:language "mi".
      ?item rdfs:label ?label .
      ?item schema:description ?description
    }
  filter (!(bound(?label) && bound(?description))) .
}
Try it!
I think there are no missing label, if the query is right. There is a tool to do this in a user friendly way, terminator by @Magnus Manske: but it seems to be broken, see an attempt to set it up for your question. author  TomT0m / talk page 10:28, 7 December 2024 (UTC)Reply

Adding located in the administrative territorial entity in a query

edit

I have this query https://w.wiki/CLJV but need in addition located in the administrative territorial entity (P131) Pmt (talk) 17:24, 7 December 2024 (UTC)Reply

SELECT DISTINCT ?item ?itemLabel ?administration ?admLabel WHERE {
  {
    SELECT DISTINCT * WHERE {
      ?item p:P31 ?statement0.
      ?statement0 (ps:P31/(wdt:P279*)) wd:Q11315.
      ?item wdt:P17 wd:Q20;
            wdt:P131/wdt:P279* ?administration.
    }
    LIMIT 1000
  }
  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "[AUTO_LANGUAGE], en, sv, nb" .
    ?item rdfs:label ?itemLabel . ?item schema:description ?description .
    ?administration rdfs:label ?admLabel .
  }
}
Try it!

Here You go, regards, Piastu (talk) 15:24, 8 December 2024 (UTC)Reply

@Pmt:
#title: shopping centers in Norway
#defaultView:Map,Table
SELECT distinct ?item ?itemLabel ?itemDescription ?image ?coord ?verwaltungseinheitLabel ?streetLabel ?zip ?adress ?website
WITH {
 SELECT distinct ?region                   # subquery for all administrative territorial 
 WHERE {
 hint:Query hint:optimizer "None" .         
 ?region wdt:P131* wd:Q20.                 # set administrative territorial: Norway or other        
 MINUS { ?region wdt:P576 _:b0. }          # no end date of region
 }
} AS %region
WHERE {
  INCLUDE %region.
  ?item wdt:P131 ?region.      
  ?item (wdt:P31/wdt:P279*) wd:Q11315 .    # is a shopping center (or subcategory)
  MINUS { ?item wdt:P582  _:b1.}           # no end date
  MINUS { ?item wdt:P576  _:b2.}           # no demolished date
  MINUS { ?item wdt:P3999 _:b3.}           # no date of offical closure 
  MINUS { ?item wdt:P31 wd:Q19860854 }     # no destroyes buildings
  MINUS { ?item wdt:P31 wd:Q15893266 }     # no former entity
  OPTIONAL { ?item wdt:P18 ?image . }     
  OPTIONAL { ?item wdt:P625 ?coord. }  
  OPTIONAL { ?item wdt:P669 ?street. }  
  OPTIONAL { ?item wdt:P281 ?zip. }  
  OPTIONAL { ?item wdt:P6375 ?adress. }  
  OPTIONAL { ?item wdt:P856 ?website. }  
  OPTIONAL { ?item wdt:P131 ?verwaltungseinheit. }  
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]". }
}
shopping centers in Norway
This is my solution for you. You can set the administrative territorial by your self. Also you can set your focus (current shopping center, maybe cinemas or others). Best regards --sk (talk) 15:51, 20 December 2024 (UTC)Reply

Retrieve multi-field values in a single cell

edit

Hi! I have this query https://w.wiki/CNu4 , but the field ?job is multi-values. I would like to retrieve all values in ?job in a sigle cell separating them with a pipete (|). I have trying with this query but doesn't work. Any ideia? Thanks

SELECT ?item ?itemLabel ?genre ?genreLabel ?birthdate (GROUP_CONCAT(?jardueraLabel; SEPARATOR="|") AS ?jarduerak) ?herrialdeaLabel
WHERE 
{
  ?item wdt:P31 wd:Q5. # instance of (P31) human (Q5).
  ?item wdt:P10069 ?TabakaleraID. # with a Tabakalera Identities ID (P10069)
  ?item wdt:P21 ?genre.
  ?item wdt:P569 ?birthdate.
  ?item wdt:P106 ?jarduera.
  ?item wdt:P27 ?herrialdea.

  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }

GROUP BY ?item ?itemLabel ?genre ?genreLabel ?birthdate ?herrialdeaLabel
}
Try it!

 – The preceding unsigned comment was added by Lmerice (talk • contribs).

@Lmerice: I tried several stuffs with the Wikidata end point but trying to get all the labels seems to make the query timeout for some reason. There are several possibilities depending on what you are trying to do, like forgetting about the label and just get the items at first and get the label in a second time. For example ListeriaBot for WikidataList does not require getting the labels for them to be displayed in the end.
With another endpoint, qlever, it can be quite fast however, but we have to rewrite a bit because there is no label service, see this query on qlever, in "es" (and in "eu" language as you might prefer : https://qlever.cs.uni-freiburg.de/wikidata/9pxzcQ?exec=true notice the changed langtags) author  TomT0m / talk page 15:33, 11 December 2024 (UTC)Reply
@Lmerice: You can try this one, You don't have to search for humans (Q5), as P10069 (TabakaleraID) is only for them (i think so, cause only humans in WD have this ID):
SELECT DISTINCT ?item ?label ?TabakaleraID ?gender ?genderLabel ?birthdate (GROUP_CONCAT(DISTINCT ?joblabel; SEPARATOR="paste-separator-here") AS ?jobs) {
  {
    SELECT * {
      ?item wdt:P10069 ?TabakaleraID;
            wdt:P21 ?gender;
            wdt:P569 ?birthdate;
            wdt:P106 ?job.
      
      SERVICE wikibase:label {
        bd:serviceParam wikibase:language "[AUTO_LANGUAGE]" .
        ?job rdfs:label ?joblabel .
      }
    }
  }
  OPTIONAL {?gender rdfs:label ?genderLabel .
            FILTER(LANG(?genderLabel) = "[AUTO_LANGUAGE]")}
  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "[AUTO_LANGUAGE]" .
    ?item rdfs:label ?label .
#     ?gender rdfs:label ?genderLabel . #for some reason slower than 'optional-two-lines' before service
  }
}
GROUP BY ?item ?label ?TabakaleraID ?gender ?genderLabel ?birthdate
Try it!
, regards, Piastu (talk) 17:07, 11 December 2024 (UTC) (why pipe as separator is a problem? Piastu (talk) 17:08, 11 December 2024 (UTC))Reply
thanks, Just what I need! Lmerice (talk) 11:40, 12 December 2024 (UTC)Reply

Basketball players of single nationality, and no other professions

edit

Let's assume I would like to mass describe Polish basketball players as "Polish basketball player". I'm not sure if there is a way to create this query the way I want it. I can create a query for instance of Human, nationality Poland, gender male, profession basketball player (https://w.wiki/CPCN). But is there a way to exclude results that have more than one nationality, and more than one profession? Example of result I would like to avoid: https://www.wikidata.org/wiki/Q9369971 (more Ukrainian than Polish), https://www.wikidata.org/wiki/Q24074 (multi sport athlete whose main sport was hockey). Tupungato (talk) 20:30, 11 December 2024 (UTC)Reply

@Tupungato: Several actually : one is using the template {{Unique values}}, using it twice as something like {{Unique values|P106|?item}} in the following gives
select ?item {
  ?item wdt:P106 wd:Q3665646 .
  ?item wdt:P27 wd:Q36 .
  
     ?item wdt:P106  ?item_P106_val1
                        filter not exists { 
                          ?item wdt:P106  ?item_P106_val2
                        filter (?item_P106_val1 != ?item_P106_val2) .
                        }
  
     ?item wdt:P27  ?item_P27_val1
                        filter not exists { 
                          ?item wdt:P27  ?item_P27_val2
                        filter (?item_P27_val1 != ?item_P27_val2) .
                        }
}
Try it!
one over one is using a "group by / count" construction that counts the statements
select ?item {
  ?item wdt:P106 wd:Q3665646 .
  ?item wdt:P27 wd:Q36 .
  
  ?item p:P106 ?stmtCountry .
  ?item p:P27 ?stmtProfession .
  
} group by ?item having (count(?stmtCountry) = 1 && count(?stmtProfession) = 1)
Try it!
Both works and seem to give the same results. Maybe you would like to look at country for sport (P1532)   also. author  TomT0m / talk page 21:28, 11 December 2024 (UTC)Reply
Thank you for your help. I'll try these tomorrow. Problem with Property:P1532 (country for sport) is that it is rarely used, mostly in high profile contemporary athletes. Tupungato (talk) 21:43, 11 December 2024 (UTC)Reply

Is there a way to Query items that have no description in certain language?

edit

I've been dabbling in Query tool and QuickStatements to mass edit items. Adding mostly descriptions. I'm doing "safe" items where potential overwriting does no damage. For example adding Polish description "anglican church in Manchester" for anglican churches in Manchester. I would also like to have a look at some batches of items where most popular items may already have description that might be more meaningful. Is there a way to use query tool to generate a list/CSV of object of certain instance, but only those that have no description in certain language? For example instances of Q18618819 (national park of Australia) that have no Polish description (Dpl in QuickStatements commands)?

Or, to look at the issue from other side, is there a way to prevent QuickStatements from overwriting descriptions in items that already have description? --Tupungato (talk) 08:50, 12 December 2024 (UTC)Reply

Ok, I figured it out myself :) Here is a query for rivers in Iceland that miss Dutch description.
select ?item {
  ?item wdt:P31 wd:Q4022 .
  ?item wdt:P17 wd:Q189 .
  
  FILTER(NOT EXISTS {
?item schema:description ?itemdesc_nl.
    FILTER(LANG(?itemdesc_nl) = "nl") # with missing Dutch description
})
}
Try it!
Tupungato (talk) 12:41, 13 December 2024 (UTC)Reply

Map of people died by Mafia

edit

Hello there, I'm trying to design a map as told. I guess the relation is "killed by instance of human member of (collect mafia organizations)" I just arrived at map of people murdered. Anyone can help going further? Thanks in advance. PS: the better result would be a layered map by mafia type organization, similar to economists died in that era. Tassoman (talk) 21:03, 12 December 2024 (UTC)Reply

@Tassoman: An attempt :
# People died by homicide on a map
#defaultView:Map

SELECT distinct ?victim ?name ?deathplace ?coord ?date ?mafiaLabel ?murderer 
       ?murdererLabel 
       (?mafiaLabel as ?layer) # color by mafia
WHERE
{
  ?victim wdt:P31 wd:Q5;
          wdt:P1196 wd:Q149086;
          wdt:P570 ?date;
          wdt:P20 ?place .
  ?victim rdfs:label ?name FILTER (lang(?name) = "it") .
  ?place wdt:P625 ?coord .
  ?place rdfs:label ?deathplace FILTER (lang(?deathplace) = "it") .
  
  #?victim wdt:P157 ?org_or_member .
  
  # any organization of this type
  ?mafia wdt:P31/wdt:P279* ?type .
  values ?type {
    wd:Q18550    # mafia
    wd:Q1788992  # crim. org.
  }
  
  # Two possibilities here, handled in either branch of the following "union" 
  {
    ?murderer wdt:P463 ?mafia .  # The murderer is a member of some crim. org
    ?victim wdt:P157 ?murderer .

  } union {
    ?victim wdt:P157 ?mafia .    # The whole organisation is noted as murderer
  }                             
  
  service  wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],mul,en". }
}
ORDER BY DESC(?date)
Try it!
It's layered by criminal org. not by organisation type as the classification of mafias is probably not an easy topic. Actually to get results I had to generalize to "any criminal org.". I don' really want to get into the specifics myself. author  TomT0m / talk page 15:44, 13 December 2024 (UTC)Reply
Hello there, thank you for your example. I got better understanding of SparkQL. I also discovered that criminal organization have country as a regular attribute. But it's impossible to distinguish between terroristic organization and mafia organization. So your example takes in political terroristic organizations also, a little too much but still interesting. Tassoman (talk) 18:56, 13 December 2024 (UTC)Reply

Leonardo da Vinci as creator and author

edit

Hi, I have a Listeria list at en:User:Ham II/Leonardo with a SPARQL query which finds instances of Leonardo da Vinci (Q762) as creator (P170), but I would like it to find instances of Leonardo da Vinci (Q762) as author (P50) as well. How should the query be changed? Ham II (talk) 07:15, 15 December 2024 (UTC)Reply

@Ham II: Two propositions, one that is literally what you asked for, quite simple :
SELECT ?item WHERE { 
  ?item wdt:P170| wdt:P50 wd:Q762 
}
Try it!
It justs query for the claims that are built with one or the other property, just adding a wdt:P170|wdt:P50, thanks to sparql property paths.
The second is a bit more elaborate and more general, and might or might not fit your needs, it uses subproperty of (P1647)   to find all properties that are special cases of creators to hopfully find all kind of works a creator can do, extending it beyond paintings or literature :
SELECT ?item ?prop ?propLabel WHERE {
  ?item ?creator_claim wd:Q762  . # all item with a "creator like" property with Leo as value
  
  ?prop wdt:P1647* wd:P170 .                  # all subproperties of wd:P170
  ?prop wikibase:directClaim ?creator_claim . # use the "direct claim" form of statements

  service  wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],mul,en". }
}
Try it!
There are weird results in this, exhibitions Leo could not possibly have any part in because exhibited creator (P10661)   is somehow a subproperty of creator (P170)  , which could be discussed, I guess, but it did find other creations.
What do you think ? author  TomT0m / talk page 10:08, 15 December 2024 (UTC)Reply
@TomT0m: Thank you so much for this! I thought I only wanted a query which showed the artworks plus the manuscripts, but the second query is much more useful! A few false positives like the exhibitions don't matter for the purposes I'm using the list for. Ham II (talk) 10:30, 15 December 2024 (UTC)Reply

I created a fragment (partial sparql query) template for this, just in case : {{Creations}} author  TomT0m / talk page 09:52, 16 December 2024 (UTC)Reply

Filter by state not valid any more (P582)

edit
SELECT distinct ?id ?end
    WHERE {
    ?id wdt:P131* ?target.
    values ?target {wd:Q1741}
    ?id p:P131* ?t2 .
    minus {?t2 pq:P582 ?end . } # check if still (qualifier P582 to enddate)

    values ?id {wd:Q116955599} # filter for simpler test
  }
Try it!

How to get rid of all matches where the assignment to located in the administrative territorial entity (P131) = Vienna (Q1741) is not valid any more (by checking end time (P582))? In the case above the relevant match is the filtered Two Apostles Saints (Q116955599). best --Herzi Pinki (talk) 12:15, 16 December 2024 (UTC)Reply

SELECT distinct ?id ?end
    WHERE {
    ?id p:P131* ?statement.
    ?statement ps:P131* ?target .
    values ?target {wd:Q1741}
    OPTIONAL { ?statement pq:P582 ?end . }
    FILTER ( !bound(?end) )

    values ?id {wd:Q116955599} # filter for simpler test
  }
Try it!
learned more: this will do the required thing. thanks --Herzi Pinki (talk) 22:23, 18 December 2024 (UTC)Reply
the solution above works to filter away items with end time (P582), but it ignores the path down located in the administrative territorial entity (P131) and matches only items directly in ?target, not in subpaths thereof. Still need help. --Herzi Pinki (talk) 15:32, 19 December 2024 (UTC)Reply
@Herzi Pinki I'm not sure this is possible with arbitrary long paths actually. This is the kind of cases it would be very convenient to have all "non historical" locations actually at the "best ranks" statements, it would be way easier.
It's easy to get all the statements on any paths from ?id to ?target that have an end date, this query works : https://w.wiki/CUUE for example.
From there there might not really something that works. We would like to express "there exists a path from ?id to ?target such that no ?statement along the path contains one on the statements of the previous query". But here we got a problem because say there is a path
"?id" P131=> "ancient location (with end date)" P131=> "something actual (without end date)" P131=> "Vienna"
If we want to apply the approach I took above, with an intermediary (sketch of query) ?id P131* ?intermediary . ?intermediary P131* "vienna" we got two matches for this query for ?intermediary, "ancient location" and "something actual", in our result set. Filtering out to remove all the "end date" statements will remove only one of the two "ancient location", so we still have a matching path to go from ?id to "Vienna" although one edge has an "end date".
We could remove all pairs "?id" "?target" for which there exists a path with an edge with an end date but … that would remove too much results as there also could be a path without an edge !
I think there is a way if we put a bound in the number of steps however because we can check every steps by checking their statements. I'll try to propose something, like, say, in the following days. author  TomT0m / talk page 17:43, 20 December 2024 (UTC)Reply
Thanks @TomT0m: for caring. Full of hope. --Herzi Pinki (talk) 20:47, 20 December 2024 (UTC)Reply

Federated query with IDREF

edit

I tried this federated query in many ways but I always get an error "Status Code=500, Status Line=SPARQL Request Failed, Response=Virtuoso S0002 Error SQ200: No table DB.DBA.SPARQL_BINDINGS_VIEW_C_0". Any ideas? I started from User:Difool/queries#Query_IdRef which works well.

SELECT DISTINCT ?item ?itemLabel ?start ?end ?idref ?idrefurl ?pref ?name
WHERE {
  ?item p:P39 ?st . ?st ps:P39 wd:Q130613200 .
  OPTIONAL { ?st pq:P580 ?sd } . BIND(YEAR(?sd) AS ?start)
  OPTIONAL { ?st pq:P582 ?ed } . BIND(YEAR(?ed) AS ?end)
  OPTIONAL { ?item wdt:P269 ?idref } .
  BIND(URI(CONCAT("http://www.idref.fr/",?idref,"/id")) AS ?idrefurl)
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" . }
  SERVICE <https://data.idref.fr/sparql> {
    SELECT DISTINCT ?idrefurl ?pref ?name WHERE {
      ?idrefurl rdf:type foaf:Person; skos:prefLabel ?pref; foaf:name ?name.
    }
  }
}
Try it!

Thanks, Dìdaxis (talk) 11:22, 19 December 2024 (UTC)Reply

@Dìdaxis: This one seems to work :
SELECT DISTINCT ?item ?itemLabel ?start ?end ?idref ?idrefurl ?pref ?name
WHERE {
  ?item p:P39 ?st . ?st ps:P39 wd:Q130613200 .
  OPTIONAL { ?st pq:P580 ?sd } . BIND(YEAR(?sd) AS ?start)
  OPTIONAL { ?st pq:P582 ?ed } . BIND(YEAR(?ed) AS ?end)
  OPTIONAL { 
    ?item wdt:P269 ?idref .
      
    BIND(URI(CONCAT("http://www.idref.fr/",?idref,"/id")) AS ?idrefurl)
    SERVICE <https://data.idref.fr/sparql> {
      ?idrefurl rdf:type foaf:Person; skos:prefLabel ?pref; foaf:name ?name.
    }
  } .

  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" . }

}
Try it!
I wrapped the binding and the service call into the optional because it's no need to try to get something if the idref is not bound. The subquery into the call is probably also not needed because of how subqueries works, they probably get the whole dataset before trying to filter with the external ?idrefurl (the dataflow is from the inside to the outside, the outside variables values may not be taken into account at first), so this is probably wildly inefficient anyway. Here you have a limited numbers of idrefs to get so better pass them to the service, I guess. The limit is not really needed I guess. author  TomT0m / talk page 11:57, 19 December 2024 (UTC)Reply
Addendum :
  • A subquery without limits when the service is called inside the optional works fine also, it's unneeded but the idref server seems to understand the fact that it does not need to compute the whole result set
  • A subquery with limits when the service is called inside the optional works fine also does take more time and does not returns any results. This is because the semantics is different, first it takes 100 (limit) results, then it checks if it fits with the ?idrefurl values given by the service call. As it's unlikely that they are correlated, it fails.
  • If the service call is outside the optional AND the bind is inside the optional, it also works, with a subquery or not, without limit
  • if the bind is outside the optional, it fails. The problem seems to arise when there may be unbound ?idref or ?idrefurl, although in practice in our case there should not be if the call is done after the optional … but we cannot guarantee that it is done ! Actually the service seems to be called before the "optional" are computed if it is outside, so it may pass unbound values to the virtuoso service, which seems to be impossible. adding a blazegraph hint to run it last makes it works, which confirms the issue.
author  TomT0m / talk page 12:23, 19 December 2024 (UTC)Reply

Query all images for an element

edit

I want to query all images for an element. In the example here, Q129029 has 2 images, but the query result has only 1 line, and a single image.

SELECT
  ?item ?wdimage
WHERE {
  { ?item wdt:P31 wd:Q977367 }
  { ?item wdt:P18 ?wdimage. }
  FILTER (?item = wd:Q129029).
}
Try it!

Any ideas on what's missing here? Thanks in advance. Pruna.ar (talk) 12:29, 19 December 2024 (UTC)Reply

@Pruna.ar: Yes. You are using the "truthy" statements which does only include best ranks statements and only allows to get the main statement value. To get all the values you have to use the "full" forms which also allows to access to the qualifiers, sources, ranks …
In the rdf export doc the first can be searched for "claims" properties and "statement" properties for the second if you want more informations.
I included the rank in the query for explanation :
SELECT
  ?item ?wdimage ?rank
WHERE {
   ?item wdt:P31 wd:Q977367 ;
         p:P18 [
           ps:P18 ?wdimage 
           ; wikibase:rank ?rank 
         ] .
  values ?item { wd:Q129029 } .
}
Try it!
You can remove or comment the ; wikibase:rank ?rank to remove the ranks. Here you have one "preferred" and one "normal" rank. By default, if there are preferred ranks statements those are shown using "wdt:" properties, not the "normal" one. If there are no "preferred" ranks, the "normal" one are shown. The "deprecated" one are never shown by default. See Rank.
To remove the "deprecated" one for the previous query we can use a filter or a values :
SELECT
  ?item ?wdimage ?rank
WHERE {
   ?item wdt:P31 wd:Q977367 ;
         p:P18 [
           ps:P18 ?wdimage 
           ; wikibase:rank ?rank 
         ] .
  values ?item { wd:Q129029 } . # allowed value for ?item
  values ?rank { wikibase:PreferredRank wikibase:NormalRank } # allowed values for rank
  # or would also works
  # filter (?rank != wikibase:DeprecatedRank)
}
Try it!
author  TomT0m / talk page 13:00, 19 December 2024 (UTC)Reply
Awesome explanation @TomT0m! Thanks a lot, it solved my problem and also taught me something new :-) Pruna.ar (talk) 18:27, 20 December 2024 (UTC)Reply