Shortcuts: WD:RAQ, w.wiki/LX

Wikidata:Request a query

From Wikidata
(Redirected from Wikidata:RAQ)
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 Jaber Abbas Alipour (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/07.

Agents linked to an instition (collection)

[edit]

Hi, I am trying to query and then visualize the people, i.e. staff members, collection agents and more connected to my institution which houses a large collection (http://www.wikidata.org/entity/Q233098).

The people are connected via: P108: employer P1416: affiliation P485: archives at P11146: collection items at

I have created this easy query but then the people are not linked... https://w.wiki/AcpG Since the Archive and Herbarium are part of the main institution, all people are connected to Q233098. Some are connected with each other (via the property 'significant person' as co-authors or co-collectors, some in academic relations (doctoral adivisor, doctoral student) but not so many yet. More work needs to be done. Occupations could maybe be useful to group the agents.

Any suggestions how best to visualize this in a graph of people connected to the museum? Thank you! S.v.Mering (talk) 13:04, 10 July 2024 (UTC)[reply]

Hello,
From what I know, in order to visualise the museums and links to the museums, you have to add the column with the museum in the SELECT, so each row would be a combination of a person and the museum it is linked to.
Here is an example based on your query: https://w.wiki/Aczp, which is not very elegant as I let it in different UNIONs and different variables. I did not know if you wanted to keep the exact result you already had (so the employer and affiliation properties only applies to the first museum, the archives at only to the second…), or wanted to have every properties for all museums. If that is the case, you could replace some properties’ links with an OR symbol (for example: ?item ((wdt:P108|wdt:P1416)/(wdt:P279*)) ?museumLinked. ) and the entities in the VALUES with the three museums. This does exactly the same but I joined some UNION together and called the variables under the same name which makes it more simple to read but you lose the kind of affiliation to the museum: https://w.wiki/Ac$o.
I am not very good in SPARQL, I hope it still helped you. --Bischnu (talk) 20:33, 10 July 2024 (UTC)[reply]
Thank you, Bischnu! This is helping me a lot already.
The issue with the archive and herbarium is that they are part of the same (main) museum that was used for affiliation/employer but I did not know how to include the parts of the organization in the original query (e.g. for 'collection items at').
As it is now, I can see three sub-graphs, which is also quite nice. I will need to play around a little and try it out.
Thank you for your help, very much appreciated! S.v.Mering (talk) 23:18, 10 July 2024 (UTC)[reply]

Match string in text property values

[edit]

How can we find sub-strings in values of author name string (P2093), like "Review By:" seen in this fix? Andy Mabbett (Pigsonthewing); Talk to Andy; Andy's edits 15:56, 11 July 2024 (UTC)[reply]

There don't seem to be any other example of the Review By problem, so I'll illustrate one approach to the report using the Charles E Griswold string:
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 "Charles E Griswold".
    ?item wikibase:apiOutputItem mwapi:title .
  }
  ?item wdt:P2093 ?string . 
  filter(regex(".*charles e griswold.*",lcase(?string)))
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
Try it!
--Tagishsimon (talk) 18:26, 11 July 2024 (UTC)[reply]
Hello,
I think that it can be done with the function CONTAINS. You would apply it somehow like this:
SELECT ?item ?ans WHERE {  
?item wdt:P31 wd:Q13442814;  
wdt:P2093 ?ans.  
FILTER(CONTAINS(?ans,"Review by:")).  
} LIMIT 10
Try it!
It is probably better to restrict on a smaller subset than all scholarly articles as I did there, this request does not succeed and times out. Also, if you are sure that the string you are looking for is always at the beginning, it is also probably better to replace “CONTAINS” by “STRSTARTS”; the latter normally needs less resources to compare the strings. --Bischnu (talk) 18:37, 11 July 2024 (UTC)[reply]
author name string (P2093) is used ~139M times, mostly on scholarly article (Q13442814) items. A query predicated on filtering 139M P2093 values is bound to fail given a 60s runtime. The reason that it is appropriate to approach this problem using MWAPI Search is that it can locate the small set of broadly qualifying items, after which a filter can be brought to bear on that small set. --Tagishsimon (talk) 19:32, 11 July 2024 (UTC)[reply]
OK thanks, great to know! I do not understand how it works. Does the MWAPI only look for the string in all the titles / item labels, after which we search for the author string in these items?
Also, I just had a look on what is MWAPI. I saw that you could find a string in all Wikipedia articles for example? How could something that huge not fail? Bischnu (talk) 19:48, 11 July 2024 (UTC)[reply]
It's a search engine - specifically CirrusSearch [1], based on w:en:Elasticsearch. Search engines are designed to index large text corpii and return useful results based on search strings. MWAPI is just a route to access Cirrus. I'm not sure offhand which parts of a wikidata item are indexed, but it's much more than labels and descriptions; certainly, P2093 is indexed, else we would not have got results in the above query. There's also an extension to Cirrus customised for wikidata - Extension:WikibaseCirrusSearch [2] which allows searches to combine text strings with statement values, so mwapi:srsearch "numismatic haswbstatement:P31=Q13442814"., for instance. --Tagishsimon (talk) 20:52, 11 July 2024 (UTC)[reply]

──────────────────────────────────────────────────────────────────────────────────────────────────── Thank you, Simon. Your example works, but a version searching for the string "Andy" finds zero resuts. Andy Mabbett (Pigsonthewing); Talk to Andy; Andy's edits 08:31, 12 July 2024 (UTC)[reply]

Yes. Odd. --Tagishsimon (talk) 11:47, 12 July 2024 (UTC)[reply]

Pipe symbol in REPLACE

[edit]

How can I specify the pipe symbol '|' in a REPLACE?

REPLACE(?entry,"\|","%7C")

returns

Query is malformed: Lexical error at line 48, column 23. Encountered: "|" (124), after : "\"\\" Vicarage (talk) 09:36, 12 July 2024 (UTC)[reply]
REPLACE(?entry,"\\|","%7C") --Tagishsimon (talk) 11:46, 12 July 2024 (UTC)[reply]
[edit]

Hi. Would it be possible to change this query to only take into account wikipedia sitelinks and not wikisource, commons, etc. ones? Thanks in advance, Paucabot (talk) 12:53, 12 July 2024 (UTC)[reply]

In what way? If you simply want to list entities that have at least one wikipedia sitelink you can do that by adding a semi-join: https://w.wiki/Aena . But if you want the count of sitelinks only to wikipedia that will require a lot more work. I might be too lazy to write the latter. Infrastruktur (talk) 12:35, 13 July 2024 (UTC)[reply]
I think I'm asking for the latter. Paucabot (talk) 20:29, 13 July 2024 (UTC)[reply]

Conflation query barely returns any results

[edit]

Not sure why this query is returning so few results. It only returns 29 items, but I expected it to return a lot more. Twin Star Exorcists (Q17228961) is one item that has both conflation (Q14946528) and anime television series (Q63952888), so not sure why it isn't part of the results too. Any help would be really appreciated!

SELECT ?item ?itemLabel (GROUP_CONCAT(?instanceOfLabel; SEPARATOR=", ") AS ?instanceOfValues) WHERE {
    ?item wdt:P31 wd:Q14946528.
    ?item wdt:P31 ?type.
    VALUES ?type { wd:Q63952888 wd:Q74262765 }
    ?item wdt:P31 ?instanceOf.
    ?instanceOf rdfs:label ?instanceOfLabel.
    FILTER(LANG(?instanceOfLabel) = "en")
    SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
  }
  GROUP BY ?item ?itemLabel
Try it!
Finnius00 (talk) 22:36, 12 July 2024 (UTC)[reply]

List of notable people whose last name is a common English noun

[edit]

I’m working on a word game and was looking for a list of “notable people” whose names are also nouns. I tried doing the query for it but it seems to be way over my limited Wikidata/SPARQL knowledge.

Here’s what I have so far:

# list of nouns
SELECT
  ?lexeme ?lexemeLabel
  ?lexical_category ?lexical_categoryLabel
WITH {
  SELECT ?lexeme ?lexemeLabel ?lexical_category WHERE {
    ?lexeme a ontolex:LexicalEntry ;
            dct:language wd:Q1860 ; 
            wikibase:lemma ?lexemeLabel .
    OPTIONAL {
      ?lexeme wikibase:lexicalCategory ?lexical_category .
    }
  }
  LIMIT 10000
} AS %results
WHERE {
  INCLUDE %results
  OPTIONAL {        
    ?lexical_category rdfs:label ?lexical_categoryLabel .
    FILTER (LANG(?lexical_categoryLabel) = "en")
  }
}
Try it!

and

# “celebrities”
SELECT DISTINCT ?lastNameLabel
WHERE {
  # Identify celebrities by their occupation
  ?celebrity wdt:P106 ?occupation;
             wdt:P734 ?lastName.
  
  # Filter for notable occupations
  VALUES ?occupation {
    wd:Q33999    # actor
    wd:Q639669   # musician
    wd:Q483501   # artist
    wd:Q36180    # writer
    wd:Q82955    # politician
    wd:Q2066131  # athlete
    wd:Q211236   # celebrity
  }

  # Get labels for readability
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
LIMIT 1000
Try it!
FiP (talk) 13:01, 19 July 2024 (UTC)[reply]

SPARQL query to sum number of seats of lower houses

[edit]

Hi all, I am a bit new to Wikidata, and to SPARQL even more!

I created Lower houses of the European Union (Q127603696) to specifically list all relevant lower houses in the EU (and the matching Upper houses of the European Union (Q127603941)). I am now trying to find a way to sum the number of seats (P1342) of each of these lower houses, to come up with a single number that I can use for composition boxes, such as in the infobox of this page European People's Party. This would avoid having to recalculate the total number of seats of lower/upper houses whenever one changes.

I tried my luck with the Wikidata_talk:SPARQL_tutorial and the Wikidata Query Service, and even asked on the live chat, but was not able to find the solution, which I believe should not be too complicated. Eventually, I posted on Wikidata:Project chat‎ and a kind soul pointed to this page.

A side question would be: how can I then use this query as a figure on Wikipedia (such as for the infobox above)?

Thanks in advance! Julius Schwarz (talk) 14:55, 19 July 2024 (UTC)[reply]