特定の xml 形式で XML ブロックを生成する SQL 'FOR 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 は、この Web ページ (?) では正しく表示されない可能性があります)。
<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を作成するための助けは大歓迎です.
ありがとう