Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7869873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:27:06+00:00 2026-06-03T01:27:06+00:00

I have a table with an XML type column. This column contains a dynamic

  • 0

I have a table with an XML type column. This column contains a dynamic list of attributes that may be different between records.

I am trying to GROUP BY COUNT over these attributes without having to go through the table separately for each attribute.

For example, one record could have attributes A, B and C and the other would have B, C, D then, when I do the GROUP BY COUNT I would get A = 1, B = 2, C = 2 and D = 1.

Is there any straightforward way to do this?

EDIT in reply to Andrew’s answer:

Because my knowledge of this construct is superficial at best I had to fiddle with it to get it to do what I want. In my actual code I needed to group by the TimeRange, as well as only select some attributes depending on their name. I am pasting the actual query below:

WITH attributes AS (
  SELECT 
  Timestamp,
  N.a.value('@name[1]', 'nvarchar(max)') AS AttributeName,
  N.a.value('(.)[1]', 'nvarchar(max)') AS AttributeValue  
  FROM MyTable
  CROSS APPLY AttributesXml.nodes('/Attributes/Attribute') AS N(a)
)
SELECT Datepart(dy, Timestamp), AttributeValue, COUNT(AttributeValue)
FROM attributes
WHERE AttributeName IN ('AttributeA', 'AttributeB')
GROUP BY Datepart(dy, Timestamp), AttributeValue

As a side-note: Is there any way to reduce this further?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T01:27:07+00:00Added an answer on June 3, 2026 at 1:27 am
    WITH attributes AS (
      SELECT a.value('(.)[1]', 'nvarchar(max)') AS attribute
      FROM YourTable
      CROSS APPLY YourXMLColumn.nodes('//path/to/attributes') AS N(a)
    )
    SELECT attribute, COUNT(attribute)
    FROM attributes
    GROUP BY attribute
    

    CROSS APPLY is like being able to JOIN the xml as a table. The WITH is needed because you can’t have xml methods in a group clause.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that contains a column that is XML data type. I
I have created a table that has a strongly type XML column in SQL2008
I have mysql table that has a column that stores xml as a string.
I have XML data type as a table column. I have stored some XML
I have an XML data type column called tags. In that, I am storing
I have an XML hierarchy like this in an XML type column of a
I have a table with a column of type XML. When I insert a
I have a column of type 'nvarchar(max)' that should now hold XML information instead
I have a table having 2 columns EmployeeId (int) and EmployeeDetails(XMl type) EmployeeId EmployeeDetails
I have an oracle table with a column from type SYS.XMLTYPE and a storage

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.