sql server - Group XML Column Data -


using sql server 2012. have table containing group identifier , small snippets of xml. here sample:

create table temp (     groupid [int] not null,     rawxml [xml] null )  insert temp values (1, '<item><criteria type="state" values="ca"/><criteria type="state" values="tx"/><criteria type="state" values="fl"/></item>') insert temp values (1, '<item><criteria type="manager" values="tim"/></item>') insert temp values (2, '<item><criteria type="name" values="walters"/></item>') insert temp values (2, '<item><criteria type="manager" values="tim"/></item>') 

what want group snippets of xml groupid form larger xml document end result structured this:

<parent>     <group groupid="1">         <item>             <criteria type="state" values="ca"/>             <criteria type="state" values="tx"/>             <criteria type="state" values="fl"/>         </item>         <item>             <criteria type="manager" values="tim"/>         </item>     </group>     <group groupid="2">         <item>             <criteria type="name" values="walters"/>         </item>         <item>             <criteria type="manager" values="tim"/>         </item>     </group> </parent> 

i fear solution concatenate contents of xml column first using type of string concatenation method, , using xml select statement, feel slow application. missing , there easy way group xml data in manner, or string concatenation choice.

of course there simpler way:

declare @t table (     groupid [int] not null,     rawxml [xml] null );  insert @t values (1, '<item><criteria type="state" values="ca"/><criteria type="state" values="tx"/><criteria type="state" values="fl"/></item>'), (1, '<item><criteria type="manager" values="tim"/></item>'), (2, '<item><criteria type="name" values="walters"/></item>'), (2, '<item><criteria type="manager" values="tim"/></item>');  select sq.groupid [@groupid],     (         select t.rawxml [node()]         @t t         t.groupid = sq.groupid         xml path(''), type     ) (select distinct g.groupid @t g) sq xml path('group'), type, root('parent'); 

(i have replaced original table equivalent tv because, well, don't clean databases of such tables afterwards. doesn't introduce difference regards question.)


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -