C Sharper
C Sharper

Reputation: 8646

Multiple XML tags in SQL

I have below data for select * from _temp :-

enter image description here

I want to generate below xml :-

<xml>
  <StoryData>
    <UserStoryId>141204</UserStoryId>
    <Description>Customer can see the applicable discount on the quote and change in premium.</Description>
    <Summary>Customer can see the applicable discount on the quote and change in premium.</Summary>
    <UserStoryID>141204</UserStoryID>
    <VagueWord>and</VagueWord>
    <VagueWord>applicable</VagueWord>
  </StoryData>

  <StoryData>
    <UserStoryId>141205</UserStoryId>
    <Description>Customer can see the applicable discount on the quote and change in premium.</Description>
    <Summary>Customer can see the applicable discount on the quote and change in premium.</Summary>
    <UserStoryID>141205</UserStoryID>
    <VagueWord>and</VagueWord>
    <VagueWord>applicable</VagueWord>
  </StoryData>  
</xml>

I tried below query :-

select distinct t1.UserStoryId,t1.Description,t1.Summary,t1.UserStoryID,t2.VagueWord
from _temp t1 left join
(
 select UserStoryId,VagueWord from _temp
) t2
on t1.UserStoryId=t2.UserStoryId
where t1.UserStoryId in (141204,141205)

FOR XML PATH('StoryData')

,ROOT('xml'),type

Which is generating :-

<xml>
  <StoryData>
    <UserStoryId>141204</UserStoryId>
    <Description>Customer can see the applicable discount on the quote and change in premium.</Description>
    <Summary>Customer can see the applicable discount on the quote and change in premium.</Summary>
    <UserStoryID>141204</UserStoryID>
    <VagueWord>and</VagueWord>
  </StoryData>
  <StoryData>
    <UserStoryId>141204</UserStoryId>
    <Description>Customer can see the applicable discount on the quote and change in premium.</Description>
    <Summary>Customer can see the applicable discount on the quote and change in premium.</Summary>
    <UserStoryID>141204</UserStoryID>
    <VagueWord>applicable</VagueWord>
  </StoryData>
  <StoryData>
    <UserStoryId>141205</UserStoryId>
    <Description>Customer can see the applicable discount on the quote and change in premium.</Description>
    <Summary>Customer can see the applicable discount on the quote and change in premium.</Summary>
    <UserStoryID>141205</UserStoryID>
    <VagueWord>and</VagueWord>
  </StoryData>
  <StoryData>
    <UserStoryId>141205</UserStoryId>
    <Description>Customer can see the applicable discount on the quote and change in premium.</Description>
    <Summary>Customer can see the applicable discount on the quote and change in premium.</Summary>
    <UserStoryID>141205</UserStoryID>
    <VagueWord>applicable</VagueWord>
  </StoryData>
</xml>

As we can see , VagueWord is multiple, StoryData tag is getting repeated for that particular UserStoryID.

I wanted distinct tag for distinct UserStoryID and Vagueword tag to be internally repeated as shown above.

How can I achieve this?

Upvotes: 4

Views: 77

Answers (1)

gofr1
gofr1

Reputation: 15997

You can use subquery:

;WITH cte AS (
SELECT *
FROM (VALUES
(141204,'Customer can see the applicable discount on the quote and change in premium.','Customer can see the applicable discount on the quote and change in premium.','and'),
(141204,'Customer can see the applicable discount on the quote and change in premium.','Customer can see the applicable discount on the quote and change in premium.','applicable'),
(141205,'Customer can see the applicable discount on the quote and change in premium.','Customer can see the applicable discount on the quote and change in premium.','and'),
(141205,'Customer can see the applicable discount on the quote and change in premium.','Customer can see the applicable discount on the quote and change in premium.','applicable')
) as t(UserStoryId, [Description], Summary, VagueWord)
)

SELECT  UserStoryId, 
        [Description], 
        Summary, 
        (SELECT VagueWord
        FROM cte
        WHERE UserStoryId = c.UserStoryId
        FOR XML PATH(''),TYPE)
FROM cte c
GROUP BY  UserStoryId, 
        [Description], 
        Summary
FOR XML PATH('StoryData'),ROOT('xml'),TYPE

Output:

<xml>
  <StoryData>
    <UserStoryId>141204</UserStoryId>
    <Description>Customer can see the applicable discount on the quote and change in premium.</Description>
    <Summary>Customer can see the applicable discount on the quote and change in premium.</Summary>
    <VagueWord>and</VagueWord>
    <VagueWord>applicable</VagueWord>
  </StoryData>
  <StoryData>
    <UserStoryId>141205</UserStoryId>
    <Description>Customer can see the applicable discount on the quote and change in premium.</Description>
    <Summary>Customer can see the applicable discount on the quote and change in premium.</Summary>
    <VagueWord>and</VagueWord>
    <VagueWord>applicable</VagueWord>
  </StoryData>
</xml>

Upvotes: 2

Related Questions