title: How to filter a variable by a property included in the variable in SPARQL?
body: <p>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.</p>
  <p>For example, let's say I want the second first name of Mozart (Chrysostom).</p>
  <p>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):</p>
  <pre><code>  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, &quot;2&quot;)
                          SERVICE wikibase:label { bd:serviceParam wikibase:language &quot;en&quot; }
                        }
  </code></pre>
  <p>However here the filter only applies to the <code>rank</code> variable, and not on the <code>first_name</code> variable:</p>
  <p>Query result:</p>
  <p><img src="https://i.stack.imgur.com/J72L5.png" alt="Query result" /></p>
  <p>I think that the problem comes from the fact that the <code>rank</code> property is a sub-element of the <code>first_name</code> property.  Would you know of a way to filter the <code>first_name</code> variable by the <code>rank</code> variable?</p>