2017-06-05 35 views
0

你好,我試圖在sql服務器的xml格式獲取結果,但我沒有得到propare XML格式的許多數據它的XML格式重複。我已經寫了這個查詢在sql服務器,但沒有得到propare數據。任何人都知道我的錯誤在哪裏,那麼請讓我知道如何做到這一點。以xml格式獲取sql查詢結果?

在這裏,我寫這篇文章的查詢:

select * from (

    SELECT SSCF.SubSubCategoryId AS InterestId,c.FeedId,c.Description,u.UserId,u.Email,u.UserName,u.ProfileImage,u.Name, 
      ISNULL(SSCL.SubSubCategory,SSC.SubSubCategory) AS Interest, 
      1 AS [Type] 
    FROM SubSubCategoryFollowers SSCF 
    LEFT JOIN SubSubCategories SSC ON SSCF.SubSubCategoryId = SSC.SubSubCategoryId    
    INNER JOIN Feed c on c.FeedId = SSC.FeedId 
    inner join Users u on u.UserId = SSCF.UserId 
    WHERE u.Email is not null 

    UNION ALL 

    SELECT SCF.SubCategoryId AS InterestId,c.FeedId,c.Description,u.UserId,u.Email,u.UserName,u.ProfileImage,u.Name, 
      ISNULL(SCL.SubCategory,SC.SubCategory) AS Interest, 
      2 AS [Type] 
    FROM SubCategoryFollowers SCF 
    LEFT JOIN SubCategories SC ON SCF.SubCategoryId = SC.SubCategoryId    
    INNER JOIN Feed c on c.FeedId = SC.FeedId 
    inner join Users u on u.UserId = SCF.UserId 
    WHERE u.Email is not null 

)as res 

group by res.UserId,res.InterestId,res.FeedId,res.Description,res.Email,res.Interest,res.Type,res.UserName,res.ProfileImage,res.Name 
order by res.FeedId    
OFFSET 1 ROWS 
FETCH NEXT 50000 ROWS ONLY 
FOR XML PATH('User'), ROOT ('Users')  

這是我目前的OP =>

<Users> 
    <User> 
    <UserId>1660</UserId> 
    <Email>xyz.com</Email> 
    <UserName>xyz</UserName> 
    <ProfileImage>20160717035320958.jpeg</ProfileImage>  
    <InterestId>15</InterestId> 
    <FeedId>4689</FeedId> 
    <Description>Test</Description>   
    <Interest>Event</Interest> 
    <Type>2</Type> 
</User> 
<User> 
    <UserId>1660</UserId> 
    <Email>xyz.com</Email> 
    <UserName>xyz</UserName> 
    <ProfileImage>20160717035320958.jpeg</ProfileImage> 
    <InterestId>16</InterestId> 
    <FeedId>4689</FeedId> 
    <Description>Test</Description>   
    <Interest>Party</Interest> 
    <Type>2</Type> 
</User> 
<User> 
    <UserId>1660</UserId> 
    <Email>xyz.com</Email> 
    <UserName>xyz</UserName> 
    <ProfileImage>20160717035320958.jpeg</ProfileImage> 
    <InterestId>21</InterestId> 
    <FeedId>4689</FeedId> 
    <Description>Test</Description>  
    <Interest>Club</Interest> 
    <Type>2</Type> 
</User> 
<User> 
<UserId>1661</UserId> 
<Email>abc.com</Email> 
<UserName>abc</UserName> 
<ProfileImage>20160717035320959.jpeg</ProfileImage>  
<InterestId>15</InterestId> 
<FeedId>4690</FeedId> 
<Description>Test1</Description>   
<Interest>Cricket</Interest> 
<Type>1</Type> 

我的預期O/P =>

<Users> 
<User> 
<UserId>1660</UserId> 
<Email>xyz.com</Email> 
<UserName>xyz</UserName> 
<ProfileImage>20160717035320958.jpeg</ProfileImage> 

<InterestId>15</InterestId> 
<FeedId>4689</FeedId> 
<Description>Test</Description>   
<Interest>Event</Interest> 
<Type>2</Type> 

<InterestId>16</InterestId> 
<FeedId>4689</FeedId> 
<Description>Test</Description>   
<Interest>Party</Interest> 
<Type>2</Type> 

<InterestId>21</InterestId> 
<FeedId>4689</FeedId> 
<Description>Test</Description>  
<Interest>Club</Interest> 
<Type>2</Type> 
</User> 
<User> 
<UserId>1661</UserId> 
<Email>abc.com</Email> 
<UserName>abc</UserName> 
<ProfileImage>20160717035320959.jpeg</ProfileImage> 

<InterestId>15</InterestId> 
<FeedId>4690</FeedId> 
<Description>Test1</Description>   
<Interest>Cricket</Interest> 
<Type>1</Type> 

我想要這種數據在XML格式任何人知道請讓我知道。

+0

什麼是你查詢的結果爲是和它是如何不同於你所期望的輸出? – iamdave

+0

現在我有重複這個數據以及像UserId是重複所有的數據是重複的,所以我需要停止這個重複的數據 – coderwill

+0

@iamdave我可以發佈我的當前操作,所以你可以很容易理解? – coderwill

回答

2

我對你的情況的一個例子。 你可以使用TYPE獲得嵌套的XML

DECLARE @SampleData AS TABLE 
(
    UserId INT, 
    Email varchar(200), 
    UserName varchar(200), 
    InterestId int, 
    Description varchar(200) 
) 

INSERT INTO @SampleData 
(
    UserId, 
    Email, 
    UserName, 
    InterestId, 
    Description 
) 
VALUES 
(1,'[email protected]','User1', 100, 'Description 100'), 
(1,'[email protected]','User1', 101, 'Description 101'), 
(1,'[email protected]','User1', 102, 'Description 102'), 
(1,'[email protected]','User1', 103, 'Description 103') 


SELECT sd.UserId, 
     sd.Email, 
     sd.UserName, 
     (
      SELECT sd2.InterestId, 
       sd2.Description 
      FROM @SampleData sd2 
      WHERE sd2.UserId = sd.UserId 
      FOR XML PATH (''), TYPE 
     ) 
FROM 
( select DISTINCT sd.UserId, sd.Email, sd.UserName 
    FROM @SampleData sd 
) sd 
FOR XML PATH('User') ,ROOT('Users') 

返回

<Users> 
    <User> 
    <UserId>1</UserId> 
    <Email>[email protected]</Email> 
    <UserName>User1</UserName> 
    <InterestId>100</InterestId> 
    <Description>Description 100</Description> 
    <InterestId>101</InterestId> 
    <Description>Description 101</Description> 
    <InterestId>102</InterestId> 
    <Description>Description 102</Description> 
    <InterestId>103</InterestId> 
    <Description>Description 103</Description> 
    </User> 
</Users> 

演示鏈接:http://rextester.com/KKEHCR86171

+0

Triv與我的查詢怎麼可以做到這一點在查詢我可以使用工會也如此 – coderwill

+0

你可以請給我與我的查詢你的答案所以這是非常充分的使用我這個解決方案它也是100%好,但我不能修改我的查詢,所以請你能幫助更多人 – coderwill

+0

你應該嘗試自己。我認爲將我的答案映射到代碼中並不困難.... :) – TriV

2

這個想法是通過標記路徑標記給xml所需的格式。 不幸的是我沒有時間通過​​你的sql語句的工作,但這個是標籤編組的例子:

select UserId as "User" , InterestId as "User/InterestId" from 
(select '1' as 'UserId', 'I1' as 'InterestId' 
union all 
select '1' as 'UserId', 'I2' as 'InterestId') x 
for xml path(''), root('Users')