Запрос SQL FOR FOR - PullRequest
       8

Запрос SQL FOR FOR

0 голосов
/ 06 марта 2011

Я пытаюсь написать запрос SQL FOR FOR, который создает блок 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.

Спасибо

1 Ответ

0 голосов
/ 07 марта 2011

Решено.

Спасибо, Марк.Мне удалось решить эту проблему, но мне был нужен один блок производительности на идентификатор с повторяющимися блоками Period_Performance (WTD, MTD, YTD и т. Д.) Внутри блока Performance.Вы можете заметить, что в «до» xml существует один блок Period_Performance на блок производительности.Спасибо, в любом случае.B

...