title: How to group count a node in an XML column in a SQL table?
body: <p>I have some XML data stored in a column in a SQL table that I need to get all the variables for one of the XML items group counted.</p>
  <p>The query looks like this:</p>
  <pre><code>select fd.stdFormData 
  from formdata fd   
  where fd.FormType = '1X'
  </code></pre>
  <p>The returned data looks like this:</p>
  <p><a href="https://i.stack.imgur.com/WAOMk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WAOMk.png" alt="enter image description here" /></a></p>
  <p>I need to group count the results of <code>treatmentDiscussed</code> to get something that looks like this:</p>
  <p><a href="https://i.stack.imgur.com/hhP0q.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hhP0q.png" alt="enter image description here" /></a></p>
  <p>I'm pretty novice at working with XML data in a SQL Server table, so any assistance on how to proceed I would be grateful for. Thanks in advance.</p>
  <p>here is what I have so far as example. Unfortunatly I get a subquery error due to too may values returned</p>
  <blockquote>
  <p>Subquery returned more than 1 value. This is not permitted when the
  subquery follows =, !=, &lt;, &lt;= , &gt;, &gt;= or when the subquery is used as
  an expression.</p>
  </blockquote>
  <pre><code>declare @Data XML ,
  @treatmentReferrals varchar(50)   
  select   @Data  =  (select  fd.Data from  formdata fd   where  fd.FormType  =  '1X' ) 
  SELECT     @Data.value('(/root//treatmentDiscussed/node())[1]', 'nvarchar(max)') as treatmentDiscussed  
  select  @treatmentReferrals = (SELECT     @Data.value('(/root//treatmentDiscussed/node())[1]', 'nvarchar(max)') as treatmentDiscussed )
   CREATE TABLE #Data
  ( 
  treatmentReferrals VARCHAR(30) 
  ) 
  INSERT INTO #Data (treatmentDiscussed)
  VALUES (@treatmentDiscussed) 
   
   DROP TABLE #Data 
  </code></pre>