2011-03-06 118 views
0

我正在嘗試編寫一個SQL'FOR XML'查詢來生成特定xml格式的XML塊。到目前爲止我的查詢已經很接近了,但是我有問題讓它產生我需要的確切的xml格式。我希望有人在這裏能幫助我。SQL'FOR XML'查詢

使用下面的SQL,我填充針對者,FOR XML查詢的SQL運行表:

CREATE TABLE PerfTable 
(
ID   INT NOT NULL, 
Name   VARCHAR(500) NOT NULL, 
P_Performance1  NUMERIC(10,2), 
B_Performance1  NUMERIC(10,2), 
P_Performance2  NUMERIC(10,2), 
B_Performance2  NUMERIC(10,2), 
P_Performance3  NUMERIC(10,2), 
B_Performance3  NUMERIC(10,2) 
); 

insert PerfTable(id, Name, P_Performance1, B_Performance1, P_Performance2, 
       B_Performance2, P_Performance3, B_Performance3) 
values (111, 'Item1', -0.111, -0.112, -0.121, -0.122, -0.131, -0.132) 

insert PerfTable(id, Name, P_Performance1, B_Performance1, P_Performance2, 
       B_Performance2, P_Performance3, B_Performance3) 
values (222, 'Item2', -0.211, -0.212, -0.221, -0.222, -0.231, -0.232) 

insert PerfTable(id, Name, P_Performance1, B_Performance1, P_Performance2, 
       B_Performance2, P_Performance3, B_Performance3) 
values (333, 'Item3', -0.311, -0.312, -0.321, -0.322, -0.331, -0.332) 


SELECT TOP 9 
    id, Name, 
    period as "Period_Performance/@Period", 
    F_Perf as "Period_Performance/F_Perf", 
    B_Perf as "Period_Performance/B_Perf" 
FROM 
    (SELECT pt.id, pt.Name, 
      pt.P_Performance1 , 
      pt.B_Performance1, 
      'WTD' as Period1, 
      pt.P_Performance2 , 
      pt.B_Performance2, 
      'MTD' as Period3, 
      pt.P_Performance3 , 
      pt.B_Performance3, 
      'YTD' as Period2 
    FROM PerfTable pt) a 
UNPIVOT 
(F_Perf FOR F IN 
     (P_Performance1, P_Performance2, P_Performance3) 
    ) AS Fund_unpvt 
UNPIVOT 
(B_Perf FOR B IN 
     (B_Performance1, B_Performance2, B_Performance3) 
    ) AS bmk_unpvt 
UNPIVOT 
    (period FOR periods IN 
     (Period1, Period2, Period3) 
    ) AS period_unpvt  
WHERE 
    (RIGHT(F, 1) = RIGHT(B, 1)) 
    AND (RIGHT(F, 1) = RIGHT(periods, 1)) 
FOR XML PATH('Performance') 

然後我運行下面的查詢:

SELECT 
    id, Name, 
    period as "Period_Performance/@Period", 
    F_Perf as "Period_Performance/F_Perf", 
    B_Perf as "Period_Performance/B_Perf" 
FROM 
    (SELECT  
     pt.id, 
     pt.Name, 
     pt.P_Performance1 , 
     pt.B_Performance1, 
     'WTD' as Period1, 
     pt.P_Performance2 , 
     pt.B_Performance2, 
     'MTD' as Period3, 
     pt.P_Performance3 , 
     pt.B_Performance3, 
     'YTD' as Period2 
    FROM PerfTable pt) a 
UNPIVOT 
(F_Perf FOR F IN 
    (P_Performance1,P_Performance2,P_Performance3) 
    ) AS Fund_unpvt 
UNPIVOT 
(B_Perf FOR B IN 
    (B_Performance1,B_Performance2,B_Performance3) 
    ) AS bmk_unpvt 
UNPIVOT 
(period FOR periods IN 
    (Period1,Period2, Period3) 
    ) AS period_unpvt  
WHERE 
    (RIGHT(F,1) = RIGHT(B,1)) 
    AND (RIGHT(F,1) = RIGHT(periods,1)) 
FOR XML PATH('Performance') 

,該查詢產生以下XML(此xml可能無法在此網頁(?)上正確顯示):

<Performance> 
    <id>111</id> 
    <Name>Item1</Name> 
    <Period_Performance Period="WTD"> 
    <F_Perf>-0.11</F_Perf> 
    <B_Perf>-0.11</B_Perf> 
    </Period_Performance> 
</Performance> 
<Performance> 
    <id>111</id> 
    <Name>Item1</Name> 
    <Period_Performance Period="YTD"> 
    <F_Perf>-0.12</F_Perf> 
    <B_Perf>-0.12</B_Perf> 
    </Period_Performance> 
</Performance> 
<Performance> 
    <id>111</id> 
    <Name>Item1</Name> 
    <Period_Performance Period="MTD"> 
    <F_Perf>-0.13</F_Perf> 
    <B_Perf>-0.13</B_Perf> 
    </Period_Performance> 
</Performance> 
<Performance> 
    <id>222</id> 
    <Name>Item2</Name> 
    <Period_Performance Period="WTD"> 
    <F_Perf>-0.21</F_Perf> 
    <B_Perf>-0.21</B_Perf> 
    </Period_Performance> 
</Performance> 
<Performance> 
    <id>222</id> 
    <Name>Item2</Name> 
    <Period_Performance Period="YTD"> 
    <F_Perf>-0.22</F_Perf> 
    <B_Perf>-0.22</B_Perf> 
    </Period_Performance> 
</Performance> 
<Performance> 
    <id>222</id> 
    <Name>Item2</Name> 
    <Period_Performance Period="MTD"> 
    <F_Perf>-0.23</F_Perf> 
    <B_Perf>-0.23</B_Perf> 
    </Period_Performance> 
</Performance> 
<Performance> 
    <id>333</id> 
    <Name>Item3</Name> 
    <Period_Performance Period="WTD"> 
    <F_Perf>-0.31</F_Perf> 
    <B_Perf>-0.31</B_Perf> 
    </Period_Performance> 
</Performance> 
<Performance> 
    <id>333</id> 
    <Name>Item3</Name> 
    <Period_Performance Period="YTD"> 
    <F_Perf>-0.32</F_Perf> 
    <B_Perf>-0.32</B_Perf> 
    </Period_Performance> 
</Performance> 
<Performance> 
    <id>333</id> 
    <Name>Item3</Name> 
    <Period_Performance Period="MTD"> 
    <F_Perf>-0.33</F_Perf> 
    <B_Perf>-0.33</B_Perf> 
    </Period_Performance> 
</Performance> 

此XML我需要生產如下:

<Performance> 
    <id>1</id> 
    <Name>Item1</Name> 
    <Period_Performance Period="WTD"> 
    <F_Perf>-0.11</F_Perf> 
    <B_Perf>-0.11</B_Perf> 
    </Period_Performance> 
    <Period_Performance Period="YTD"> 
    <F_Perf>-0.12</F_Perf> 
    <B_Perf>-0.12</B_Perf> 
    </Period_Performance> 
    <Period_Performance Period="MTD"> 
    <F_Perf>-0.13</F_Perf> 
    <B_Perf>-0.13</B_Perf> 
    </Period_Performance> 
</Performance> 
<Performance> 
    <id>2</id> 
    <Name>Item2</Name> 
    <Period_Performance Period="WTD"> 
    <F_Perf>-0.21</F_Perf> 
    <B_Perf>-0.21</B_Perf> 
    </Period_Performance> 
    <Period_Performance Period="YTD"> 
    <F_Perf>-0.22</F_Perf> 
    <B_Perf>-0.22</B_Perf> 
    </Period_Performance> 
    <Period_Performance Period="MTD"> 
    <F_Perf>-0.23</F_Perf> 
    <B_Perf>-0.23</B_Perf> 
    </Period_Performance> 
</Performance> 
<Performance> 
    <id>3</id> 
    <Name>Item3</Name> 
    <Period_Performance Period="WTD"> 
    <F_Perf>-0.31</F_Perf> 
    <B_Perf>-0.31</B_Perf> 
    </Period_Performance> 
    <Period_Performance Period="YTD"> 
    <F_Perf>-0.32</F_Perf> 
    <B_Perf>-0.32</B_Perf> 
    </Period_Performance> 
    <Period_Performance Period="MTD"> 
    <F_Perf>-0.33</F_Perf> 
    <B_Perf>-0.33</B_Perf> 
    </Period_Performance> 
</Performance> 

任何幫助來創建所需的XML,你可以給,非常感謝。

謝謝

+0

如果您發佈代碼,XML或數據樣本,請**在文本編輯器中突出顯示這些行,然後單擊編輯器工具欄上的「代碼示例」按鈕(「{}」),以良好地格式化和語法突出顯示它! – 2011-03-06 18:23:02

+0

你可以請**解釋**你得到的和你要找的是什麼不同?它看起來非常類似於我... – 2011-03-06 18:23:49

+0

謝謝馬克。我已經能夠解決這個問題,但我需要的是每個ID一個Performance塊,並在Performance塊中重複Period_Performance(WTD,MTD,YTD等)塊。您可能會注意到,在'before'xml中,每個Performance塊有一個Period_Performance塊。不管怎麼說,還是要謝謝你。乙 – brendan 2011-03-07 10:58:12

回答

0

已解決。

謝謝Marc。我已經能夠解決這個問題,但我需要的是每個ID一個Performance塊,並在Performance塊中重複Period_Performance(WTD,MTD,YTD等)塊。您可能會注意到,在'before'xml中,每個Performance塊有一個Period_Performance塊。不管怎麼說,還是要謝謝你。 B