0

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:

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?

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • you have to get the value from the same blank node representing a statement with the wanted rank: `SELECT DISTINCT ?id ?idLabel ?first_nameLabel WHERE { VALUES ?id {wd:Q254} . ?id p:P735 [pq:P1545 "2" ; ps:P735 ?first_name] ; SERVICE wikibase:label { bd:serviceParam wikibase:language "en" } }` – UninformedUser Oct 25 '21 at 15:16
  • or if you want the rank as well, `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" } }` - no `regex` necessary – UninformedUser Oct 25 '21 at 15:17
  • Thanks you so much for your answer @UninformedUser, it works! Could you turn your comment into an answer so that I can accept it? – ArcticBearing Oct 25 '21 at 17:50

1 Answers1

0
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" }           
    }
Matthias Winkelmann
  • 15,870
  • 7
  • 64
  • 76