Skip to main content
Tweeted twitter.com/StackDBAs/status/698232913742049280
added 4 characters in body
Source Link
Tom V
  • 15.7k
  • 7
  • 64
  • 87

My source data in a SqlSQL Server table has XML structured data. For each of the source rows I need to query the following sample xmlXML structure in a way that I get a calculated value of all A sub elements. For example the SUM of bb and product of cc * dd over all A elements. The number of A elements in the structure is unknown.

<root>
    <A>
        <bb>bb1</bb>
        <cc>cc1</cc>
        <dd>dd1</dd>
    </A>
    <A>
        <bb>bb2</bb>
        <cc>cc2</cc>
        <dd>dd2</dd>
    </A>
    <A>
        <bb>bb3</bb>
        <cc>cc3</cc>
        <dd>dd3</dd>
    </A>
</root>

I experiementedexperimented with sth.something like this, but it looks like not being the right way:

SELECT Convert(xml,myXmlCol).value('(/root/A[1]/bb)[1]', 'int') as MyResult FROM SourceTable

My source data in a Sql Server table has XML structured data. For each of the source rows I need to query the following sample xml structure in a way that I get a calculated value of all A sub elements. For example the SUM of bb and product of cc * dd over all A elements. The number of A elements in the structure is unknown.

<root>
    <A>
        <bb>bb1</bb>
        <cc>cc1</cc>
        <dd>dd1</dd>
    </A>
    <A>
        <bb>bb2</bb>
        <cc>cc2</cc>
        <dd>dd2</dd>
    </A>
    <A>
        <bb>bb3</bb>
        <cc>cc3</cc>
        <dd>dd3</dd>
    </A>
</root>

I experiemented with sth. like this, but it looks like not being the right way:

SELECT Convert(xml,myXmlCol).value('(/root/A[1]/bb)[1]', 'int') as MyResult FROM SourceTable

My source data in a SQL Server table has XML structured data. For each of the source rows I need to query the following sample XML structure in a way that I get a calculated value of all A sub elements. For example the SUM of bb and product of cc * dd over all A elements. The number of A elements in the structure is unknown.

<root>
    <A>
        <bb>bb1</bb>
        <cc>cc1</cc>
        <dd>dd1</dd>
    </A>
    <A>
        <bb>bb2</bb>
        <cc>cc2</cc>
        <dd>dd2</dd>
    </A>
    <A>
        <bb>bb3</bb>
        <cc>cc3</cc>
        <dd>dd3</dd>
    </A>
</root>

I experimented with something like this, but it looks like not being the right way:

SELECT Convert(xml,myXmlCol).value('(/root/A[1]/bb)[1]', 'int') as MyResult FROM SourceTable
deleted 292 characters in body
Source Link
Magier
  • 4.8k
  • 8
  • 47
  • 89

My source data in a Sql Server table has XML structured data. For each of the source rows I need to query the following sample xml structure in a way that I get all elements in a separate row in the resultcalculated value of all A sub elements. The numberFor example the SUM of bb and product of cc * dd over all A elements is unknown. In addition to that I need to have some The number of theA elements below (let's say bb and dd) as separate columns in the resultstructure is unknown.

<root>
    <A>
        <bb>bb1</bb>
        <cc>cc1</cc>
        <dd>dd1</dd>
    </A>
    <A>
        <bb>bb2</bb>
        <cc>cc2</cc>
        <dd>dd2</dd>
    </A>
    <A>
        <bb>bb3</bb>
        <cc>cc3</cc>
        <dd>dd3</dd>
    </A>
</root>

So the result in this sample should look like the following:

Row#1: | bb1 | dd1

Row#2: | bb2 | dd2

Row#3: | bb3 | dd3

This shows that the final result in a way has to "expand" depending of the number of elements in the source. Is this possible?

I experiemented with sth. like this, but it looks lokelike not being the right way:

SELECT Convert(xml,myXmlCol).value('(/root/A[1]/bb)[1]', 'int') as MyResult FROM SourceTable

My source data in a Sql Server table has XML structured data. For each of the source rows I need to query the following sample xml structure in a way that I get all elements in a separate row in the result. The number of A elements is unknown. In addition to that I need to have some of the elements below (let's say bb and dd) as separate columns in the result.

<root>
    <A>
        <bb>bb1</bb>
        <cc>cc1</cc>
        <dd>dd1</dd>
    </A>
    <A>
        <bb>bb2</bb>
        <cc>cc2</cc>
        <dd>dd2</dd>
    </A>
    <A>
        <bb>bb3</bb>
        <cc>cc3</cc>
        <dd>dd3</dd>
    </A>
</root>

So the result in this sample should look like the following:

Row#1: | bb1 | dd1

Row#2: | bb2 | dd2

Row#3: | bb3 | dd3

This shows that the final result in a way has to "expand" depending of the number of elements in the source. Is this possible?

I experiemented with sth. like this, but it looks loke not the right way:

SELECT Convert(xml,myXmlCol).value('(/root/A[1]/bb)[1]', 'int') as MyResult FROM SourceTable

My source data in a Sql Server table has XML structured data. For each of the source rows I need to query the following sample xml structure in a way that I get a calculated value of all A sub elements. For example the SUM of bb and product of cc * dd over all A elements. The number of A elements in the structure is unknown.

<root>
    <A>
        <bb>bb1</bb>
        <cc>cc1</cc>
        <dd>dd1</dd>
    </A>
    <A>
        <bb>bb2</bb>
        <cc>cc2</cc>
        <dd>dd2</dd>
    </A>
    <A>
        <bb>bb3</bb>
        <cc>cc3</cc>
        <dd>dd3</dd>
    </A>
</root>

I experiemented with sth. like this, but it looks like not being the right way:

SELECT Convert(xml,myXmlCol).value('(/root/A[1]/bb)[1]', 'int') as MyResult FROM SourceTable
Source Link
Magier
  • 4.8k
  • 8
  • 47
  • 89

Howto query xml

My source data in a Sql Server table has XML structured data. For each of the source rows I need to query the following sample xml structure in a way that I get all elements in a separate row in the result. The number of A elements is unknown. In addition to that I need to have some of the elements below (let's say bb and dd) as separate columns in the result.

<root>
    <A>
        <bb>bb1</bb>
        <cc>cc1</cc>
        <dd>dd1</dd>
    </A>
    <A>
        <bb>bb2</bb>
        <cc>cc2</cc>
        <dd>dd2</dd>
    </A>
    <A>
        <bb>bb3</bb>
        <cc>cc3</cc>
        <dd>dd3</dd>
    </A>
</root>

So the result in this sample should look like the following:

Row#1: | bb1 | dd1

Row#2: | bb2 | dd2

Row#3: | bb3 | dd3

This shows that the final result in a way has to "expand" depending of the number of elements in the source. Is this possible?

I experiemented with sth. like this, but it looks loke not the right way:

SELECT Convert(xml,myXmlCol).value('(/root/A[1]/bb)[1]', 'int') as MyResult FROM SourceTable