title: Using GROUP BY, COUNT and SAMPLE in apache jena SPARQL
body: <p>So I have an RDF schema that contains many "groups", and each of these groups has a "name", and contains a number of "elements". I need to select the name of every group, along with the number of elements for each. Here is a sample of a group RDF schema...</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;Group rdf:ID="group_actinoid"&gt;
      &lt;name rdf:datatype="&amp;xsd;string"&gt;Actinoid&lt;/name&gt;
      &lt;element rdf:resource="#Ac"/&gt;
      &lt;element rdf:resource="#Th"/&gt;
      &lt;element rdf:resource="#Pa"/&gt;
      &lt;element rdf:resource="#U"/&gt;
      &lt;element rdf:resource="#Np"/&gt;
      &lt;element rdf:resource="#Pu"/&gt;
      &lt;element rdf:resource="#Am"/&gt;
      &lt;element rdf:resource="#Cm"/&gt;
      &lt;element rdf:resource="#Bk"/&gt;
      &lt;element rdf:resource="#Cf"/&gt;
      &lt;element rdf:resource="#Es"/&gt;
      &lt;element rdf:resource="#Fm"/&gt;
      &lt;element rdf:resource="#Md"/&gt;
      &lt;element rdf:resource="#No"/&gt;
  &lt;/Group&gt;
  </code></pre>
  
  <p>...and here's the query I've been trying to get to work...</p>
  
  <pre><code>  1 PREFIX pt:&lt;http://www.daml.org/2003/01/periodictable/PeriodicTable#&gt;
    2 PREFIX rdfs:&lt;http://www.w3.org/2000/01/rdf-schema#&gt;
    3 PREFIX rdf:&lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#&gt;
    4 PREFIX xsd:&lt;http://www.w3.org/2001/XMLSchema#&gt;
    5
    6 SELECT (SAMPLE(?name) AS ?NAME) (COUNT(?elem) AS ?ELEMENTCOUNT)
    7 WHERE {
    8         ?group rdf:type pt:Group .
    9         ?group pt:name ?name .
   10         ?elem pt:element ?group .
   11       }
   12 GROUP BY ?group
  </code></pre>
  
  <p>...but I'm getting an empty result and I'm not really sure why. I <em>should</em> be getting a group name, along with however many elements that group contains, for every group in the owl file.</p>