SQL query against XML columns

Hi All

I have a SQL 2012 table, where one of the columns is an XML data type.  Below is one of the values:
<items>
  <Counter CounterName="processed" CounterValue="70" />
  <Counter CounterName="deferred" CounterValue="1" />
  <Counter CounterName="delivered" CounterValue="70" />
  <Counter CounterName="sent" CounterValue="70" />
  <Counter CounterName="click" CounterValue="2" />
  <Counter CounterName="open" CounterValue="22" />
</items>

Open in new window

Question:  How can I write a query that displays all of the above as columns, such as ...  
SELECT 
   ??? as processed, 
   ??? as deferred, 
   ??? as delivered, --- etc. 
FROM mytable

Open in new window

Thanks in advance.
Jim
LVL 67
Jim HornMicrosoft SQL Server Data DudeAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Data DudeAuthor Commented:
Received an answer through another source
DECLARE @input TABLE (ID INT NOT NULL, XmlCol XML)

INSERT INTO @input VALUES(1, '<items>
  <Counter CounterName="processed" CounterValue="70" />
  <Counter CounterName="deferred" CounterValue="1" />
  <Counter CounterName="delivered" CounterValue="70" />
  <Counter CounterName="sent" CounterValue="70" />
  <Counter CounterName="click" CounterValue="2" />
  <Counter CounterName="open" CounterValue="22" />
</items>'), (2, '<items>
  <Counter CounterName="processed" CounterValue="170" />
  <Counter CounterName="deferred" CounterValue="11" />
  <Counter CounterName="delivered" CounterValue="170" />
  <Counter CounterName="sent" CounterValue="170" />
  <Counter CounterName="click" CounterValue="12" />
  <Counter CounterName="open" CounterValue="212" />
</items>')

SELECT
    ID,
    Processed = xc.value('(Counter[@CounterName="processed"]/@CounterValue)[1]', 'int'),
    Deferred = xc.value('(Counter[@CounterName="deferred"]/@CounterValue)[1]', 'int'),
    Delivered = xc.value('(Counter[@CounterName="delivered"]/@CounterValue)[1]', 'int'),
    [Sent] = xc.value('(Counter[@CounterName="sent"]/@CounterValue)[1]', 'int'),
    Click = xc.value('(Counter[@CounterName="click"]/@CounterValue)[1]', 'int'),
    [Open] = xc.value('(Counter[@CounterName="open"]/@CounterValue)[1]', 'int')
FROM    
    @input
CROSS APPLY
    XmlCol.nodes('/items') AS XT(XC)

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.