2016-12-09 7 views
0

다음 XML 코드가 있으며 코드 뒤에 표시된 XML을 생성하려고합니다. 몇 가지 방법을 시도했지만 성공하지는 못했습니다.다중 루트 XML - SQL Server

두 개의 테이블이 있습니다. [meiForecasting].[dbo].george_mdx은 아래에 표시된 데이터가 있고 다른 데이터는 다음과 같습니다. 나는 priceband (pricedfrom - pricedto)의 각 그룹 사이에 속하는 george_mdx의 각 UnitPrice에 대해 xml을 얻을 수 있도록 테이블에 가입하려고합니다. 그러면 다음 xml 형식을 생성 할 수 있습니다.

아래 코드를 참조하십시오.

내부 xml은 완벽하게 작동하지만 바깥 쪽 요소 (및 pricebands)는 문제가됩니다.

(Select 

(select 

pb.pricebandID '@Id', 
pb.[pricedfrom] '@start', 
pb.[PricedTo] '@end', 

(
SELECT cast(yearnumber as int) AS '@number' 
,(convert(varchar(12), cast(yearstart as date), 126)) AS '@yearstart' 
    ,(convert(varchar(12), cast(yearend as date), 126)) AS '@yearend' 
    ,(
    SELECT cast(X.WeekNumber as int) AS '@number' 
      ,cast(x.Quantity as int) AS '@units'  
    FROM [meiForecasting].[dbo].george_mdx AS X 
    WHERE cast(X.YearNumber as int)=cast(tbl.YearNumber as int) 
    FOR XML PATH('week'),TYPE 
    ) 
FROM [meiForecasting].[dbo].george_mdx AS tbl inner join   meiforecasting.PLANNING.PriceBand as pb 
ON (tbl.unitprice <= pb.PricedTo AND tbl.unitprice >= pb.PricedFrom) 
GROUP BY YearNumber,YearStart,YearEnd, pb.GroupID 
FOR XML PATH('year'),ROOT('years') 
    ) 
) 

FOR XML PATH ('Priceband'),ROOT ('PriceBands') 
) 
) 

테이블 [meiForecasting]. [DBO] .george_mdx

+------------+------------+----------+-----------+ 
| yearnumber | weeknumber | quantity | unitprice | 
+------------+------------+----------+-----------+ 
|   1 |   44 |  3807 | 45662.55 | 
|   1 |   45 |  4048 | 45295.00 | 
|   1 |   46 |  3862 | 45047.68 | 
|   1 |   47 |  3677 | 41013.00 | 
|   1 |   48 |  3695 | 39387.43 | 
|   1 |   49 |  3871 | 43199.02 | 
|   1 |   50 |  3686 | 39131.82 | 
|   2 |   1 |  4287 | 51086.02 | 
|   2 |   2 |  4751 | 60404.40 | 
|   2 |   3 |  5536 | 67468.90 | 
|   2 |   4 |  3712 | 45776.32 | 
|   2 |   5 |  3367 | 44288.19 | 
|   2 |   6 |  3598 | 45307.49 | 
|   2 |   7 |  3461 | 47757.59 | 
|   2 |   8 |  3334 | 43771.33 | 
|   2 |   9 |  3325 | 41656.88 | 
|   2 |   10 |  3313 | 44738.39 | 
|   2 |   32 |  3303 | 40144.35 | 
|   2 |   33 |  3229 | 45892.27 | 
|   2 |   34 |  3313 | 44570.07 | 
|   2 |   35 |  3338 | 44978.12 | 
|   2 |   36 |  3432 | 46760.30 | 
|   2 |   37 |  3340 | 48341.05 | 
|   2 |   38 |  3597 | 48673.97 | 
|   2 |   39 |  3628 | 48636.17 | 
|   3 |   29 |  3179 | 43610.97 | 
+------------+------------+----------+-----------+ 


+--------------------+---------+----------+ 
| Planning.  |priceband|   | 
+--------------------+---------+----------+ 
|     | 
| 43     | 0.00 | 1000.00 | 
| 45     | 0.00 | 1000.00 | 
| 49     | 0.00 | 999.99 | 
| 50     | 1000.00 | 1999.99 | 
| 51     | 2000.00 | 9999.00 | 
| 52     | 0.00 | 15.00 | 
| 53     | 15.01 | 300.00 | 
| 62     | 0.00 | 15.00 | 
| 63     | 15.01 | 200.00 | 
| 69     | 100.00 | 500.99 | 
| 70     | 501.00 | 1000.00 | 
| 78     | 0.00 | 299.99 | 
| 79     | 300.00 | 399.99 | 
| 80     | 400.00 | 499.99 | 
| 81     | 500.00 | 599.99 | 
| 82     | 600.00 | 699.99 | 
| 83     | 700.00 | 999.99 | 
| 84     | 1000.00 | 10000.00 | 
| 125    | 50.00 | 99.99 | 
| 126    | 100.00 | 199.99 | 
| 127    | 200.00 | 299.99 | 
| 128    | 300.00 | 399.99 | 
| 129    | 400.00 | 9999.00 | 
+--------------------+---------+----------+ 

예상 XML

<priceBands> <priceBand id="1" end="99.99" start="0.00"> 
<years> <year number="1"> 
<week number="1" units="41"/> 
<week number="2" units="41"/> 
<week number="3" units="41"/> 
<week number="4" units="41"/> 
<week number="5" units="41"/> 
<week number="6" units="41"/> 
<week number="7" units="41"/> 
<week number="8" units="41"/> 
<week number="9" units="41"/> 
<week number="10" units="41"/> 
<week number="11" units="41"/> 
<week number="12" units="41"/> 
<week number="13" units="41"/> 
<week number="14" units="41"/> 
<week number="15" units="41"/> 
<week number="16" units="41"/> 
<week number="17" units="41"/> 
<week number="18" units="41"/> 
<week number="19" units="41"/> 
<week number="20" units="41"/> 
<week number="21" units="41"/> 
<week number="22" units="41"/> 
<week number="23" units="41"/> 
<week number="24" units="41"/> 
<week number="25" units="41"/> 
<week number="26" units="41"/> 
<week number="27" units="41"/> 
<week number="28" units="41"/> 
<week number="29" units="41"/> 
<week number="30" units="41"/> 
<week number="31" units="41"/> 
<week number="32" units="41"/> 
<week number="33" units="41"/> 
<week number="34" units="41"/> 
<week number="35" units="41"/> 
<week number="36" units="41"/> 
<week number="37" units="41"/> 
<week number="38" units="41"/> 
<week number="39" units="41"/> 
<week number="40" units="41"/> 
<week number="41" units="41"/> 
<week number="42" units="41"/> 
<week number="43" units="41"/> 
<week number="44" units="41"/> 
<week number="45" units="41"/> 
<week number="46" units="41"/> 
<week number="47" units="41"/> 
<week number="48" units="41"/> 
<week number="49" units="41"/> 
<week number="50" units="41"/> 
<week number="41" units="41"/> 
<week number="52" units="41"/> 

</year> -<year number="2"> 
<week number="1" units="41"/> 
<week number="2" units="41"/> 
<week number="3" units="41"/> 
<week number="4" units="41"/> 
<week number="5" units="41"/> 
<week number="6" units="41"/> 
<week number="7" units="41"/> 
<week number="8" units="41"/> 
<week number="9" units="41"/> 
<week number="10" units="41"/> 
<week number="11" units="41"/> 
<week number="12" units="41"/> 
<week number="13" units="41"/> 
<week number="14" units="41"/> 
<week number="15" units="41"/> 
<week number="16" units="41"/> 
<week number="17" units="41"/> 
<week number="18" units="41"/> 
<week number="19" units="41"/> 
<week number="20" units="41"/> 
<week number="21" units="41"/> 
<week number="22" units="41"/> 
<week number="23" units="41"/> 
<week number="24" units="41"/> 
<week number="25" units="41"/> 
<week number="26" units="41"/> 
<week number="27" units="41"/> 
<week number="28" units="41"/> 
<week number="29" units="41"/> 
<week number="30" units="41"/> 
<week number="31" units="41"/> 
<week number="32" units="41"/> 
<week number="33" units="41"/> 
<week number="34" units="41"/> 
<week number="35" units="41"/> 
<week number="36" units="41"/> 
<week number="37" units="41"/> 
<week number="38" units="41"/> 
<week number="39" units="41"/> 
<week number="40" units="41"/> 
<week number="41" units="41"/> 
<week number="42" units="41"/> 
<week number="43" units="41"/> 
<week number="44" units="41"/> 
<week number="45" units="41"/> 
<week number="46" units="41"/> 
<week number="47" units="41"/> 
<week number="48" units="41"/> 
<week number="49" units="41"/> 
<week number="50" units="41"/> 
<week number="41" units="41"/> 
<week number="52" units="41"/> 

</year> <year number="3"> 
<week number="1" units="41"/> 
<week number="2" units="41"/> 
<week number="3" units="41"/> 
<week number="4" units="41"/> 
<week number="5" units="41"/> 
<week number="6" units="41"/> 
<week number="7" units="41"/> 
<week number="8" units="41"/> 
<week number="9" units="41"/> 
<week number="10" units="41"/> 
<week number="11" units="41"/> 
<week number="12" units="41"/> 
<week number="13" units="41"/> 
<week number="14" units="41"/> 
<week number="15" units="41"/> 
<week number="16" units="41"/> 
<week number="17" units="41"/> 
<week number="18" units="41"/> 
<week number="19" units="41"/> 
<week number="20" units="41"/> 
<week number="21" units="41"/> 
<week number="22" units="41"/> 
<week number="23" units="41"/> 
<week number="24" units="41"/> 
<week number="25" units="41"/> 
<week number="26" units="41"/> 
<week number="27" units="41"/> 
<week number="28" units="41"/> 
<week number="29" units="41"/> 
<week number="30" units="41"/> 
<week number="31" units="41"/> 
<week number="32" units="41"/> 
<week number="33" units="41"/> 
<week number="34" units="41"/> 
<week number="35" units="41"/> 
<week number="36" units="41"/> 
<week number="37" units="41"/> 
<week number="38" units="41"/> 
<week number="39" units="41"/> 
<week number="40" units="41"/> 
<week number="41" units="41"/> 
<week number="42" units="41"/> 
<week number="43" units="41"/> 
<week number="44" units="41"/> 
<week number="45" units="41"/> 
<week number="46" units="41"/> 
<week number="47" units="41"/> 
<week number="48" units="41"/> 
<week number="49" units="41"/> 
<week number="50" units="41"/> 
<week number="41" units="41"/> 
<week number="52" units="41"/> 

</year> -<year number="4"> 
<week number="1" units="41"/> 
<week number="2" units="41"/> 
<week number="3" units="41"/> 
<week number="4" units="41"/> 
<week number="5" units="41"/> 
<week number="6" units="41"/> 
<week number="7" units="41"/> 
<week number="8" units="41"/> 
<week number="9" units="41"/> 
<week number="10" units="41"/> 
<week number="11" units="41"/> 
<week number="12" units="41"/> 
<week number="13" units="41"/> 
<week number="14" units="41"/> 
<week number="15" units="41"/> 
<week number="16" units="41"/> 
<week number="17" units="41"/> 
<week number="18" units="41"/> 
<week number="19" units="41"/> 
<week number="20" units="41"/> 
<week number="21" units="41"/> 
<week number="22" units="41"/> 
<week number="23" units="41"/> 
<week number="24" units="41"/> 
<week number="25" units="41"/> 
<week number="26" units="41"/> 
<week number="27" units="41"/> 
<week number="28" units="41"/> 
<week number="29" units="41"/> 
<week number="30" units="41"/> 
<week number="31" units="41"/> 
<week number="32" units="41"/> 
<week number="33" units="41"/> 
<week number="34" units="41"/> 
<week number="35" units="41"/> 
<week number="36" units="41"/> 
<week number="37" units="41"/> 
<week number="38" units="41"/> 
<week number="39" units="41"/> 
<week number="40" units="41"/> 
<week number="41" units="41"/> 
<week number="42" units="41"/> 
<week number="43" units="41"/> 
<week number="44" units="41"/> 
<week number="45" units="41"/> 
<week number="46" units="41"/> 
<week number="47" units="41"/> 
<week number="48" units="41"/> 
<week number="49" units="41"/> 
<week number="50" units="41"/> 
<week number="41" units="41"/> 
<week number="52" units="41"/> 

</year> <year number="5"> 
<week number="1" units="41"/> 
<week number="2" units="41"/> 
<week number="3" units="41"/> 
<week number="4" units="41"/> 
<week number="5" units="41"/> 
<week number="6" units="41"/> 
<week number="7" units="41"/> 
<week number="8" units="41"/> 
<week number="9" units="41"/> 
<week number="10" units="41"/> 
<week number="11" units="41"/> 
<week number="12" units="41"/> 
<week number="13" units="41"/> 
<week number="14" units="41"/> 
<week number="15" units="41"/> 
<week number="16" units="41"/> 
<week number="17" units="41"/> 
<week number="18" units="41"/> 
<week number="19" units="41"/> 
<week number="20" units="41"/> 
<week number="21" units="41"/> 
<week number="22" units="41"/> 
<week number="23" units="41"/> 
<week number="24" units="41"/> 
<week number="25" units="41"/> 
<week number="26" units="41"/> 
<week number="27" units="41"/> 
<week number="28" units="41"/> 
<week number="29" units="41"/> 
<week number="30" units="41"/> 
<week number="31" units="41"/> 
<week number="32" units="41"/> 
<week number="33" units="41"/> 
<week number="34" units="41"/> 
<week number="35" units="41"/> 
<week number="36" units="41"/> 
<week number="37" units="41"/> 
<week number="38" units="41"/> 
<week number="39" units="41"/> 
<week number="40" units="41"/> 
<week number="41" units="41"/> 
<week number="42" units="41"/> 
<week number="43" units="41"/> 
<week number="44" units="41"/> 
<week number="45" units="41"/> 
<week number="46" units="41"/> 
<week number="47" units="41"/> 
<week number="48" units="41"/> 
<week number="49" units="41"/> 
<week number="50" units="41"/> 
<week number="41" units="41"/> 
<week number="52" units="41"/> 
</year> </years> </priceBand> 

<priceBand id="2" end="499.99" start="100.00"> 

<years> <year number="1"> 
<week number="1" units="41"/> 
<week number="2" units="41"/> 
<week number="3" units="41"/> 
<week number="4" units="41"/> 
<week number="5" units="41"/> 
<week number="6" units="41"/> 
<week number="7" units="41"/> 
<week number="8" units="41"/> 
<week number="9" units="41"/> 
<week number="10" units="41"/> 
<week number="11" units="41"/> 
<week number="12" units="41"/> 
<week number="13" units="41"/> 
<week number="14" units="41"/> 
<week number="15" units="41"/> 
<week number="16" units="41"/> 
<week number="17" units="41"/> 
<week number="18" units="41"/> 
<week number="19" units="41"/> 
<week number="20" units="41"/> 
<week number="21" units="41"/> 
<week number="22" units="41"/> 
<week number="23" units="41"/> 
<week number="24" units="41"/> 
<week number="25" units="41"/> 
<week number="26" units="41"/> 
<week number="27" units="41"/> 
<week number="28" units="41"/> 
<week number="29" units="41"/> 
<week number="30" units="41"/> 
<week number="31" units="41"/> 
<week number="32" units="41"/> 
<week number="33" units="41"/> 
<week number="34" units="41"/> 
<week number="35" units="41"/> 
<week number="36" units="41"/> 
<week number="37" units="41"/> 
<week number="38" units="41"/> 
<week number="39" units="41"/> 
<week number="40" units="41"/> 
<week number="41" units="41"/> 
<week number="42" units="41"/> 
<week number="43" units="41"/> 
<week number="44" units="41"/> 
<week number="45" units="41"/> 
<week number="46" units="41"/> 
<week number="47" units="41"/> 
<week number="48" units="41"/> 
<week number="49" units="41"/> 
<week number="50" units="41"/> 
<week number="41" units="41"/> 
<week number="52" units="41"/> 

</year> <year number="2"> 
<week number="1" units="41"/> 
<week number="2" units="41"/> 
<week number="3" units="41"/> 
<week number="4" units="41"/> 
<week number="5" units="41"/> 
<week number="6" units="41"/> 
<week number="7" units="41"/> 
<week number="8" units="41"/> 
<week number="9" units="41"/> 
<week number="10" units="41"/> 
<week number="11" units="41"/> 
<week number="12" units="41"/> 
<week number="13" units="41"/> 
<week number="14" units="41"/> 
<week number="15" units="41"/> 
<week number="16" units="41"/> 
<week number="17" units="41"/> 
<week number="18" units="41"/> 
<week number="19" units="41"/> 
<week number="20" units="41"/> 
<week number="21" units="41"/> 
<week number="22" units="41"/> 
<week number="23" units="41"/> 
<week number="24" units="41"/> 
<week number="25" units="41"/> 
<week number="26" units="41"/> 
<week number="27" units="41"/> 
<week number="28" units="41"/> 
<week number="29" units="41"/> 
<week number="30" units="41"/> 
<week number="31" units="41"/> 
<week number="32" units="41"/> 
<week number="33" units="41"/> 
<week number="34" units="41"/> 
<week number="35" units="41"/> 
<week number="36" units="41"/> 
<week number="37" units="41"/> 
<week number="38" units="41"/> 
<week number="39" units="41"/> 
<week number="40" units="41"/> 
<week number="41" units="41"/> 
<week number="42" units="41"/> 
<week number="43" units="41"/> 
<week number="44" units="41"/> 
<week number="45" units="41"/> 
<week number="46" units="41"/> 
<week number="47" units="41"/> 
<week number="48" units="41"/> 
<week number="49" units="41"/> 
<week number="50" units="41"/> 
<week number="41" units="41"/> 
<week number="52" units="41"/> 

</year> <year number="3"> 
<week number="1" units="41"/> 
<week number="2" units="41"/> 
<week number="3" units="41"/> 
<week number="4" units="41"/> 
<week number="5" units="41"/> 
<week number="6" units="41"/> 
<week number="7" units="41"/> 
<week number="8" units="41"/> 
<week number="9" units="41"/> 
<week number="10" units="41"/> 
<week number="11" units="41"/> 
<week number="12" units="41"/> 
<week number="13" units="41"/> 
<week number="14" units="41"/> 
<week number="15" units="41"/> 
<week number="16" units="41"/> 
<week number="17" units="41"/> 
<week number="18" units="41"/> 
<week number="19" units="41"/> 
<week number="20" units="41"/> 
<week number="21" units="41"/> 
<week number="22" units="41"/> 
<week number="23" units="41"/> 
<week number="24" units="41"/> 
<week number="25" units="41"/> 
<week number="26" units="41"/> 
<week number="27" units="41"/> 
<week number="28" units="41"/> 
<week number="29" units="41"/> 
<week number="30" units="41"/> 
<week number="31" units="41"/> 
<week number="32" units="41"/> 
<week number="33" units="41"/> 
<week number="34" units="41"/> 
<week number="35" units="41"/> 
<week number="36" units="41"/> 
<week number="37" units="41"/> 
<week number="38" units="41"/> 
<week number="39" units="41"/> 
<week number="40" units="41"/> 
<week number="41" units="41"/> 
<week number="42" units="41"/> 
<week number="43" units="41"/> 
<week number="44" units="41"/> 
<week number="45" units="41"/> 
<week number="46" units="41"/> 
<week number="47" units="41"/> 
<week number="48" units="41"/> 
<week number="49" units="41"/> 
<week number="50" units="41"/> 
<week number="41" units="41"/> 
<week number="52" units="41"/> 

</year> <year number="4"> 
<week number="1" units="41"/> 
<week number="2" units="41"/> 
<week number="3" units="41"/> 
<week number="4" units="41"/> 
<week number="5" units="41"/> 
<week number="6" units="41"/> 
<week number="7" units="41"/> 
<week number="8" units="41"/> 
<week number="9" units="41"/> 
<week number="10" units="41"/> 
<week number="11" units="41"/> 
<week number="12" units="41"/> 
<week number="13" units="41"/> 
<week number="14" units="41"/> 
<week number="15" units="41"/> 
<week number="16" units="41"/> 
<week number="17" units="41"/> 
<week number="18" units="41"/> 
<week number="19" units="41"/> 
<week number="20" units="41"/> 
<week number="21" units="41"/> 
<week number="22" units="41"/> 
<week number="23" units="41"/> 
<week number="24" units="41"/> 
<week number="25" units="41"/> 
<week number="26" units="41"/> 
<week number="27" units="41"/> 
<week number="28" units="41"/> 
<week number="29" units="41"/> 
<week number="30" units="41"/> 
<week number="31" units="41"/> 
<week number="32" units="41"/> 
<week number="33" units="41"/> 
<week number="34" units="41"/> 
<week number="35" units="41"/> 
<week number="36" units="41"/> 
<week number="37" units="41"/> 
<week number="38" units="41"/> 
<week number="39" units="41"/> 
<week number="40" units="41"/> 
<week number="41" units="41"/> 
<week number="42" units="41"/> 
<week number="43" units="41"/> 
<week number="44" units="41"/> 
<week number="45" units="41"/> 
<week number="46" units="41"/> 
<week number="47" units="41"/> 
<week number="48" units="41"/> 
<week number="49" units="41"/> 
<week number="50" units="41"/> 
<week number="41" units="41"/> 
<week number="52" units="41"/> 

</year> -<year number="5"> 
<week number="1" units="41"/> 
<week number="2" units="41"/> 
<week number="3" units="41"/> 
. 
. 
. 
<week number="52" units="41"/> 

</year> </years> </priceBand> 

<priceBand id="3" end="999.99" start="500.00"> <years> <year number="1"> 
<week number="1" units="41"/> 
<week number="2" units="41"/> 
<week number="3" units="41"/> 
<week number="4" units="41"/> 
<week number="5" units="41"/> 
<week number="6" units="41"/> 
<week number="7" units="41"/> 
<week number="8" units="41"/> 
<week number="9" units="41"/> 
<week number="10" units="41"/> 
<week number="11" units="41"/> 
<week number="12" units="41"/> 
<week number="13" units="41"/> 
<week number="14" units="41"/> 
<week number="15" units="41"/> 
<week number="16" units="41"/> 
<week number="17" units="41"/> 
<week number="18" units="41"/> 
<week number="19" units="41"/> 
<week number="20" units="41"/> 
<week number="21" units="41"/> 
<week number="22" units="41"/> 
<week number="23" units="41"/> 
<week number="24" units="41"/> 
<week number="25" units="41"/> 
<week number="26" units="41"/> 
<week number="27" units="41"/> 
<week number="28" units="41"/> 
<week number="29" units="41"/> 
<week number="30" units="41"/> 
<week number="31" units="41"/> 
<week number="32" units="41"/> 
<week number="33" units="41"/> 
<week number="34" units="41"/> 
<week number="35" units="41"/> 
<week number="36" units="41"/> 
<week number="37" units="41"/> 
<week number="38" units="41"/> 
<week number="39" units="41"/> 
<week number="40" units="41"/> 
<week number="41" units="41"/> 
<week number="42" units="41"/> 
<week number="43" units="41"/> 
<week number="44" units="41"/> 
<week number="45" units="41"/> 
<week number="46" units="41"/> 
<week number="47" units="41"/> 
<week number="48" units="41"/> 
<week number="49" units="41"/> 
<week number="50" units="41"/> 
<week number="41" units="41"/> 
<week number="52" units="41"/> 

</year> <year number="2"> 
<week number="1" units="41"/> 
<week number="2" units="41"/> 
<week number="3" units="41"/> 
<week number="4" units="41"/> 
<week number="5" units="41"/> 
<week number="6" units="41"/> 
<week number="7" units="41"/> 
<week number="8" units="41"/> 
<week number="9" units="41"/> 
<week number="10" units="41"/> 
<week number="11" units="41"/> 
<week number="12" units="41"/> 
<week number="13" units="41"/> 
<week number="14" units="41"/> 
<week number="15" units="41"/> 
<week number="16" units="41"/> 
<week number="17" units="41"/> 
<week number="18" units="41"/> 
<week number="19" units="41"/> 
<week number="20" units="41"/> 
<week number="21" units="41"/> 
<week number="22" units="41"/> 
<week number="23" units="41"/> 
<week number="24" units="41"/> 
<week number="25" units="41"/> 
<week number="26" units="41"/> 
<week number="27" units="41"/> 
<week number="28" units="41"/> 
<week number="29" units="41"/> 
<week number="30" units="41"/> 
<week number="31" units="41"/> 
<week number="32" units="41"/> 
<week number="33" units="41"/> 
<week number="34" units="41"/> 
<week number="35" units="41"/> 
<week number="36" units="41"/> 
<week number="37" units="41"/> 
<week number="38" units="41"/> 
<week number="39" units="41"/> 
<week number="40" units="41"/> 
<week number="41" units="41"/> 
<week number="42" units="41"/> 
<week number="43" units="41"/> 
<week number="44" units="41"/> 
<week number="45" units="41"/> 
<week number="46" units="41"/> 
<week number="47" units="41"/> 
<week number="48" units="41"/> 
<week number="49" units="41"/> 
<week number="50" units="41"/> 
<week number="41" units="41"/> 
<week number="52" units="41"/> 

</year> <year number="3"> 
<week number="1" units="41"/> 
<week number="2" units="41"/> 
<week number="3" units="41"/> 
<week number="4" units="41"/> 
<week number="5" units="41"/> 
<week number="6" units="41"/> 
<week number="7" units="41"/> 
<week number="8" units="41"/> 
<week number="9" units="41"/> 
<week number="10" units="41"/> 
<week number="11" units="41"/> 
<week number="12" units="41"/> 
<week number="13" units="41"/> 
<week number="14" units="41"/> 
<week number="15" units="41"/> 
<week number="16" units="41"/> 
<week number="17" units="41"/> 
<week number="18" units="41"/> 
<week number="19" units="41"/> 
<week number="20" units="41"/> 
<week number="21" units="41"/> 
<week number="22" units="41"/> 
<week number="23" units="41"/> 
<week number="24" units="41"/> 
<week number="25" units="41"/> 
<week number="26" units="41"/> 
<week number="27" units="41"/> 
<week number="28" units="41"/> 
<week number="29" units="41"/> 
<week number="30" units="41"/> 
<week number="31" units="41"/> 
<week number="32" units="41"/> 
<week number="33" units="41"/> 
<week number="34" units="41"/> 
<week number="35" units="41"/> 
<week number="36" units="41"/> 
<week number="37" units="41"/> 
<week number="38" units="41"/> 
<week number="39" units="41"/> 
<week number="40" units="41"/> 
<week number="41" units="41"/> 
<week number="42" units="41"/> 
<week number="43" units="41"/> 
<week number="44" units="41"/> 
<week number="45" units="41"/> 
<week number="46" units="41"/> 
<week number="47" units="41"/> 
<week number="48" units="41"/> 
<week number="49" units="41"/> 
<week number="50" units="41"/> 
<week number="41" units="41"/> 
<week number="52" units="41"/> 

</year> -<year number="4"> 
<week number="1" units="41"/> 
<week number="2" units="41"/> 
<week number="3" units="41"/> 
<week number="4" units="41"/> 
<week number="5" units="41"/> 
<week number="6" units="41"/> 
<week number="7" units="41"/> 
<week number="8" units="41"/> 
<week number="9" units="41"/> 
<week number="10" units="41"/> 
<week number="11" units="41"/> 
<week number="12" units="41"/> 
<week number="13" units="41"/> 
<week number="14" units="41"/> 
<week number="15" units="41"/> 
<week number="16" units="41"/> 
<week number="17" units="41"/> 
<week number="18" units="41"/> 
<week number="19" units="41"/> 
<week number="20" units="41"/> 
<week number="21" units="41"/> 
<week number="22" units="41"/> 
<week number="23" units="41"/> 
<week number="24" units="41"/> 
<week number="25" units="41"/> 
<week number="26" units="41"/> 
<week number="27" units="41"/> 
<week number="28" units="41"/> 
<week number="29" units="41"/> 
<week number="30" units="41"/> 
<week number="31" units="41"/> 
<week number="32" units="41"/> 
<week number="33" units="41"/> 
<week number="34" units="41"/> 
<week number="35" units="41"/> 
<week number="36" units="41"/> 
<week number="37" units="41"/> 
<week number="38" units="41"/> 
<week number="39" units="41"/> 
<week number="40" units="41"/> 
<week number="41" units="41"/> 
<week number="42" units="41"/> 
<week number="43" units="41"/> 
<week number="44" units="41"/> 
<week number="45" units="41"/> 
<week number="46" units="41"/> 
<week number="47" units="41"/> 
<week number="48" units="41"/> 
<week number="49" units="41"/> 
<week number="50" units="41"/> 
<week number="41" units="41"/> 
<week number="52" units="41"/> 

</year> <year number="5"> 
<week number="1" units="41"/> 
<week number="2" units="41"/> 
<week number="3" units="41"/> 
<week number="4" units="41"/> 
<week number="5" units="41"/> 
<week number="6" units="41"/> 
<week number="7" units="41"/> 
<week number="8" units="41"/> 
<week number="9" units="41"/> 
<week number="10" units="41"/> 
<week number="11" units="41"/> 
<week number="12" units="41"/> 
<week number="13" units="41"/> 
<week number="14" units="41"/> 
<week number="15" units="41"/> 
<week number="16" units="41"/> 
<week number="17" units="41"/> 
<week number="18" units="41"/> 
<week number="19" units="41"/> 
<week number="20" units="41"/> 
<week number="21" units="41"/> 
<week number="22" units="41"/> 
<week number="23" units="41"/> 
<week number="24" units="41"/> 
<week number="25" units="41"/> 
<week number="26" units="41"/> 
<week number="27" units="41"/> 
<week number="28" units="41"/> 
<week number="29" units="41"/> 
<week number="30" units="41"/> 
<week number="31" units="41"/> 
<week number="32" units="41"/> 
<week number="33" units="41"/> 
<week number="34" units="41"/> 
<week number="35" units="41"/> 
<week number="36" units="41"/> 
<week number="37" units="41"/> 
<week number="38" units="41"/> 
<week number="39" units="41"/> 
<week number="40" units="41"/> 
<week number="41" units="41"/> 
<week number="42" units="41"/> 
<week number="43" units="41"/> 
<week number="44" units="41"/> 
<week number="45" units="41"/> 
<week number="46" units="41"/> 
<week number="47" units="41"/> 
<week number="48" units="41"/> 
<week number="49" units="41"/> 
<week number="50" units="41"/> 
<week number="41" units="41"/> 
<week number="52" units="41"/> 
</year> </years> 
</priceBand> </priceBands> 
+0

[관련 질문] (http://stackoverflow.com/q/40923206/5089204)에서 말씀 드렸듯이. ** 샘플 데이터 축소 **를 포함시키는 것이 좋습니다. 논리를 이해하려면 그룹 당 몇 개의 행만 있으면됩니다. 다른 한편으로 ** 예상 출력에 해당 숫자 **를 더하는 것이 중요합니다. 외국인의 머리를 생각해보십시오. 나는 최선을 다하고 있습니다 ... 베스트는 [MCVE (자세한 내용은 여기)] – Shnugo

+0

more : 귀하의 질의는 주어진 테이블에 맞지 않습니다. 샘플 테이블에 연말 연시가 없습니다. ** MCVE **를 제공하면 KISS (* 작고 단순하게 유지)에 도움이되며, 그런 성가신 오류를 피하는 데 도움이됩니다 ... – Shnugo

+0

한 번 더 힌트 : 이전 질문을보십시오. 나는 당신을 위해 당신의 일을했습니다 ... 어떻게 테이블 변수가 선언되고 채워지는지보십시오. 이것은 정확히 당신이 여기서해야 할 일입니다. ** ** !! ** 데이터를 가지고 있습니다. – Shnugo

답변

0

내 마법의 유리 전구가 나에게 말한다 (SAMPLE ONLY-그림이 정확하지 않을 수 있습니다) 당신이 이런 식으로 뭔가를 찾고있을 수도 있습니다 :

하지만 내 질문에 대한 답변을 참조하십시오. r ...

SELECT pricebandID AS [@id] 
     ,pricedTo AS [@end] 
     ,pricedFrom AS [@start] 
     ,(
     SELECT cast(yearnumber as int) AS '@number' 
     ,(convert(varchar(12), cast(yearstart as date), 126)) AS '@yearstart' 
      ,(convert(varchar(12), cast(yearend as date), 126)) AS '@yearend' 
      ,(
      SELECT cast(X.WeekNumber as int) AS '@number' 
        ,cast(x.Quantity as int) AS '@units'  
      FROM @george_mdx AS X 
      WHERE cast(X.YearNumber as int)=cast(tbl.YearNumber as int) 
       AND X.unitprice BETWEEN pl.pricedFrom AND pl.pricedTo 
      FOR XML PATH('week'),TYPE 
      ) 
     FROM @george_mdx AS tbl 
     WHERE tbl.unitprice BETWEEN pl.pricedFrom AND pl.pricedTo 
     GROUP BY YearNumber,YearStart,YearEnd 
     FOR XML PATH('year'),ROOT('years') 
     ) 

FROM @Planning AS pl 
FOR XML PATH('priceBand'),ROOT('priceBands') 
+0

감사합니다 Shnugo - 몇 가지 수정 작업 –