title: Table to XML using TSQL with facet grouping and counts
body: <p>I have a requirement to turn a table into XML with counts for values. </p>
  
  <p>e.g. </p>
  
  <pre><code>Table
  Id , Type, AnotherType
  1, This, Widget
  2, This, Fimble
  3, That, Widget
  </code></pre>
  
  <p>I want the output something like this. This needs to be dynamic so new columns or facets added would generate the this type of output.</p>
  
  <pre><code>&lt;matrix name="Type"&gt;
   &lt;facet name = "this" count=2&gt;
     &lt;id value = 1&gt;&lt;/id&gt;
     &lt;id value = 2&gt;&lt;/id&gt;
   &lt;/facet&gt;     
   &lt;facet name = "that" count=1&gt;
     &lt;id value = 3&gt;&lt;/id&gt;
   &lt;/facet&gt;
  &lt;/matrix&gt;
  &lt;matrix name="AnotherType"&gt;
   &lt;facet name = "Widget" count=2&gt;
     &lt;id value = 1&gt;&lt;/id&gt;
     &lt;id value = 3&gt;&lt;/id&gt;
   &lt;/facet&gt;
   &lt;facet name = "Fimble" count=1&gt;
     &lt;id value = 2&gt;&lt;/id&gt;
   &lt;/facet&gt;
  &lt;/matrix&gt;
  </code></pre>
  
  <p>Is this possible?</p>
  
  <p>If the columns/values change, I'd like a solution that handles that - e.g it will generate a new matrix and facts dependant on the data.</p>