I have the following xml. How can I keep the value of each cond-id and apply it to each subsequent price-container element before encountering a new cond-id with out creating duplicate rows.
I have tried several things and this is the closest I have come.
declare @xml xml =
'-<module mod-id="333">
<title>NNN NNNNN NNNNN</title>
-<data-code>
<code-id>333-004</code-id>
<description>XXX XXX XXXXXXXX XXX X XXXXXXXXXXXXXXXX</description>
-<applic>
-<p32>
-<condition cond-id="o-000008888">
<ctext> NNN NNNNNNNNN NN NNNNN NNNNNNNNN NN</ctext>
</condition>
-<m234 domicile="all" ver-start="4.40" target-ver="4.40" vocation="all">
-<value>
<price-container pwdb-id="p-000121212">267</price-container>
<weight-container pwdb-id="w-000676767">0/40</weight-container>
</value>
</m234>
</p32>
</applic>
</data-code>
-<data-code>
<code-id>333-005</code-id>
<description>NNNNNNNNNNNNN NNNNNNNNNN NNNNN N NNNN N NNN NNNNNNNN</description>
-<applic>
-<p32>
-<condition cond-id="o-000002222">
<ctext> NNNNNN NNNNN XXXX NNNN XXXX NNNNNNNN XXXXXXXX </ctext>
</condition>
-<m234 domicile="all" ver-start="4.40" target-ver="4.40" vocation="all">
-<value>
<price-container pwdb-id="p-000123456">N/C</price-container>
<weight-container pwdb-id="w-000234567">0/0</weight-container>
</value>
</m234>
-<condition cond-id="o-000033333">
<ctext> Price with DC 622-005 , DC 622-197, , and DC 622-292 </ctext>
</condition>
-<m234 domicile="all" ver-start="4.80" target-ver="4.80" vocation="all">
-<value>
<price-container pwdb-id="p-000456789">99999</price-container>
<weight-container pwdb-id="w-000789012">0/0</weight-container>
</value>
</m234>
</p32>
</applic>
</data-code>
</module>'
select
price.value('(../../../../../code-id)[1]', 'varchar(50)') as data_code,
price.value('@pwdb-id', 'varchar(50)') as pwdb_id,
price.value('(text())[1]', 'varchar(50)') as text,
cond.value('(@cond-id) [1]', 'varchar(50)') as cond_id
from @xml.nodes('/module/data-code/applic/p32') as Xtble(datanode)
cross apply Xtble.datanode.nodes('m234/value/price-container') as Xtble2(price)
outer apply Xtble.datanode.nodes('condition') as Xtble3(cond)
This is what I am getting
data_code pwdb_id text cond_id
333-004 p-000121212 267 o-000008888
333-005 p-000123456 N/C o-000002222
333-005 p-000456789 99999 o-000002222
333-005 p-000123456 N/C o-000033333
333-005 p-000456789 99999 o-000033333
This is what I want
data_code pwdb_id text cond_id
333-004 p-000121212 267 o-000008888
333-005 p-000123456 N/C o-000002222
333-005 p-000456789 99999 o-000033333