How to filter a variable by a property included in the variable in SPARQL? - sparql

I want to write a SPARQL query that would return the first name of a person based on the ranking of the name on Wikidata.
For example, let's say I want the second first name of Mozart (Chrysostom).
This is what I have so far (Mozart Wikidata ID is Q254, first name's property is P735, with P1545 giving the ordinal position of the name):
SELECT DISTINCT ?full_name ?full_nameLabel ?first_nameLabel ?rank
WHERE
{
VALUES ?full_name {wd:Q254} .
?full_name p:P735 [pq:P1545 ?rank] ;
p:P735 [ps:P735 ?first_name] ;
FILTER regex(?rank, "2")
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}
However here the filter only applies to the rank variable, and not on the first_name variable:
Query result:
I think that the problem comes from the fact that the rank property is a sub-element of the first_name property. Would you know of a way to filter the first_name variable by the rank variable?

SELECT DISTINCT ?id ?idLabel ?first_nameLabel ?rank
WHERE {
VALUES ?id {wd:Q254} .
?id p:P735 [
pq:P1545 ?rank;
ps:P735 ?first_name
]
FILTER(?rank = "2")
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}

Related

How can I pull the Wikidata description?

I'm trying to pull the wikidata description of biblical figures.
For example, for David, this would pull: king of Israel and Judah.
Here's what I started with:
select ?person ?personLabel
where {
?person wdt:P31 wd:Q20643955.
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
From Wikidata Query Service/User Manual ยง Label_service:
You can fetch the label, alias, or description [...] In automatic mode, you only need to specify the service template, e.g.:
PREFIX wikibase: <http://wikiba.se/ontology#>
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
and WDQS will automatically generate labels as follows:
If an unbound variable in SELECT is named ?NAMELabel, then WDQS produces the label (rdfs:label) for the entity in variable ?NAME.
If an unbound variable in SELECT is named ?NAMEAltLabel, then WDQS produces the alias (skos:altLabel) for the entity in variable
?NAME.
If an unbound variable in SELECT is named ?NAMEDescription, then WDQS produces the description (schema:description) for the
entity in variable ?NAME.
So you can just specify ?personDescription among your selected variables.
Here an example.

SPARQL - Concat values on multiple rows

I want a list of all currencies by El Salvador with their subdivision.
I use this query:
SELECT ?currency ?currencyLabel ?currencyIso4217 ?subdivisionLabel {
?currency wdt:P498 ?currencyIso4217 .
?currency wdt:P9059 ?subdivision .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
{
?country wdt:P38 ?currency .
BIND(wd:Q792 AS ?country).
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}
}
Try it here: Link
This gives the following result:
Works perfectly. But row 2 and three are the same currency. That currency only uses multiple names for the subdivision. I want them to concat so I get this output:
Is that possible?
Grouping in SPARQL works similar to SQL.
The function GROUP BY combines results that have identical values into groups. Afterwards, an aggregate function is applied to the other (non-identical) values for each group.
Typical aggregate functions are COUNT, SUM, MIN, MAX, AVG, GROUP_CONCAT, and SAMPLE.
For you GROUP_CONCAT is of interest. It performs a string concatenation across the values of a group. With the argument separator you can even specify a separator character. The order of the strings is arbitrary.
The syntax of GROUP BY and GROUP_CONCAT was already given to you in the comment by UninformedUser but I repeat it here in a slightly adapted form:
SELECT ?currency ?currencyLabel ?currencyIso4217 (GROUP_CONCAT(?subdivisionLabel; separator = ", ") as ?subdivisionLabels) {
BIND(wd:Q792 AS ?country).
?country wdt:P38 ?currency .
?currency wdt:P498 ?currencyIso4217 .
?currency wdt:P9059 ?subdivision .
?subdivision rdfs:label ?subdivisionLabel .
FILTER(lang(?subdivisionLabel) = 'en')
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
} GROUP BY ?currency ?currencyLabel ?currencyIso4217

How to follow a path depend on qualifier SPARQL

I like to query out all people who is connected to each other but filter by qualifier value of the path
For example, the query below will get all human related to Putin. But his spouse, whose in statement get the qualifier "endtime" should not be followed
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?pep ?pepLabel ?relation ?relationLabel ?relatedPerson ?relatedPersonLabel ?endtimequalifier
WHERE
{
VALUES ?pep {wd:Q7747}
?relatedPerson wdt:P31 wd:Q5.
?pep ?relation ?relatedPerson.
#What should I put here for the query to ignore the spouse since the endtimequalifier is available
OPTIONAL{
?pep p:P26 [ps:P26 ?spouse; pq:P582 ?endtimequalifier ].
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

Filtering URL value

What I am trying to do is to select a certain entity for the certain condition. If the condition is url, then how I can make a proper query to get the only result that matches the v1 = "https://www.getmagicbullet.com/" .
The commented sentences are what I tried different ways, but only to fail. How could I adjust the query to get the right answer?
Thank you for your help.
SELECT DISTINCT ?iLabel ?p ?v1
WHERE {
?i wdt:P31 wd:Q212920.
?i wdt:P856 ?v1.
# FILTER (?v1Label = "https://www.getmagicbullet.com/")
# { ?v1 rdfs:label "https://www.getmagicbullet.com/"#en }
# UNION { ?v1 skos:altLabel "https://www.getmagicbullet.com/"#en }
SERVICE wikibase:label { bd:serviceParam wikibase:language "ko,en,[AUTO_LANGUAGE]". }
}
LIMIT 1000

SPARQL - Extract Label from entity URI

I'm trying to extract a list of diseases that have symptoms from Wikidata.
The thing is, when I query I get a list of entity URIs, not a list of labels, for the Symptoms column.
My query, tested on here :
SELECT ?disease ?diseaseLabel (GROUP_CONCAT(?symptoms; SEPARATOR = ", ") AS ?Symptoms)
WHERE {
?disease wdt:P31 wd:Q12136.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
OPTIONAL { ?disease wdt:P780 ?symptoms. }
FILTER(EXISTS { ?disease wdt:P780+ ?symptom. })
}
GROUP BY ?disease ?diseaseLabel
Which gives the following result :
For example , at the disease measles ,what I want to select for the Symptoms column is : fever, cough, runny nose, maculopapular rash, lymphadenopathy, anorexia, diarrhea..
Which are the exact labels for the URIs in the Symptom column for the particular disease.
Any help/hint and suggestions are welcomed , thank you !