title: How to return all S->P->O triples from a starting resource to a specified path depth?
body: <p>My goal is to graphically represent the S->P->O relations within a depth two edges from the specified resource, <code>p:Person_1</code>. I want all relations within that path length to be returned from my query as <code>?s, ?p, ?o</code> for further processing in my graphical application.</p>
  
  <p>I tried the first query below which gives me my first set of <code>?s ?p ?o</code> with repeats, then <code>?p2, ?o2, ?p3, ?o3</code> as additional columns in the result. I want to bind <code>?p2</code> and <code>?p3</code> to <code>?p</code>, <code>?o2</code> and <code>?o3</code> to <code>?o</code>. </p>
  
  <pre><code>  SELECT *
      WHERE {
          p:Person_1 ?p ?o .
          BIND("p:Person_1" as ?s)
          OPTIONAL{
              ?o ?p2 ?o2 .
          }
          OPTIONAL{
              ?o2 ?p3 ?o3 .
          }
      }
  </code></pre>
  
  <p>Then, based on <a href="https://stackoverflow.com/questions/37186530/how-do-i-construct-get-the-whole-sub-graph-from-a-given-resource-in-rdf-graph">How do I construct get the whole sub graph from a given resource in RDF Graph?</a>, I tried using <code>CONSTRUCT</code> to return the graph. </p>
  
  <pre><code>PREFIX p: &lt;http://www.example.org/person/&gt; 
  PREFIX x: &lt;example.org/foo/&gt;
  
  construct { ?s ?p ?o }
  FROM &lt;http://localhost:8890/MYGRAPH&gt;
  where { p:Person_1 (x:|!x:)* ?s . 
          ?s ?p ?o . 
  }
  </code></pre>
  
  <p>I am using Virtuoso and I get the error:</p>
  
  <pre><code>Virtuoso 37000 Error SP031: SPARQL compiler: Variable ?_::trans_subj_9_3 in T_IN list is not a value from some triple
  </code></pre>
  
  <p>I could post-process the result from my first query but I want to learn how to do this correctly with SPARQL, preferably on Virtuoso.</p>
  
  <p>Update after testing the advice from @AKSW : 
  Both <code>CONSTRUCT</code> and <code>SELECT</code> statements work with the pattern suggested.</p>
  
  <pre><code>CONSTRUCT { ?s ?p ?o }
  FROM &lt;http://localhost:8890/MYGRAPH&gt;
  where { p:Person_1 (x:foo|!x:bar)* ?s . 
          ?s ?p ?o . 
  } LIMIT 100
  </code></pre>
  
  <p>and:</p>
  
  <pre><code>SELECT s ?p ?o 
  FROM &lt;http://localhost:8890/MYGRAPH&gt;
  where { p:Person_1 (x:foo|!x:bar)* ?s . 
          ?s ?p ?o . 
  } LIMIT 100
  </code></pre>
  
  <p>The <code>SELECT</code> results in several duplicates that cannot be removed using <code>DISTINCT</code>, which results in an error that I assume is due to the 'datatype' of some of the returned values. </p>
  
  <pre><code>Virtuoso 22023 Error SR066: Unsupported case in CONVERT (DATETIME -&gt; IRI_ID)
  </code></pre>
  
  <p>It appears some post-SPARQL processing is in order.</p>
  
  <p>This gets me most of the way there. Still hoping I can find a solution for SPARQL that is like Cypher's "number of hops away" :</p>
  
  <pre><code>OPTIONAL MATCH path=s-[*1..3]-(o)
  </code></pre>