SQL“FOR XML”询问
我正在尝试编写一个 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 的任何帮助。
谢谢
I am trying to write a SQL 'FOR XML' query that produces an XML block in a specific xml format. The query I have so far is close but I am having problems getting it produce the exact xml format that I need. I hoping someone on here can help me.
Using the following SQL, I populate the table against which, the SQL FOR XML query is run:
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')
Then I run the following query:
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')
This query produces the following XML (this xml may not display correctly on this webpage(?)):
<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>
This XML that I need to produce is below:
<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>
Any help to create the desired XML you can give, is greatly appreciated.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
已解决。
谢谢马克。我已经能够解决这个问题,但我需要的是每个 id 一个 Performance 块,并在 Performance 块内重复Period_Performance(WTD、MTD、YTD 等)块。您可能会注意到,在“之前”的 xml 中,每个 Performance 块都有一个 period_Performance 块。不管怎样,谢谢。乙
Resolved.
Thanks Marc. I have been able to solve this issue, but what I needed was one Performance block per id, with repeating Period_Performance (WTD, MTD, YTD etc.) blocks within the Performance block. You might notice that in the 'before' xml, there is one Period_Performance block per Performance block. Thanks anyway. B