Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Grouping XML data in SQL

Author  Topic 

kbhit
Starting Member

2 Posts

Posted - 2014-05-21 : 06:57:24
Hi,

I am joining multiple tables and my result is following:

UserGroup
UserName

UG1
Tom

UG1
Harry

UG2
Albert

UG3
Jim

UG3
Sam


I want xml in following format:


<UserGroupsInfo>
<UserGroups>
<UserGroup name="UG1">
<User>
Tom
</User>
<User>
Hary
</User>
</UserGroup>
<UserGroup name="UG2">
<User>
Albert
</User>
</UserGroup>
<UserGroup name="UG3">
<User>
Jim
</User>
<User>
Sam
</User>
</UserGroup>
</UserGroups>
</UserGroupsInfo>

I have tried all combinations of for xml path but no success yet. Can someone help?

Regards,
KB

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-05-21 : 08:17:06
I solved it this way:


WITH JoinedTables(UserGroup, UserName) as (
SELECT 'UG1', 'Tom' UNION All
SELECT 'UG1', 'Harry' UNION all
SELECT 'UG2', 'Albert' UNION all
SELECT 'UG3', 'Jim' UNION all
SELECT 'UG3', 'Sam'
)

SELECT UserGroup "@name"
, (SELECT UserName "User" FROM JoinedTables T2
Where t2.UserGroup = t1.UserGroup
FOR XML PATH(''), TYPE)

FROM (SELECT DISTINCT UserGroup FROM JoinedTables) t1
FOR XML PATH('UserGroup'), ROOT('UserGroupsInfo')
Go to Top of Page

kbhit
Starting Member

2 Posts

Posted - 2014-05-21 : 09:47:20
Thank you, your solution is perfect.

PS: Just noticed that i am having & lt; in my nested for xml. I tried using TYPE but i am getting following error:

The xml data type cannot be selected as DISTINCT because it is not comparable.

SELECT UserGroup "@name"
, (SELECT UserName "User" FROM JoinedTables T2
Where t2.UserGroup = t1.UserGroup
FOR XML PATH(''), TYPE)

FROM (SELECT DISTINCT UserGroup FROM JoinedTables) t1
FOR XML PATH('UserGroup'), TYPE,ROOT('UserGroupsInfo')
Go to Top of Page

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-05-21 : 11:41:18
Try removing TYPE from the outer query (that's the one with DISTINCT)
Go to Top of Page

MuralikrishnaVeera
Posting Yak Master

129 Posts

Posted - 2014-05-22 : 07:50:41
Hai britton is it possible to convert the below XML data into table.

<UserGroupsInfo>
<UserGroup name="UG1">
<User>Tom</User>
<User>Harry</User>
</UserGroup>
<UserGroup name="UG2">
<User>Albert</User>
</UserGroup>
<UserGroup name="UG3">
<User>Jim</User>
<User>Sam</User>
</UserGroup>
</UserGroupsInfo>

I want output table like

UG1 UG2 UG3
TOm Albert Jim
Harry NULL Sam

I just want to know whether it is possible or not because i am new to XML



---------------
Murali Krishna

You live only once ..If you do it right once is enough.......
Go to Top of Page

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-05-22 : 08:47:35
Short answer: yes. Reference:

http://blog.sqlauthority.com/2009/02/13/sql-server-simple-example-of-reading-xml-file-using-t-sql/
Go to Top of Page

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-05-22 : 11:34:58
Something like this:


declare @xml XML = '
<UserGroupsInfo>
<UserGroup name="UG1">
<User>Tom</User>
<User>Harry</User>
</UserGroup>
<UserGroup name="UG2">
<User>Albert</User>
</UserGroup>
<UserGroup name="UG3">
<User>Jim</User>
<User>Sam</User>
</UserGroup>
</UserGroupsInfo>
'

;with
Users(UserName, GroupName, rn) as (
select [User].value('.', 'varchar(50)') Name
, [User].value('../@name[1]', 'varchar(50)') GroupName
, ROW_NUMBER() over(
partition by [User].value('../@name[1]', 'varchar(50)')
order by (select 1)) as rn
from @xml.nodes('/UserGroupsInfo/UserGroup/User') x([User])
)

select UG1, UG2, UG3 from Users
pivot (max(UserName) for GroupName in (UG1, UG2, UG3)) pvt
return
Go to Top of Page

MuralikrishnaVeera
Posting Yak Master

129 Posts

Posted - 2014-05-23 : 05:06:11
Perfect.Thank you britton

---------------
Murali Krishna

You live only once ..If you do it right once is enough.......
Go to Top of Page
   

- Advertisement -