0

我正在尝试编写一个 SQL 'FOR XML' 查询,该查询以特定的 xml 格式生成一个 XML 块。到目前为止,我的查询很接近,但我无法让它生成我需要的确切 xml 格式。我希望这里有人可以帮助我。

使用以下 SQL,我填充了运行 SQL FOR XML 查询的表:

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 的任何帮助。

谢谢

4

1 回答 1

0

解决。

谢谢马克。我已经能够解决这个问题,但我需要的是每个 id 一个 Performance 块,在 Performance 块中重复 Period_Performance(WTD、MTD、YTD 等)块。您可能会注意到,在“之前”xml 中,每个 Performance 块都有一个 Period_Performance 块。不管怎么说,还是要谢谢你。乙

于 2011-03-07T10:59:16.807 回答