sql - count occurrence of xml child nodes from two identical parent nodes -
i'm trying count number child nodes within 2 identical parent nodes in xml clob i've stored in table of mine.
the xml clob has format this:
<products> <id>1</id> <discount></discount> <discount></discount> </products> <products> <id>2</id> <discount></discount> </products> i want xmlquery go through xml clob , identify how many discounts there in each products , ignore have less or 0 <discount>. in example above should return 2.
thanks.
i realized answer sql-server only. i'll let here in case other people
here query 1 record each product 1 or more discount node. done using xml query function "count()"
declare @xml xml set @xml = '<products> <id>1</id> <discount></discount> <discount></discount> </products> <products> <id>2</id> <discount></discount> </products>' select n.xmlnode.query('.') productnode @xml.nodes('/products') n(xmlnode) n.xmlnode.query('count(discount)').value('.','int') > 0
Comments
Post a Comment